Merge pull request #20 from dngfx/master

Add bit.ly links to tweets
This commit is contained in:
jesopo 2018-09-09 15:50:48 +01:00 committed by GitHub
commit 820523a28d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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.events = events
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,46 @@ class Module(object):
traceback.print_exc()
tweet = None
if tweet:
linked_id = tweet["id"]
username = "@%s" % tweet["user"]["screen_name"]
url_shortener_link = False
chopped_uname = username[1:]
tweet_link = "https://twitter.com/%s/status/%s" % (
chopped_uname, linked_id)
url_shortener_link = self.events.on("get").on(
"shortlink").call(
url=tweet_link)[0]
url_shortener_link = "" if url_shortener_link == False else \
"-- " + url_shortener_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,
url_shortener_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"],
url_shortener_link)
)
else:
event["stderr"].write("Invalid tweet identifiers provided")
else: