diff --git a/src/sessions/twitter/session.py b/src/sessions/twitter/session.py index ce02c888..f74b8837 100644 --- a/src/sessions/twitter/session.py +++ b/src/sessions/twitter/session.py @@ -509,15 +509,27 @@ class Session(base.baseSession): call_threaded(self.stream.filter, follow=self.stream_listener.users) 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: return - if hasattr(status, "retweeted_status") and status.retweeted_status.truncated: - status.retweeted_status._json["full_text"] = status.retweeted_status.extended_tweet["full_text"] - if hasattr(status, "quoted_status") and status.quoted_status.truncated: - status.quoted_status._json["full_text"] = status.quoted_status.extended_tweet["full_text"] + # 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. + # As usual, we handle also quotes and retweets at first. + 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: - 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]) 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 = 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"]) \ No newline at end of file