WOOP WOOP THAT'S THE SOUND OF LINKS FOR TWEETS
This commit is contained in:
parent
1af290b954
commit
7aecd2b4ca
2 changed files with 45 additions and 17 deletions
|
@ -16,7 +16,8 @@ class Module(object):
|
||||||
usage="<url>")
|
usage="<url>")
|
||||||
|
|
||||||
def shortlink(self, event):
|
def shortlink(self, event):
|
||||||
url = event["url"]
|
url = event if type(event) is str else event["url"]
|
||||||
|
|
||||||
if not re.match(REGEX_URL, url):
|
if not re.match(REGEX_URL, url):
|
||||||
url = "http://%s" % url
|
url = "http://%s" % url
|
||||||
data = Utils.get_url(URL_BITLYSHORTEN, get_params={
|
data = Utils.get_url(URL_BITLYSHORTEN, get_params={
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#--require-config twitter-api-key
|
# --require-config twitter-api-key
|
||||||
#--require-config twitter-api-secret
|
# --require-config twitter-api-secret
|
||||||
#--require-config twitter-access-token
|
# --require-config twitter-access-token
|
||||||
#--require-config twitter-access-secret
|
# --require-config twitter-access-secret
|
||||||
|
|
||||||
import datetime, re, time, traceback
|
import datetime, re, time, traceback
|
||||||
import twitter
|
import twitter
|
||||||
|
@ -10,16 +10,23 @@ import Utils
|
||||||
REGEX_TWITTERURL = re.compile(
|
REGEX_TWITTERURL = re.compile(
|
||||||
"https?://(?:www\.)?twitter.com/[^/]+/status/(\d+)", re.I)
|
"https?://(?:www\.)?twitter.com/[^/]+/status/(\d+)", re.I)
|
||||||
|
|
||||||
|
|
||||||
class Module(object):
|
class Module(object):
|
||||||
|
|
||||||
def __init__(self, bot, events, exports):
|
def __init__(self, bot, events, exports):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
self.bitly_is_enabled = "bitly" in self.bot.modules.modules
|
||||||
|
|
||||||
events.on("received").on("command").on("tweet", "tw"
|
events.on("received").on("command").on("tweet", "tw"
|
||||||
).hook(self.tweet, help="Find a tweet",
|
).hook(self.tweet,
|
||||||
|
help="Find a tweet",
|
||||||
usage="[@username/URL/ID]")
|
usage="[@username/URL/ID]")
|
||||||
|
|
||||||
def make_timestamp(self, s):
|
def make_timestamp(self, s):
|
||||||
seconds_since = time.time()-datetime.datetime.strptime(s,
|
seconds_since = time.time() - datetime.datetime.strptime(s,
|
||||||
"%a %b %d %H:%M:%S %z %Y").timestamp()
|
"%a %b %d "
|
||||||
|
"%H:%M:%S %z "
|
||||||
|
"%Y").timestamp()
|
||||||
since, unit = Utils.time_unit(seconds_since)
|
since, unit = Utils.time_unit(seconds_since)
|
||||||
return "%s %s ago" % (since, unit)
|
return "%s %s ago" % (since, unit)
|
||||||
|
|
||||||
|
@ -56,7 +63,18 @@ class Module(object):
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
tweet = None
|
tweet = None
|
||||||
if tweet:
|
if tweet:
|
||||||
|
linked_id = tweet["id"]
|
||||||
username = "@%s" % tweet["user"]["screen_name"]
|
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:
|
if "retweeted_status" in tweet:
|
||||||
original_username = "@%s" % tweet["retweeted_status"
|
original_username = "@%s" % tweet["retweeted_status"
|
||||||
]["user"]["screen_name"]
|
]["user"]["screen_name"]
|
||||||
|
@ -64,13 +82,22 @@ class Module(object):
|
||||||
retweet_timestamp = self.make_timestamp(tweet[
|
retweet_timestamp = self.make_timestamp(tweet[
|
||||||
"created_at"])
|
"created_at"])
|
||||||
original_timestamp = self.make_timestamp(tweet[
|
original_timestamp = self.make_timestamp(tweet[
|
||||||
"retweeted_status"]["created_at"])
|
"retweeted_status"][
|
||||||
event["stdout"].write("(%s (%s) retweeted %s (%s)) %s" % (
|
"created_at"])
|
||||||
|
event["stdout"].write(
|
||||||
|
"(%s (%s) retweeted %s (%s)) %s %s" % (
|
||||||
username, retweet_timestamp,
|
username, retweet_timestamp,
|
||||||
original_username, original_timestamp, original_text))
|
original_username, original_timestamp,
|
||||||
|
original_text,
|
||||||
|
bitly_link))
|
||||||
else:
|
else:
|
||||||
event["stdout"].write("(%s, %s) %s" % (username,
|
event["stdout"].write("(%s, %s) %s %s" % (username,
|
||||||
self.make_timestamp(tweet["created_at"]), tweet["text"]))
|
self.make_timestamp(
|
||||||
|
tweet[
|
||||||
|
"created_at"]),
|
||||||
|
tweet["text"],
|
||||||
|
bitly_link)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Invalid tweet identifiers provided")
|
event["stderr"].write("Invalid tweet identifiers provided")
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue