Initial test for supporting a subset of the Streaming API

This commit is contained in:
Manuel Cortez 2021-06-28 17:03:26 -05:00
parent a7838bbf7d
commit 168c7e7a5d
3 changed files with 48 additions and 3 deletions

View File

@ -126,6 +126,7 @@ class Controller(object):
pub.subscribe(self.update_sent_dms, "sent-dms-updated")
pub.subscribe(self.more_dms, "more-sent-dms")
pub.subscribe(self.manage_sent_tweets, "sent-tweet")
pub.subscribe(self.manage_tweet_in_home, "tweet-in-home")
pub.subscribe(self.manage_friend, "friend")
pub.subscribe(self.manage_unfollowing, "unfollowing")
pub.subscribe(self.manage_favourite, "favourite")
@ -266,6 +267,7 @@ class Controller(object):
if sessions.sessions[i].is_logged == False: continue
self.start_buffers(sessions.sessions[i])
self.set_buffer_positions(sessions.sessions[i])
sessions.sessions[i].start_streaming()
if config.app["app-settings"]["play_ready_sound"] == True:
sessions.sessions[list(sessions.sessions.keys())[0]].sound.play("ready.ogg")
if config.app["app-settings"]["speak_ready_msg"] == True:
@ -1269,8 +1271,6 @@ class Controller(object):
def manage_sent_tweets(self, data, user):
buffer = self.search_buffer("sent_tweets", user)
if buffer == None: return
# if "sent_tweets" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
# self.notify(buffer.session, play_sound=play_sound)
data = buffer.session.check_quoted_status(data)
data = buffer.session.check_long_tweet(data)
if data == False: # Long tweet deleted from twishort.
@ -1627,3 +1627,10 @@ class Controller(object):
def save_data_in_db(self):
for i in sessions.sessions:
sessions.sessions[i].save_persistent_data()
def manage_tweet_in_home(self, data, user):
buffer = self.search_buffer("home_timeline", user)
if buffer == None or buffer.session.db["user_name"] != user: return
buffer.add_new_item(data)
if "home_timeline" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, "tweet_received.ogg")

View File

@ -17,7 +17,7 @@ from keys import keyring
from sessions import base
from sessions.twitter import utils, compose
from sessions.twitter.long_tweets import tweets, twishort
from . import reduce
from . import reduce, streaming
from .wxUI import authorisationDialog
log = logging.getLogger("sessions.twitterSession")
@ -125,6 +125,7 @@ class Session(base.baseSession):
# This will be especially useful because if the user reactivates their account later, TWblue will try to retrieve such user again at startup.
# If we wouldn't implement this approach, TWBlue would save permanently the "deleted user" object.
self.deleted_users = {}
pub.subscribe(self.handle_new_status, "newStatus")
# @_require_configuration
def login(self, verify_credentials=True):
@ -501,3 +502,22 @@ class Session(base.baseSession):
if hasattr(i, "retweeted_status") and (i.retweeted_status.user.id_str in self.db["users"]) == False:
users[i.retweeted_status.user.id_str] = i.retweeted_status.user
self.db["users"] = users
def start_streaming(self):
self.stream_listener = streaming.StreamListener(twitter_api=self.twitter, user=self.db["user_name"])
self.stream = tweepy.Stream(auth = self.auth, listener=self.stream_listener)
call_threaded(self.stream.filter, follow=self.stream_listener.users)
def handle_new_status(self, status, user):
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"]
if status.truncated:
status._json["full_text"] = status.extended_tweet["full_text"]
num = self.order_buffer("home_timeline", [status])
if num == 1:
status = reduce.reduce_tweet(status)
pub.sendMessage("tweet-in-home", data=status, user=self.db["user_name"])

View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
import tweepy
from pubsub import pub
class StreamListener(tweepy.StreamListener):
def __init__(self, twitter_api, user, *args, **kwargs):
super(StreamListener, self).__init__(*args, **kwargs)
self.api = twitter_api
self.user = user
self.users = [str(id) for id in self.api.friends_ids()]
def on_status(self, status):
""" Checks data arriving as a tweet. """
if status.user.id_str in self.users:
pub.sendMessage("newStatus", status=status, user=self.user)
# print(status.text)