Parse correctly incoming tweets from Streaming API

This commit is contained in:
Manuel Cortez 2021-06-29 05:05:20 -05:00
parent 168c7e7a5d
commit bb5ead80de

View File

@ -509,15 +509,27 @@ class Session(base.baseSession):
call_threaded(self.stream.filter, follow=self.stream_listener.users) call_threaded(self.stream.filter, follow=self.stream_listener.users)
def handle_new_status(self, status, user): def handle_new_status(self, status, user):
""" Handles a new status present in the Streaming API. """
# Discard processing the status if the streaming sends a tweet for another account.
if self.db["user_name"] != user: if self.db["user_name"] != user:
return return
if hasattr(status, "retweeted_status") and status.retweeted_status.truncated: # the Streaming API sends non-extended tweets with an optional parameter "extended_tweets" which contains full_text and other data.
status.retweeted_status._json["full_text"] = status.retweeted_status.extended_tweet["full_text"] # so we have to make sure we check it before processing the normal status.
if hasattr(status, "quoted_status") and status.quoted_status.truncated: # As usual, we handle also quotes and retweets at first.
status.quoted_status._json["full_text"] = status.quoted_status.extended_tweet["full_text"] if hasattr(status, "retweeted_status") and hasattr(status.retweeted_status, "extended_tweet"):
status.retweeted_status._json = {**status.retweeted_status._json, **status.retweeted_status._json["extended_tweet"]}
# compose.compose_tweet requires the parent tweet to have a full_text field, so we have to add it to retweets here.
status._json["full_text"] = status._json["text"]
if hasattr(status, "quoted_status") and hasattr(status.quoted_status, "extended_tweet"):
status.quoted_status._json = {**status.quoted_status._json, **status.quoted_status._json["extended_tweet"]}
if status.truncated: if status.truncated:
status._json["full_text"] = status.extended_tweet["full_text"] status._json = {**status._json, **status._json["extended_tweet"]}
# Sends status to database, where it will be reduced and changed according to our needs.
num = self.order_buffer("home_timeline", [status]) num = self.order_buffer("home_timeline", [status])
if num == 1: if num == 1:
# However, we have to do the "reduce and change" process here because the status we sent to the db is going to be a different object that the one sent to database.
status = reduce.reduce_tweet(status) status = reduce.reduce_tweet(status)
status = self.check_quoted_status(status)
status = self.check_long_tweet(status)
# Send it to the main controller object.
pub.sendMessage("tweet-in-home", data=status, user=self.db["user_name"]) pub.sendMessage("tweet-in-home", data=status, user=self.db["user_name"])