Rework sessions to be handled with unique names

This commit is contained in:
2022-11-15 11:54:59 -06:00
parent 862dbd0b8a
commit 2e8c8c6db4
5 changed files with 61 additions and 55 deletions

View File

@@ -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)
return "Mastodon: {}@{}".format(user, instance)

View File

@@ -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")
return "Twitter: {}".format(self.db["user_name"])

View File

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