From 8fe2f4c64df2f1098af6893fe0608f8e3d7412f0 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Sun, 4 Jul 2021 09:15:04 -0500 Subject: [PATCH] Exclude muted users from Streaming API --- src/sessions/twitter/session.py | 2 +- src/sessions/twitter/streaming.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/sessions/twitter/session.py b/src/sessions/twitter/session.py index c23d411c..35d7a495 100644 --- a/src/sessions/twitter/session.py +++ b/src/sessions/twitter/session.py @@ -504,7 +504,7 @@ class Session(base.baseSession): self.db["users"] = users def start_streaming(self): - self.stream_listener = streaming.StreamListener(twitter_api=self.twitter, user=self.db["user_name"], user_id=self.db["user_id"]) + self.stream_listener = streaming.StreamListener(twitter_api=self.twitter, user=self.db["user_name"], user_id=self.db["user_id"], muted_users=self.db["muted_users"]) self.stream = streaming.Stream(auth = self.auth, listener=self.stream_listener, chunk_size=1025) self.stream_thread = call_threaded(self.stream.filter, follow=self.stream_listener.users, stall_warnings=True) diff --git a/src/sessions/twitter/streaming.py b/src/sessions/twitter/streaming.py index 29cfe052..dcd5711d 100644 --- a/src/sessions/twitter/streaming.py +++ b/src/sessions/twitter/streaming.py @@ -14,14 +14,23 @@ log = logging.getLogger("sessions.twitter.streaming") class StreamListener(tweepy.StreamListener): - def __init__(self, twitter_api, user, user_id, *args, **kwargs): + def __init__(self, twitter_api, user, user_id, muted_users=[], *args, **kwargs): super(StreamListener, self).__init__(*args, **kwargs) + log.debug("Starting streaming listener for account {}".format(user)) + self.started = False + self.users = [] self.api = twitter_api self.user = user self.user_id = user_id - self.users = [str(id) for id in self.api.friends_ids()] + friends = self.api.friends_ids() + log.debug("Retrieved {} friends to add to the streaming listener.".format(len(friends))) self.users.append(str(self.user_id)) - log.debug("Started streaming object for user {}".format(self.user)) + log.debug("Got {} muted users.".format(len(muted_users))) + for user in friends: + if user not in muted_users: + self.users.append(str(user)) + self.started = True + log.debug("Streaming listener started with {} users to follow.".format(len(self.users))) def on_connect(self): pub.sendMessage("streamConnected", user=self.user)