Refactor tweet-formatting logic in to it's own functions

This commit is contained in:
jesopo 2019-05-22 19:42:36 +01:00
parent 02ddb85167
commit 801eb5adb4

View file

@ -19,45 +19,22 @@ class Module(ModuleManager.BaseModule):
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)
@utils.hook("received.command.tw", alias_of="tweet") def _get_api(self):
@utils.hook("received.command.tweet")
def tweet(self, event):
"""
:help: Get/find a tweet
:usage: [@username/URL/ID]
"""
api_key = self.bot.config["twitter-api-key"] api_key = self.bot.config["twitter-api-key"]
api_secret = self.bot.config["twitter-api-secret"] api_secret = self.bot.config["twitter-api-secret"]
access_token = self.bot.config["twitter-access-token"] access_token = self.bot.config["twitter-access-token"]
access_secret = self.bot.config["twitter-access-secret"] access_secret = self.bot.config["twitter-access-secret"]
return twitter.Twitter(auth=twitter.OAuth(
if event["args"]:
target = event["args"]
else:
target = event["target"].buffer.find(REGEX_TWITTERURL)
if target:
target = target.message
if target:
twitter_object = twitter.Twitter(auth=twitter.OAuth(
access_token, access_secret, api_key, api_secret)) access_token, access_secret, api_key, api_secret))
url_match = re.search(REGEX_TWITTERURL, target)
if url_match or target.isdigit(): def _from_id(self, tweet_id):
tweet_id = url_match.group(1) if url_match else target api = self._get_api()
try: try:
tweet = twitter_object.statuses.show(id=tweet_id) return api.statuses.show(id=tweet_id)
except: except:
traceback.print_exc() traceback.print_exc()
tweet = None
else: def _format_tweet(self, tweet):
if target.startswith("@"):
target = target[1:]
try:
tweet = twitter_object.statuses.user_timeline(
screen_name=target, count=1)[0]
except:
traceback.print_exc()
tweet = None
if tweet:
linked_id = tweet["id"] linked_id = tweet["id"]
username = tweet["user"]["screen_name"] username = tweet["user"]["screen_name"]
@ -75,15 +52,47 @@ class Module(ModuleManager.BaseModule):
"created_at"]) "created_at"])
original_timestamp = self.make_timestamp(tweet[ original_timestamp = self.make_timestamp(tweet[
"retweeted_status"]["created_at"]) "retweeted_status"]["created_at"])
event["stdout"].write( return "(@%s (%s) retweeted %s (%s)) %s%s" % (
"(@%s (%s) retweeted %s (%s)) %s%s" % (
username, retweet_timestamp, original_username, username, retweet_timestamp, original_username,
original_timestamp, html.unescape(original_text), original_timestamp, html.unescape(original_text),
short_url)) short_url)
else: else:
event["stdout"].write("(@%s, %s) %s%s" % (username, return "(@%s, %s) %s%s" % (username,
self.make_timestamp(tweet["created_at"]), self.make_timestamp(tweet["created_at"]),
html.unescape(tweet["text"]), short_url)) html.unescape(tweet["text"]), short_url)
@utils.hook("received.command.tw", alias_of="tweet")
@utils.hook("received.command.tweet")
def tweet(self, event):
"""
:help: Get/find a tweet
:usage: [@username/URL/ID]
"""
if event["args"]:
target = event["args"]
else:
target = event["target"].buffer.find(REGEX_TWITTERURL)
if target:
target = target.message
if target:
api = self._get_api()
url_match = re.search(REGEX_TWITTERURL, target)
if url_match or target.isdigit():
tweet_id = url_match.group(1) if url_match else target
tweet = self._from_id(tweet_id)
else:
if target.startswith("@"):
target = target[1:]
try:
tweet = twitter_object.statuses.user_timeline(
screen_name=target, count=1)[0]
except:
traceback.print_exc()
tweet = None
if tweet:
tweet_str = self._format_tweet(tweet)
event["stdout"].write(tweet_str)
else: else:
event["stderr"].write("Invalid tweet identifiers provided") event["stderr"].write("Invalid tweet identifiers provided")
else: else: