From 2e8c8c6db4a1e4ac5c5c283f2f75e0f6bc9ba742 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Tue, 15 Nov 2022 11:54:59 -0600 Subject: [PATCH] Rework sessions to be handled with unique names --- src/controller/mainController.py | 66 +++++++++++++++++-------------- src/controller/twitter/handler.py | 12 +++--- src/sessions/mastodon/session.py | 2 +- src/sessions/twitter/session.py | 22 +++++------ src/sessions/twitter/streaming.py | 14 +++---- 5 files changed, 61 insertions(+), 55 deletions(-) diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 339e477f..f076d298 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -899,17 +899,19 @@ class Controller(object): if message != None: output.speak(message, speech=session.settings["reporting"]["speech_reporting"], braille=session.settings["reporting"]["braille_reporting"]) - def manage_sent_dm(self, data, user): - buffer = self.search_buffer("sent_direct_messages", user) - if buffer == None: return + def manage_sent_dm(self, data, session_name): + buffer = self.search_buffer("sent_direct_messages", session_name) + if buffer == None: + return play_sound = "dm_sent.ogg" if "sent_direct_messages" not in buffer.session.settings["other_buffers"]["muted_buffers"]: self.notify(buffer.session, play_sound=play_sound) buffer.add_new_item(data) - def manage_sent_tweets(self, data, user): - buffer = self.search_buffer("sent_tweets", user) - if buffer == None: return + def manage_sent_tweets(self, data, session_name): + buffer = self.search_buffer("sent_tweets", session_name) + if buffer == None: + return data = buffer.session.check_quoted_status(data) data = buffer.session.check_long_tweet(data) if data == False: # Long tweet deleted from twishort. @@ -922,37 +924,43 @@ class Controller(object): buffer.session.db[buffer.name] = items buffer.add_new_item(data) - def manage_friend(self, data, user): - buffer = self.search_buffer("friends", user) - if buffer == None: return + def manage_friend(self, data, session_name): + buffer = self.search_buffer("friends", session_name) + if buffer == None: + return buffer.add_new_item(data) - def manage_unfollowing(self, item, user): - buffer = self.search_buffer("friends", user) - if buffer == None: return + def manage_unfollowing(self, item, session_name): + buffer = self.search_buffer("friends", session_name) + if buffer == None: + return buffer.remove_item(item) - def manage_favourite(self, data, user): - buffer = self.search_buffer("favourites", user) - if buffer == None: return + def manage_favourite(self, data, session_name): + buffer = self.search_buffer("favourites", session_name) + if buffer == None: + return play_sound = "favourite.ogg" if "favourites" not in buffer.session.settings["other_buffers"]["muted_buffers"]: self.notify(buffer.session, play_sound=play_sound) buffer.add_new_item(data) - def manage_unfavourite(self, item, user): - buffer = self.search_buffer("favourites", user) - if buffer == None: return + def manage_unfavourite(self, item, session_name): + buffer = self.search_buffer("favourites", session_name) + if buffer == None: + return buffer.remove_item(item) - def manage_blocked_user(self, data, user): - buffer = self.search_buffer("blocked", user) - if buffer == None: return + def manage_blocked_user(self, data, session_name): + buffer = self.search_buffer("blocked", session_name) + if buffer == None: + return buffer.add_new_item(data) - def manage_unblocked_user(self, item, user): - buffer = self.search_buffer("blocked", user) - if buffer == None: return + def manage_unblocked_user(self, item, session_name): + buffer = self.search_buffer("blocked", session_name) + if buffer == None: + return buffer.remove_item(item) def start_buffers(self, session): @@ -1151,8 +1159,8 @@ class Controller(object): if hasattr(buffer, "ocr_image"): return buffer.ocr_image() - def update_sent_dms(self, total, account): - sent_dms = self.search_buffer("sent_direct_messages", account) + def update_sent_dms(self, total, session_name): + sent_dms = self.search_buffer("sent_direct_messages", session_name) if sent_dms != None: sent_dms.put_items_on_list(total) @@ -1165,11 +1173,11 @@ class Controller(object): for i in sessions.sessions: sessions.sessions[i].save_persistent_data() - def manage_new_tweet(self, data, name, _buffers): + def manage_new_tweet(self, data, session_name, _buffers): sound_to_play = None for buff in _buffers: - buffer = self.search_buffer(buff, name) - if buffer == None or buffer.session.get_name() != name: + buffer = self.search_buffer(buff, session_name) + if buffer == None or buffer.session.get_name() != session_name: return buffer.add_new_item(data) if buff == "home_timeline": sound_to_play = "tweet_received.ogg" diff --git a/src/controller/twitter/handler.py b/src/controller/twitter/handler.py index 43a59a43..2d7a8e7d 100644 --- a/src/controller/twitter/handler.py +++ b/src/controller/twitter/handler.py @@ -44,27 +44,27 @@ class Handler(object): pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Blocked users"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_blocks", name="blocked", sessionObject=session, account=session.get_name())) elif i == 'muted': pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Muted users"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_mutes", name="muted", sessionObject=session, account=session.get_name())) - pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="timelines", account=session.db["user_name"])) + pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="timelines", account=name)) timelines_position =controller.view.search("timelines", name) for i in session.settings["other_buffers"]["timelines"]: pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_(u"Timeline for {}").format(i,), parent_tab=timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="user_timeline", name="%s-timeline" % (i,), sessionObject=session, account=session.get_name(), sound="tweet_timeline.ogg", bufferType=None, user_id=i, include_ext_alt_text=True, tweet_mode="extended")) - pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Likes timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="favs_timelines", account=session.db["user_name"])) + pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Likes timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="favs_timelines", account=name)) favs_timelines_position =controller.view.search("favs_timelines", name) for i in session.settings["other_buffers"]["favourites_timelines"]: - pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_("Likes for {}").format(i,), parent_tab=favs_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_favorites", name="%s-favorite" % (i,), sessionObject=session, account=session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, include_ext_alt_text=True, tweet_mode="extended")) + pub.sendMessage("createBuffer", buffer_type="BaseBuffer", session_type=session.type, buffer_title=_("Likes for {}").format(i,), parent_tab=favs_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_favorites", name="%s-favorite" % (i,), sessionObject=session, account=name, bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, include_ext_alt_text=True, tweet_mode="extended")) pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Followers timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="followers_timelines", account=session.get_name())) followers_timelines_position =controller.view.search("followers_timelines", name) for i in session.settings["other_buffers"]["followers_timelines"]: pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_("Followers for {}").format(i,), parent_tab=followers_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_followers", name="%s-followers" % (i,), sessionObject=session, account=session.get_name(), sound="new_event.ogg", user_id=i)) - pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Following timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="friends_timelines", account=session.db["user_name"])) + pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Following timelines"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="friends_timelines", account=name)) friends_timelines_position =controller.view.search("friends_timelines", name) for i in session.settings["other_buffers"]["friends_timelines"]: pub.sendMessage("createBuffer", buffer_type="PeopleBuffer", session_type=session.type, buffer_title=_(u"Friends for {}").format(i,), parent_tab=friends_timelines_position, start=False, kwargs=dict(parent=controller.view.nb, function="get_friends", name="%s-friends" % (i,), sessionObject=session, account=session.get_name(), sound="new_event.ogg", user_id=i)) - pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Lists"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="lists", account=session.db["user_name"])) + pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Lists"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="lists", account=name)) lists_position =controller.view.search("lists", name) for i in session.settings["other_buffers"]["lists"]: pub.sendMessage("createBuffer", buffer_type="ListBuffer", session_type=session.type, buffer_title=_(u"List for {}").format(i), parent_tab=lists_position, start=False, kwargs=dict(parent=controller.view.nb, function="list_timeline", name="%s-list" % (i,), sessionObject=session, account=session.get_name(), bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), include_ext_alt_text=True, tweet_mode="extended")) - pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Searches"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="searches", account=session.db["user_name"])) + pub.sendMessage("createBuffer", buffer_type="EmptyBuffer", session_type="base", buffer_title=_("Searches"), parent_tab=root_position, start=False, kwargs=dict(parent=controller.view.nb, name="searches", account=name)) searches_position =controller.view.search("searches", name) for i in session.settings["other_buffers"]["tweet_searches"]: pub.sendMessage("createBuffer", buffer_type="SearchBuffer", session_type=session.type, buffer_title=_(u"Search for {}").format(i), parent_tab=searches_position, start=False, kwargs=dict(parent=controller.view.nb, function="search_tweets", name="%s-searchterm" % (i,), sessionObject=session, account=session.get_name(), bufferType="searchPanel", sound="search_updated.ogg", q=i, include_ext_alt_text=True, tweet_mode="extended")) diff --git a/src/sessions/mastodon/session.py b/src/sessions/mastodon/session.py index 7cde2f93..221c98cc 100644 --- a/src/sessions/mastodon/session.py +++ b/src/sessions/mastodon/session.py @@ -169,4 +169,4 @@ class Session(base.baseSession): instance = self.settings["mastodon"]["instance"] instance = instance.replace("https://", "") user = self.db["user_name"] - return "{}@{} (mastodon)".format(user, instance) \ No newline at end of file + return "Mastodon: {}@{}".format(user, instance) \ No newline at end of file diff --git a/src/sessions/twitter/session.py b/src/sessions/twitter/session.py index 320ef28f..3898addc 100644 --- a/src/sessions/twitter/session.py +++ b/src/sessions/twitter/session.py @@ -106,10 +106,8 @@ class Session(base.baseSession): else: objects.insert(0, i) incoming = incoming+1 self.db["direct_messages"] = objects - self.db["sent_direct_messages"] = sent_objects - pub.sendMessage("sent-dms-updated", total=sent, account=self.db["user_name"]) - + pub.sendMessage("sent-dms-updated", total=sent, session_name=self.get_name()) return incoming def __init__(self, *args, **kwargs): @@ -555,7 +553,7 @@ class Session(base.baseSession): def start_streaming(self): if config.app["app-settings"]["no_streaming"]: return - self.stream = streaming.Stream(twitter_api=self.twitter, user=self.get_name(), user_id=self.db["user_id"], muted_users=self.db["muted_users"], consumer_key=appkeys.twitter_api_key, consumer_secret=appkeys.twitter_api_secret, access_token=self.settings["twitter"]["user_key"], access_token_secret=self.settings["twitter"]["user_secret"], chunk_size=1025) + self.stream = streaming.Stream(twitter_api=self.twitter, session_name=self.get_name(), user_id=self.db["user_id"], muted_users=self.db["muted_users"], consumer_key=appkeys.twitter_api_key, consumer_secret=appkeys.twitter_api_secret, access_token=self.settings["twitter"]["user_key"], access_token_secret=self.settings["twitter"]["user_secret"], chunk_size=1025) self.stream_thread = call_threaded(self.stream.filter, follow=self.stream.users, stall_warnings=True) def stop_streaming(self): @@ -565,12 +563,12 @@ class Session(base.baseSession): self.stream.running = False log.debug("Stream stopped for accounr {}".format(self.db["user_name"])) - def handle_new_status(self, status, user): + def handle_new_status(self, status, session_name): """ Handles a new status present in the Streaming API. """ if self.logged == False: return # Discard processing the status if the streaming sends a tweet for another account. - if self.get_name() != user: + if self.get_name() != session_name: return # the Streaming API sends non-extended tweets with an optional parameter "extended_tweets" which contains full_text and other data. # so we have to make sure we check it before processing the normal status. @@ -607,7 +605,7 @@ class Session(base.baseSession): status = self.check_quoted_status(status) status = self.check_long_tweet(status) # Send it to the main controller object. - pub.sendMessage("newTweet", data=status, user=self.get_name(), _buffers=buffers_to_send) + pub.sendMessage("newTweet", data=status, session_name=self.get_name(), _buffers=buffers_to_send) def check_streams(self): if config.app["app-settings"]["no_streaming"]: @@ -618,11 +616,11 @@ class Session(base.baseSession): if self.stream.running == False: self.start_streaming() - def handle_connected(self, user): + def handle_connected(self, session_name): if self.logged == False: return - if user != self.get_name(): - log.debug("Connected streaming endpoint on account {}".format(user)) + if session_name != self.get_name(): + log.debug("Connected streaming endpoint on session {}".format(session_name)) def send_tweet(self, *tweets): """ Convenience function to send a thread. """ @@ -673,7 +671,7 @@ class Session(base.baseSession): else: sent_dms.insert(0, item) self.db["sent_direct_messages"] = sent_dms - pub.sendMessage("sent-dm", data=item, user=self.db["user_name"]) + pub.sendMessage("sent-dm", data=item, session_name=self.get_name()) def get_name(self): - return "{} ({})".format(self.db["user_name"], "Twitter") \ No newline at end of file + return "Twitter: {}".format(self.db["user_name"]) \ No newline at end of file diff --git a/src/sessions/twitter/streaming.py b/src/sessions/twitter/streaming.py index 137147fc..d904429b 100644 --- a/src/sessions/twitter/streaming.py +++ b/src/sessions/twitter/streaming.py @@ -14,13 +14,13 @@ log = logging.getLogger("sessions.twitter.streaming") class Stream(tweepy.Stream): - def __init__(self, twitter_api, user, user_id, muted_users=[], *args, **kwargs): + def __init__(self, twitter_api, session_name, user_id, muted_users=[], *args, **kwargs): super(Stream, self).__init__(*args, **kwargs) - log.debug("Starting streaming listener for account {}".format(user)) + log.debug("Starting streaming listener for session {}".format(session_name)) self.started = False self.users = [] self.api = twitter_api - self.user = user + self.session_name = session_name self.user_id = user_id friends = self.api.get_friend_ids() log.debug("Retrieved {} friends to add to the streaming listener.".format(len(friends))) @@ -33,15 +33,15 @@ class Stream(tweepy.Stream): log.debug("Streaming listener started with {} users to follow.".format(len(self.users))) def on_connect(self): - pub.sendMessage("streamConnected", user=self.user) + pub.sendMessage("streamConnected", session_name=self.session_name) def on_exception(self, ex): - log.exception("Exception received on streaming endpoint for user {}".format(self.user)) + log.exception("Exception received on streaming endpoint for session {}".format(self.session_name)) def on_status(self, status): """ Checks data arriving as a tweet. """ # Hide replies to users not followed by current account. - if status.in_reply_to_user_id_str != None and status.in_reply_to_user_id_str not in self.users and status.user.screen_name != self.user: + if status.in_reply_to_user_id_str != None and status.in_reply_to_user_id_str not in self.users and status.user.id != self.user_id: return if status.user.id_str in self.users: - pub.sendMessage("newStatus", status=status, user=self.user) + pub.sendMessage("newStatus", status=status, session_name=self.session_name)