Added templates for direct messages and sent direct messages

This commit is contained in:
Manuel Cortez 2021-12-10 17:15:24 -06:00
parent 416151570c
commit 58ba722bd7
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790
3 changed files with 46 additions and 5 deletions

View File

@ -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]

View File

@ -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)
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

View File

@ -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