2018-09-09 12:12:06 +00:00
|
|
|
# --require-config twitter-api-key
|
|
|
|
# --require-config twitter-api-secret
|
|
|
|
# --require-config twitter-access-token
|
|
|
|
# --require-config twitter-access-secret
|
2016-03-30 18:31:23 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import datetime, re, time, traceback
|
|
|
|
import twitter
|
|
|
|
import Utils
|
|
|
|
|
|
|
|
REGEX_TWITTERURL = re.compile(
|
|
|
|
"https?://(?:www\.)?twitter.com/[^/]+/status/(\d+)", re.I)
|
|
|
|
|
2018-09-09 12:12:06 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Module(object):
|
2018-09-09 12:12:06 +00:00
|
|
|
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-09-09 12:12:06 +00:00
|
|
|
self.bitly_is_enabled = "bitly" in self.bot.modules.modules
|
2018-09-09 12:20:59 +00:00
|
|
|
self.events = events
|
2018-09-09 12:12:06 +00:00
|
|
|
|
2018-09-04 10:30:00 +00:00
|
|
|
events.on("received").on("command").on("tweet", "tw"
|
2018-09-09 12:12:06 +00:00
|
|
|
).hook(self.tweet,
|
|
|
|
help="Find a tweet",
|
|
|
|
usage="[@username/URL/ID]")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def make_timestamp(self, s):
|
2018-09-09 12:12:06 +00:00
|
|
|
seconds_since = time.time() - datetime.datetime.strptime(s,
|
|
|
|
"%a %b %d "
|
|
|
|
"%H:%M:%S %z "
|
|
|
|
"%Y").timestamp()
|
2016-03-29 11:56:58 +00:00
|
|
|
since, unit = Utils.time_unit(seconds_since)
|
|
|
|
return "%s %s ago" % (since, unit)
|
|
|
|
|
2018-09-04 10:30:00 +00:00
|
|
|
def tweet(self, event):
|
2016-03-29 11:56:58 +00:00
|
|
|
api_key = self.bot.config["twitter-api-key"]
|
|
|
|
api_secret = self.bot.config["twitter-api-secret"]
|
|
|
|
access_token = self.bot.config["twitter-access-token"]
|
|
|
|
access_secret = self.bot.config["twitter-access-secret"]
|
|
|
|
|
|
|
|
if event["args"]:
|
|
|
|
target = event["args"]
|
|
|
|
else:
|
2018-08-28 11:23:57 +00:00
|
|
|
target = event["buffer"].find(REGEX_TWITTERURL)
|
2016-03-29 11:56:58 +00:00
|
|
|
if target:
|
|
|
|
target = target.message
|
|
|
|
if target:
|
|
|
|
twitter_object = twitter.Twitter(auth=twitter.OAuth(
|
|
|
|
access_token, access_secret, api_key, api_secret))
|
|
|
|
url_match = re.search(REGEX_TWITTERURL, target)
|
|
|
|
if url_match or target.isdigit():
|
|
|
|
tweet_id = url_match.group(1) if url_match else target
|
|
|
|
try:
|
|
|
|
tweet = twitter_object.statuses.show(id=tweet_id)
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
tweet = None
|
2016-03-31 11:10:18 +00:00
|
|
|
else:
|
|
|
|
if target.startswith("@"):
|
|
|
|
taret = target[1:]
|
2016-03-29 11:56:58 +00:00
|
|
|
try:
|
|
|
|
tweet = twitter_object.statuses.user_timeline(
|
2016-03-31 11:10:18 +00:00
|
|
|
screen_name=target, count=1)[0]
|
2016-03-29 11:56:58 +00:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
tweet = None
|
|
|
|
if tweet:
|
2018-09-09 12:12:06 +00:00
|
|
|
linked_id = tweet["id"]
|
2016-03-29 11:56:58 +00:00
|
|
|
username = "@%s" % tweet["user"]["screen_name"]
|
2018-09-09 12:12:06 +00:00
|
|
|
|
|
|
|
bitly_link = ""
|
|
|
|
if self.bitly_is_enabled:
|
|
|
|
chopped_uname = username[1:]
|
|
|
|
tweet_link = "https://twitter.com/%s/status/%s" % (
|
|
|
|
chopped_uname, linked_id)
|
|
|
|
|
2018-09-09 12:20:59 +00:00
|
|
|
bitly_link = " -- " + self.events.call("get.shortlink",
|
|
|
|
url=tweet_link)
|
2018-09-09 12:12:06 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if "retweeted_status" in tweet:
|
|
|
|
original_username = "@%s" % tweet["retweeted_status"
|
2018-09-09 12:12:06 +00:00
|
|
|
]["user"]["screen_name"]
|
2016-03-29 11:56:58 +00:00
|
|
|
original_text = tweet["retweeted_status"]["text"]
|
|
|
|
retweet_timestamp = self.make_timestamp(tweet[
|
2018-09-09 12:12:06 +00:00
|
|
|
"created_at"])
|
2016-03-29 11:56:58 +00:00
|
|
|
original_timestamp = self.make_timestamp(tweet[
|
2018-09-09 12:12:06 +00:00
|
|
|
"retweeted_status"][
|
|
|
|
"created_at"])
|
|
|
|
event["stdout"].write(
|
|
|
|
"(%s (%s) retweeted %s (%s)) %s %s" % (
|
|
|
|
username, retweet_timestamp,
|
|
|
|
original_username, original_timestamp,
|
|
|
|
original_text,
|
|
|
|
bitly_link))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-09-09 12:12:06 +00:00
|
|
|
event["stdout"].write("(%s, %s) %s %s" % (username,
|
|
|
|
self.make_timestamp(
|
|
|
|
tweet[
|
|
|
|
"created_at"]),
|
|
|
|
tweet["text"],
|
|
|
|
bitly_link)
|
|
|
|
)
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Invalid tweet identifiers provided")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No tweet provided to get information about")
|