From 58ba722bd771cba973d265468ccd778697e388c1 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 10 Dec 2021 17:15:24 -0600 Subject: [PATCH] Added templates for direct messages and sent direct messages --- src/Conf.defaults | 2 ++ .../buffers/twitter/directMessages.py | 16 +++++++-- src/sessions/twitter/templates.py | 33 +++++++++++++++++-- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/Conf.defaults b/src/Conf.defaults index 3d684a65..8777227b 100644 --- a/src/Conf.defaults +++ b/src/Conf.defaults @@ -50,6 +50,8 @@ speech_reporting = boolean(default=True) [templates] tweet = string(default="$display_name, $text $image_descriptions $date. $source") +dm = string(default="$sender_display_name, $text $date") +dm_sent = string(default=">$recipient_display_name, $text $date") [filters] diff --git a/src/controller/buffers/twitter/directMessages.py b/src/controller/buffers/twitter/directMessages.py index 06cbed9a..776521d0 100644 --- a/src/controller/buffers/twitter/directMessages.py +++ b/src/controller/buffers/twitter/directMessages.py @@ -8,7 +8,7 @@ import config import languageHandler import logging from controller import messages -from sessions.twitter import compose, utils +from sessions.twitter import compose, utils, templates from mysc.thread_utils import call_threaded from tweepy.errors import TweepyException from pubsub import pub @@ -129,6 +129,12 @@ class DirectMessagesBuffer(base.BaseBuffer): def open_in_browser(self, *args, **kwargs): output.speak(_(u"This action is not supported in the buffer yet.")) + def get_message(self): + template = self.session.settings["templates"]["dm"] + dm = self.get_right_tweet() + t = templates.render_dm(dm, template, self.session, relative_times=self.session.settings["general"]["relative_times"], offset_seconds=self.session.db["utc_offset"]) + return t + class SentDirectMessagesBuffer(DirectMessagesBuffer): def __init__(self, *args, **kwargs): @@ -150,4 +156,10 @@ class SentDirectMessagesBuffer(DirectMessagesBuffer): else: for i in items: tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) - self.buffer.list.insert_item(False, *tweet) \ No newline at end of file + self.buffer.list.insert_item(False, *tweet) + + def get_message(self): + template = self.session.settings["templates"]["dm_sent"] + dm = self.get_right_tweet() + t = templates.render_dm(dm, template, self.session, relative_times=self.session.settings["general"]["relative_times"], offset_seconds=self.session.db["utc_offset"]) + return t diff --git a/src/sessions/twitter/templates.py b/src/sessions/twitter/templates.py index b35c1e8c..5a5cdf86 100644 --- a/src/sessions/twitter/templates.py +++ b/src/sessions/twitter/templates.py @@ -14,10 +14,10 @@ def process_date(field, relative_times=True, offset_seconds=0): return ts def process_text(tweet): - if hasattr(tweet, "text"): - text = tweet.text - elif hasattr(tweet, "full_text"): + if hasattr(tweet, "full_text"): text = tweet.full_text + elif hasattr(tweet, "text"): + text = tweet.text # Cleanup mentions, so we'll remove more than 2 mentions to make the tweet easier to read. text = utils.clean_mentions(text) # Replace URLS for extended version of those. @@ -77,4 +77,31 @@ def render_tweet(tweet, template, session, relative_times=False, offset_seconds= available_data.update(image_descriptions=image_descriptions) result = Template(template).safe_substitute(**available_data) result = re.sub(r"\$\w+", "", result) + return result + +def render_dm(dm, template, session, relative_times=False, offset_seconds=0): + """ Renders direct messages by using the provided template. + Available data will be stored in the following variables: + $date: Creation date. + $sender_display_name: User profile name for user who sent the dm. + $sender_screen_name: User screen name for user sending the dm, this is the same name used to reference the user in Twitter. + $recipient_display_name: User profile name for user who received the dm. + $recipient_screen_name: User screen name for user receiving the dm, this is the same name used to reference the user in Twitter. + $text: Text of the direct message. + """ + available_data = dict() + available_data.update(text=utils.expand_urls(dm.message_create["message_data"]["text"], dm.message_create["message_data"]["entities"])) + # Let's remove the last 3 digits in the timestamp string. + # Twitter sends their "epoch" timestamp with 3 digits for milliseconds and arrow doesn't like it. + original_date = arrow.get(int(dm.created_timestamp)) + if relative_times == True: + ts = original_date.humanize(locale=languageHandler.curLang[:2]) + else: + ts = original_date.shift(seconds=offset_seconds) + available_data.update(date=ts) + sender = session.get_user(dm.message_create["sender_id"]) + recipient = session.get_user(dm.message_create["target"]["recipient_id"]) + available_data.update(sender_display_name=sender.name, sender_screen_name=sender.screen_name, recipient_display_name=recipient.name, recipient_screen_name=recipient.screen_name) + result = Template(template).safe_substitute(**available_data) + result = re.sub(r"\$\w+", "", result) return result \ No newline at end of file