aboutsummaryrefslogtreecommitdiff
path: root/modules/tweets.py
diff options
context:
space:
mode:
authorGravatar dngfx2018-09-09 13:12:06 +0100
committerGravatar dongfix2018-09-09 13:12:06 +0100
commit7aecd2b4ca4a2dc05146c49aaa44fffdf16fa5bd (patch)
tree704834d6f16982d408007d1ca895c24d50cc920a /modules/tweets.py
parentBetter indentation in commands.py (diff)
signature
WOOP WOOP THAT'S THE SOUND OF LINKS FOR TWEETS
Diffstat (limited to 'modules/tweets.py')
-rw-r--r--modules/tweets.py59
1 files changed, 43 insertions, 16 deletions
diff --git a/modules/tweets.py b/modules/tweets.py
index ff628b6e..93492212 100644
--- a/modules/tweets.py
+++ b/modules/tweets.py
@@ -1,7 +1,7 @@
-#--require-config twitter-api-key
-#--require-config twitter-api-secret
-#--require-config twitter-access-token
-#--require-config twitter-access-secret
+# --require-config twitter-api-key
+# --require-config twitter-api-secret
+# --require-config twitter-access-token
+# --require-config twitter-access-secret
import datetime, re, time, traceback
import twitter
@@ -10,16 +10,23 @@ import Utils
REGEX_TWITTERURL = re.compile(
"https?://(?:www\.)?twitter.com/[^/]+/status/(\d+)", re.I)
+
class Module(object):
+
def __init__(self, bot, events, exports):
self.bot = bot
+ self.bitly_is_enabled = "bitly" in self.bot.modules.modules
+
events.on("received").on("command").on("tweet", "tw"
- ).hook(self.tweet, help="Find a tweet",
- usage="[@username/URL/ID]")
+ ).hook(self.tweet,
+ help="Find a tweet",
+ usage="[@username/URL/ID]")
def make_timestamp(self, s):
- seconds_since = time.time()-datetime.datetime.strptime(s,
- "%a %b %d %H:%M:%S %z %Y").timestamp()
+ seconds_since = time.time() - datetime.datetime.strptime(s,
+ "%a %b %d "
+ "%H:%M:%S %z "
+ "%Y").timestamp()
since, unit = Utils.time_unit(seconds_since)
return "%s %s ago" % (since, unit)
@@ -56,21 +63,41 @@ class Module(object):
traceback.print_exc()
tweet = None
if tweet:
+ linked_id = tweet["id"]
username = "@%s" % tweet["user"]["screen_name"]
+
+ bitly_link = ""
+ if self.bitly_is_enabled:
+ bitly = self.bot.modules.modules["bitly"]
+ chopped_uname = username[1:]
+ tweet_link = "https://twitter.com/%s/status/%s" % (
+ chopped_uname, linked_id)
+
+ bitly_link = " -- " + bitly.shortlink(tweet_link)
+
if "retweeted_status" in tweet:
original_username = "@%s" % tweet["retweeted_status"
- ]["user"]["screen_name"]
+ ]["user"]["screen_name"]
original_text = tweet["retweeted_status"]["text"]
retweet_timestamp = self.make_timestamp(tweet[
- "created_at"])
+ "created_at"])
original_timestamp = self.make_timestamp(tweet[
- "retweeted_status"]["created_at"])
- event["stdout"].write("(%s (%s) retweeted %s (%s)) %s" % (
- username, retweet_timestamp,
- original_username, original_timestamp, original_text))
+ "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))
else:
- event["stdout"].write("(%s, %s) %s" % (username,
- self.make_timestamp(tweet["created_at"]), tweet["text"]))
+ event["stdout"].write("(%s, %s) %s %s" % (username,
+ self.make_timestamp(
+ tweet[
+ "created_at"]),
+ tweet["text"],
+ bitly_link)
+ )
else:
event["stderr"].write("Invalid tweet identifiers provided")
else: