Deleted code related to Streaming Features

This commit is contained in:
Manuel Cortez 2018-08-16 10:42:14 -05:00
parent 41d4c97067
commit bfad5b82f0
8 changed files with 10 additions and 493 deletions

View File

@ -19,17 +19,3 @@ translators = [u"Manuel Cortéz (English)", u"Mohammed Al Shara, Hatoun Felemban
url = u"https://twblue.es" url = u"https://twblue.es"
report_bugs_url = "https://github.com/manuelcortez/twblue/issues" report_bugs_url = "https://github.com/manuelcortez/twblue/issues"
supported_languages = [] supported_languages = []
def streaming_lives():
""" Check if we are in August 16.
ToDo: This method should be removed after deadline==True"""
# Let's import config here so we will avoid breaking things when setup.py is going to be used.
# Check if user has disabled the streaming API things from settings.
import config
if config.app != None:
no_streaming = config.app["app-settings"]["no_streaming"]
if no_streaming == True:
return False
deadline = datetime.date(2018, 8, 16)
now = datetime.datetime.now().date()
return deadline>now

View File

@ -108,27 +108,6 @@ class Controller(object):
[results.append(self.search_buffer(i.name, i.account)) for i in buffers if i.account == account and (i.type != "account")] [results.append(self.search_buffer(i.name, i.account)) for i in buffers if i.account == account and (i.type != "account")]
return results return results
def bind_stream_events(self):
""" Binds all the streaming events with their functions."""
log.debug("Binding events for the Twitter stream API...")
pub.subscribe(self.manage_home_timelines, "item-in-home")
pub.subscribe(self.manage_mentions, "mention")
pub.subscribe(self.manage_direct_messages, "direct-message")
pub.subscribe(self.manage_sent_tweets, "sent-tweet")
pub.subscribe(self.manage_events, "event")
pub.subscribe(self.manage_followers, "follower")
pub.subscribe(self.manage_friend, "friend")
pub.subscribe(self.manage_unfollowing, "unfollowing")
pub.subscribe(self.manage_favourite, "favourite")
pub.subscribe(self.manage_unfavourite, "unfavourite")
pub.subscribe(self.manage_blocked_user, "blocked-user")
pub.subscribe(self.manage_unblocked_user, "unblocked-user")
pub.subscribe(self.manage_item_in_timeline, "item-in-timeline")
pub.subscribe(self.manage_item_in_list, "item-in-list")
pub.subscribe(self.on_tweet_deleted, "tweet-deleted")
pub.subscribe(self.manage_stream_errors, "stream-error")
# pub.subscribe(self.restart_streams, "restart-streams")
def bind_other_events(self): def bind_other_events(self):
""" Binds the local application events with their functions.""" """ Binds the local application events with their functions."""
log.debug("Binding other application events...") log.debug("Binding other application events...")
@ -142,6 +121,13 @@ class Controller(object):
pub.subscribe(self.search_topic, "search") pub.subscribe(self.search_topic, "search")
pub.subscribe(self.update_sent_dms, "sent-dms-updated") pub.subscribe(self.update_sent_dms, "sent-dms-updated")
pub.subscribe(self.more_dms, "more-sent-dms") pub.subscribe(self.more_dms, "more-sent-dms")
pub.subscribe(self.manage_sent_tweets, "sent-tweet")
pub.subscribe(self.manage_friend, "friend")
pub.subscribe(self.manage_unfollowing, "unfollowing")
pub.subscribe(self.manage_favourite, "favourite")
pub.subscribe(self.manage_unfavourite, "unfavourite")
pub.subscribe(self.manage_blocked_user, "blocked-user")
pub.subscribe(self.manage_unblocked_user, "unblocked-user")
if system == "Windows": if system == "Windows":
pub.subscribe(self.invisible_shorcuts_changed, "invisible-shorcuts-changed") pub.subscribe(self.invisible_shorcuts_changed, "invisible-shorcuts-changed")
widgetUtils.connect_event(self.view, widgetUtils.MENU, self.show_hide, menuitem=self.view.show_hide) widgetUtils.connect_event(self.view, widgetUtils.MENU, self.show_hide, menuitem=self.view.show_hide)
@ -234,8 +220,6 @@ class Controller(object):
# This saves the current account (important in invisible mode) # This saves the current account (important in invisible mode)
self.current_account = "" self.current_account = ""
self.view.prepare() self.view.prepare()
if application.streaming_lives():
self.bind_stream_events()
self.bind_other_events() self.bind_other_events()
if system == "Windows": if system == "Windows":
self.set_systray_icon() self.set_systray_icon()
@ -350,10 +334,6 @@ class Controller(object):
muted = buffersController.peopleBufferController(self.view.nb, "list_mutes", "muted", session, session.db["user_name"]) muted = buffersController.peopleBufferController(self.view.nb, "list_mutes", "muted", session, session.db["user_name"])
self.buffers.append(muted) self.buffers.append(muted)
self.view.insert_buffer(muted.buffer, name=_(u"Muted users"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) self.view.insert_buffer(muted.buffer, name=_(u"Muted users"), pos=self.view.search(session.db["user_name"], session.db["user_name"]))
elif i == 'events' and application.streaming_lives():
events = buffersController.eventsBufferController(self.view.nb, "events", session, session.db["user_name"], bufferType="dmPanel", screen_name=session.db["user_name"])
self.buffers.append(events)
self.view.insert_buffer(events.buffer, name=_(u"Events"), pos=self.view.search(session.db["user_name"], session.db["user_name"]))
timelines = buffersController.emptyPanel(self.view.nb, "timelines", session.db["user_name"]) timelines = buffersController.emptyPanel(self.view.nb, "timelines", session.db["user_name"])
self.buffers.append(timelines) self.buffers.append(timelines)
self.view.insert_buffer(timelines.buffer , name=_(u"Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) self.view.insert_buffer(timelines.buffer , name=_(u"Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"]))
@ -578,7 +558,6 @@ class Controller(object):
if listBuffer != None: listBuffer.get_user_ids() if listBuffer != None: listBuffer.get_user_ids()
buff.session.db["lists"].pop(older_list) buff.session.db["lists"].pop(older_list)
buff.session.db["lists"].append(list) buff.session.db["lists"].append(list)
if listBuffer != None: pub.sendMessage("restart-streams", streams=["timelinesStream"], session=buff.session)
except TwythonError as e: except TwythonError as e:
output.speak("error %s: %s" % (e.error_code, e.msg)) output.speak("error %s: %s" % (e.error_code, e.msg))
@ -607,7 +586,6 @@ class Controller(object):
if listBuffer != None: listBuffer.get_user_ids() if listBuffer != None: listBuffer.get_user_ids()
buff.session.db["lists"].pop(older_list) buff.session.db["lists"].pop(older_list)
buff.session.db["lists"].append(list) buff.session.db["lists"].append(list)
if listBuffer != None: pub.sendMessage("restart-streams", streams=["timelinesStream"], session=buff.session)
except TwythonError as e: except TwythonError as e:
output.speak("error %s: %s" % (e.error_code, e.msg)) output.speak("error %s: %s" % (e.error_code, e.msg))
@ -667,8 +645,6 @@ class Controller(object):
for item in session_.sessions: for item in session_.sessions:
if session_.sessions[item].logged == False: continue if session_.sessions[item].logged == False: continue
log.debug("Disconnecting streams for %s session" % (session_.sessions[item].session_id,)) log.debug("Disconnecting streams for %s session" % (session_.sessions[item].session_id,))
if hasattr(session_.sessions[item], "main_stream"): session_.sessions[item].main_stream.disconnect()
if hasattr(session_.sessions[item], "timelinesStream"): session_.sessions[item].timelinesStream.disconnect()
session_.sessions[item].sound.cleaner.cancel() session_.sessions[item].sound.cleaner.cancel()
log.debug("Shelving database for " + session_.sessions[item].session_id) log.debug("Shelving database for " + session_.sessions[item].session_id)
session_.sessions[item].shelve() session_.sessions[item].shelve()
@ -848,9 +824,6 @@ class Controller(object):
if usr["following"] == False: if usr["following"] == False:
commonMessageDialogs.no_following() commonMessageDialogs.no_following()
return return
if application.streaming_lives():
answer = commonMessageDialogs.protected_user()
if answer == widgetUtils.NO: return
tl_type = dlg.get_action() tl_type = dlg.get_action()
if tl_type == "tweets": if tl_type == "tweets":
if usr["statuses_count"] == 0: if usr["statuses_count"] == 0:
@ -870,7 +843,6 @@ class Controller(object):
self.view.insert_buffer(tl.buffer, name=_(u"Timeline for {}").format(dlg.get_user()), pos=pos) self.view.insert_buffer(tl.buffer, name=_(u"Timeline for {}").format(dlg.get_user()), pos=pos)
buff.session.settings["other_buffers"]["timelines"].append(usr["id_str"]) buff.session.settings["other_buffers"]["timelines"].append(usr["id_str"])
pub.sendMessage("buffer-title-changed", buffer=tl) pub.sendMessage("buffer-title-changed", buffer=tl)
pub.sendMessage("restart-streams", streams=["timelinesStream"], session=buff.session)
buff.session.sound.play("create_timeline.ogg") buff.session.sound.play("create_timeline.ogg")
elif tl_type == "favourites": elif tl_type == "favourites":
if usr["favourites_count"] == 0: if usr["favourites_count"] == 0:
@ -941,7 +913,6 @@ class Controller(object):
search.tweet = buffer.get_right_tweet() search.tweet = buffer.get_right_tweet()
search.start_stream(start=True) search.start_stream(start=True)
pos=self.view.search("searches", buffer.session.db["user_name"]) pos=self.view.search("searches", buffer.session.db["user_name"])
# self.buffers.append(search)
self.insert_buffer(search, pos) self.insert_buffer(search, pos)
self.view.insert_buffer(search.buffer, name=_(u"Conversation with {0}").format(user), pos=pos) self.view.insert_buffer(search.buffer, name=_(u"Conversation with {0}").format(user), pos=pos)
@ -1270,32 +1241,6 @@ class Controller(object):
if message != None: if message != None:
output.speak(message, speech=session.settings["reporting"]["speech_reporting"], braille=session.settings["reporting"]["braille_reporting"]) output.speak(message, speech=session.settings["reporting"]["speech_reporting"], braille=session.settings["reporting"]["braille_reporting"])
def manage_home_timelines(self, data, user):
buffer = self.search_buffer("home_timeline", user)
if buffer == None: return
play_sound = "tweet_received.ogg"
if "home_timeline" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound)
buffer.add_new_item(data)
def manage_mentions(self, data, user):
buffer = self.search_buffer("mentions", user)
if buffer == None: return
play_sound = "mention_received.ogg"
message = _(u"One mention from %s ") % (data["user"]["name"])
if "mentions" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound, message=message)
buffer.add_new_item(data)
def manage_direct_messages(self, data, user):
buffer = self.search_buffer("direct_messages", user)
if buffer == None: return
play_sound = "dm_received.ogg"
message = _(u"New direct message")
if "direct_messages" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound, message=message)
buffer.add_new_item(data)
def manage_sent_dm(self, data, user): def manage_sent_dm(self, data, user):
buffer = self.search_buffer("sent_direct_messages", user) buffer = self.search_buffer("sent_direct_messages", user)
if buffer == None: return if buffer == None: return
@ -1312,33 +1257,15 @@ class Controller(object):
self.notify(buffer.session, play_sound=play_sound) self.notify(buffer.session, play_sound=play_sound)
buffer.add_new_item(data) buffer.add_new_item(data)
def manage_events(self, data, user):
buffer = self.search_buffer("events", user)
if buffer == None: return
play_sound = "new_event.ogg"
if "events" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound)
buffer.add_new_item(data)
def manage_followers(self, data, user):
buffer = self.search_buffer("followers", user)
if buffer == None: return
play_sound = "update_followers.ogg"
if "followers" not in buffer.session.settings["other_buffers"]["muted_buffers"]:
self.notify(buffer.session, play_sound=play_sound)
buffer.add_new_item(data)
def manage_friend(self, data, user): def manage_friend(self, data, user):
buffer = self.search_buffer("friends", user) buffer = self.search_buffer("friends", user)
if buffer == None: return if buffer == None: return
buffer.add_new_item(data) buffer.add_new_item(data)
pub.sendMessage("restart-streams", streams=["main_stream"], session=buffer.session)
def manage_unfollowing(self, item, user): def manage_unfollowing(self, item, user):
buffer = self.search_buffer("friends", user) buffer = self.search_buffer("friends", user)
if buffer == None: return if buffer == None: return
buffer.remove_item(item) buffer.remove_item(item)
pub.sendMessage("restart-streams", streams=["main_stream"], session=buffer.session)
def manage_favourite(self, data, user): def manage_favourite(self, data, user):
buffer = self.search_buffer("favourites", user) buffer = self.search_buffer("favourites", user)
@ -1357,29 +1284,11 @@ class Controller(object):
buffer = self.search_buffer("blocked", user) buffer = self.search_buffer("blocked", user)
if buffer == None: return if buffer == None: return
buffer.add_new_item(data) buffer.add_new_item(data)
pub.sendMessage("restart-streams", streams=["main_stream"], session=buffer.session)
def manage_unblocked_user(self, item, user): def manage_unblocked_user(self, item, user):
buffer = self.search_buffer("blocked", user) buffer = self.search_buffer("blocked", user)
if buffer == None: return if buffer == None: return
buffer.remove_item(item) buffer.remove_item(item)
pub.sendMessage("restart-streams", streams=["main_stream"], session=buffer.session)
def manage_item_in_timeline(self, data, user, who):
buffer = self.search_buffer("%s-timeline" % (who,), user)
if buffer == None: return
play_sound = "tweet_timeline.ogg"
if "%s-timeline" % (who,) not in buffer.session.settings["other_buffers"]["muted_buffers"] and buffer.session.settings["sound"]["session_mute"] == False:
self.notify(buffer.session, play_sound=play_sound, message=_(u"One tweet from %s") % (data["user"]["name"]))
buffer.add_new_item(data)
def manage_item_in_list(self, data, user, where):
buffer = self.search_buffer("%s" % (where,), user)
if buffer == None: return
play_sound = "list_tweet.ogg"
if "%s" % (where,) not in buffer.session.settings["other_buffers"]["muted_buffers"] and buffer.session.settings["sound"]["session_mute"] == False:
self.notify(buffer.session, play_sound=play_sound, message=_(u"One tweet from %s") % (data["user"]["name"]))
buffer.add_new_item(data)
def start_buffers(self, session): def start_buffers(self, session):
log.debug("starting buffers... Session %s" % (session.session_id,)) log.debug("starting buffers... Session %s" % (session.session_id,))
@ -1406,30 +1315,11 @@ class Controller(object):
continue continue
if change_title: if change_title:
pub.sendMessage("buffer-title-changed", buffer=i) pub.sendMessage("buffer-title-changed", buffer=i)
if application.streaming_lives():
log.debug("Starting the streaming endpoint")
session.start_streaming()
def set_positions(self): def set_positions(self):
for i in session_.sessions: for i in session_.sessions:
self.set_buffer_positions(i) self.set_buffer_positions(i)
def manage_stream_errors(self, session):
log.error(" Restarting %s session streams. It will be destroyed" % (session,))
s = session_.sessions[session]
try:
if hasattr(s, "main_stream"):
s.main_stream.disconnect()
log.error("main stream disconnected")
del s.main_stream
if hasattr(s, "timelinesStream"):
s.timelinesStream.disconnect()
del s.timelinesStream
except AttributeError:
pass
s.counter = 0
# s.listen_stream_error()
def check_connection(self): def check_connection(self):
if self.started == False: if self.started == False:
return return
@ -1486,13 +1376,6 @@ class Controller(object):
tl.start_stream(play_sound=False) tl.start_stream(play_sound=False)
buff.session.settings["other_buffers"]["lists"].append(create) buff.session.settings["other_buffers"]["lists"].append(create)
buff.session.settings.write() buff.session.settings.write()
pub.sendMessage("restart-streams", streams=["timelinesStream"], session=buff.session)
def restart_streams(self, streams=[], session=None):
for i in streams:
log.debug("Restarting the %s stream" % (i,))
session.remove_stream(i)
session.check_connection()
def invisible_shorcuts_changed(self, registered): def invisible_shorcuts_changed(self, registered):
if registered == True: if registered == True:
@ -1618,12 +1501,6 @@ class Controller(object):
if n != None: if n != None:
output.speak(_(u"{0} items retrieved").format(n,)) output.speak(_(u"{0} items retrieved").format(n,))
def on_tweet_deleted(self, data):
id = data["delete"]["status"]["id"]
for i in self.buffers:
if hasattr(i, "remove_tweet") and hasattr(i, "name"):
i.remove_tweet(id)
def buffer_title_changed(self, buffer): def buffer_title_changed(self, buffer):
if "-timeline" in buffer.name: if "-timeline" in buffer.name:
title = _(u"Timeline for {}").format(buffer.username,) title = _(u"Timeline for {}").format(buffer.username,)

View File

@ -138,8 +138,6 @@ class accountSettingsController(globalSettingsController):
widgetUtils.connect_event(self.dialog.general.au, widgetUtils.BUTTON_PRESSED, self.manage_autocomplete) widgetUtils.connect_event(self.dialog.general.au, widgetUtils.BUTTON_PRESSED, self.manage_autocomplete)
self.dialog.set_value("general", "relative_time", self.config["general"]["relative_times"]) self.dialog.set_value("general", "relative_time", self.config["general"]["relative_times"])
self.dialog.set_value("general", "show_screen_names", self.config["general"]["show_screen_names"]) self.dialog.set_value("general", "show_screen_names", self.config["general"]["show_screen_names"])
if application.streaming_lives():
self.dialog.set_value("general", "apiCalls", self.config["general"]["max_api_calls"])
self.dialog.set_value("general", "itemsPerApiCall", self.config["general"]["max_tweets_per_call"]) self.dialog.set_value("general", "itemsPerApiCall", self.config["general"]["max_tweets_per_call"])
self.dialog.set_value("general", "reverse_timelines", self.config["general"]["reverse_timelines"]) self.dialog.set_value("general", "reverse_timelines", self.config["general"]["reverse_timelines"])
rt = self.config["general"]["retweet_mode"] rt = self.config["general"]["retweet_mode"]
@ -191,8 +189,6 @@ class accountSettingsController(globalSettingsController):
self.needs_restart = True self.needs_restart = True
self.config["general"]["relative_times"] = self.dialog.get_value("general", "relative_time") self.config["general"]["relative_times"] = self.dialog.get_value("general", "relative_time")
self.config["general"]["show_screen_names"] = self.dialog.get_value("general", "show_screen_names") self.config["general"]["show_screen_names"] = self.dialog.get_value("general", "show_screen_names")
if application.streaming_lives():
self.config["general"]["max_api_calls"] = self.dialog.get_value("general", "apiCalls")
self.config["general"]["max_tweets_per_call"] = self.dialog.get_value("general", "itemsPerApiCall") self.config["general"]["max_tweets_per_call"] = self.dialog.get_value("general", "itemsPerApiCall")
if self.config["general"]["persist_size"] != self.dialog.get_value("general", "persist_size"): if self.config["general"]["persist_size"] != self.dialog.get_value("general", "persist_size"):
if self.dialog.get_value("general", "persist_size") == '': if self.dialog.get_value("general", "persist_size") == '':
@ -293,8 +289,6 @@ class accountSettingsController(globalSettingsController):
all_buffers['friends']=_(u"Friends") all_buffers['friends']=_(u"Friends")
all_buffers['blocks']=_(u"Blocked users") all_buffers['blocks']=_(u"Blocked users")
all_buffers['muted']=_(u"Muted users") all_buffers['muted']=_(u"Muted users")
if application.streaming_lives():
all_buffers['events']=_(u"Events")
list_buffers = [] list_buffers = []
hidden_buffers=[] hidden_buffers=[]
for i in all_buffers.keys(): for i in all_buffers.keys():

View File

@ -141,7 +141,6 @@ class Session(object):
self.reconnection_function_active = False self.reconnection_function_active = False
self.counter = 0 self.counter = 0
self.lists = [] self.lists = []
pub.subscribe(self.add_friends, "friends-receibed")
@property @property
def is_logged(self): def is_logged(self):
@ -280,10 +279,7 @@ class Session(object):
update_function str: The function to call. This function must be child of self.twitter.twitter update_function str: The function to call. This function must be child of self.twitter.twitter
returns a list with all items retrieved.""" returns a list with all items retrieved."""
if application.streaming_lives(): max = 0
max = int(self.settings["general"]["max_api_calls"])-1
else:
max = 0
results = [] results = []
data = getattr(self.twitter.twitter, update_function)(count=self.settings["general"]["max_tweets_per_call"], *args, **kwargs) data = getattr(self.twitter.twitter, update_function)(count=self.settings["general"]["max_tweets_per_call"], *args, **kwargs)
results.extend(data) results.extend(data)
@ -376,51 +372,6 @@ class Session(object):
self.db[name]["cursor"] = tl["next_cursor"] self.db[name]["cursor"] = tl["next_cursor"]
return num return num
def start_streaming(self):
""" Start the streaming for sending tweets in realtime."""
if application.streaming_lives():
if not hasattr(self, "main_stream"):
self.get_timelines()
if not hasattr(self, "timelinesStream"):
self.get_main_stream()
def get_main_stream(self):
if application.streaming_lives():
log.debug("Starting the main stream...")
self.main_stream = twitter.buffers.stream.streamer(keyring.get("api_key"), keyring.get("api_secret"), self.settings["twitter"]["user_key"], self.settings["twitter"]["user_secret"], self)
stream_threaded(self.main_stream.user, self.session_id)
def get_timelines(self):
if application.streaming_lives():
log.debug("Starting the timelines stream...")
self.timelinesStream = twitter.buffers.indibidual.timelinesStreamer(keyring.get("api_key"), keyring.get("api_secret"), self.settings["twitter"]["user_key"], self.settings["twitter"]["user_secret"], session=self)
ids = ""
for i in self.settings["other_buffers"]["timelines"]:
ids = ids + "%s, " % (self.db[i+"-timeline"][0]["user"]["id_str"])
for i in self.lists:
for z in i.users:
ids += str(z) + ", "
if ids != "":
stream_threaded(self.timelinesStream.statuses.filter, self.session_id, follow=ids)
def add_friends(self):
if application.streaming_lives():
try:
self.timelinesStream.set_friends(self.main_stream.friends)
except AttributeError:
pass
def listen_stream_error(self):
if hasattr(self, "main_stream") and application.streaming_lives():
log.debug("Disconnecting the main stream...")
self.main_stream.disconnect()
del self.main_stream
if hasattr(self, "timelinesStream") and application.streaming_lives():
log.debug("disconnecting the timelines stream...")
self.timelinesStream.disconnect()
del self.timelinesStream
def check_connection(self): def check_connection(self):
log.debug("Executing check connection...") log.debug("Executing check connection...")
instan = 0 instan = 0
@ -434,27 +385,7 @@ class Session(object):
self.counter = 0 self.counter = 0
if self.reconnection_function_active == True: return if self.reconnection_function_active == True: return
self.reconnection_function_active = True self.reconnection_function_active = True
if not hasattr(self, "main_stream") and application.streaming_lives():
self.get_main_stream()
if not hasattr(self, "timelinesStream") and application.streaming_lives():
self.get_timelines()
self.reconnection_function_active = False self.reconnection_function_active = False
if hasattr(self, "timelinesStream") and not hasattr(self.timelinesStream, "friends"):
self.add_friends()
# try:
# urllib2.urlopen("http://74.125.228.231", timeout=5)
# except urllib2.URLError:
# pub.sendMessage("stream-error", session=self.session_id)
def remove_stream(self, stream):
if application.streaming_lives():
if stream == "timelinesStream":
if hasattr(self, "timelinesStream"):
self.timelinesStream.disconnect()
del self.timelinesStream
else:
self.main_stream.disconnect()
del self.main_stream
def shelve(self): def shelve(self):
"Shelve the database to allow for persistance." "Shelve the database to allow for persistance."

View File

@ -1 +1 @@
import buffers, utils, compose, twitter import utils, compose, twitter

View File

@ -1 +0,0 @@
import stream, indibidual

View File

@ -1,77 +0,0 @@
# -*- coding: utf-8 -*-
import config
from requests.auth import HTTPProxyAuth
from twitter import compose, utils
from twython import TwythonStreamer
from pubsub import pub
import logging as original_logger
log = original_logger.getLogger("TimelinesStream")
class timelinesStreamer(TwythonStreamer):
def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret, timeout=300, retry_count=None, retry_in=10, client_args=None, handlers=None, chunk_size=1, session=None):
self.session = session
super(timelinesStreamer, self).__init__(app_key, app_secret, oauth_token, oauth_token_secret, timeout=60, retry_count=None, retry_in=180, handlers=None, chunk_size=1)
self.lists = self.session.lists
def on_error(self, status_code, data):
log.error("error in stream: %s: %s" % (status_code, data))
# pub.sendMessage("stream-error", session=self.session.session_id)
def on_timeout(self, *args, **kwargs):
log.error("Twitter timeout Error")
# pub.sendMessage("stream-error", session=self.session.session_id)
def check_tls(self, data):
for i in self.session.settings["other_buffers"]["timelines"]:
if data["user"]["id_str"] == i:
if utils.find_item(data["id"], self.session.db["%s-timeline" % (i,)]) != None:
log.error("duplicated tweet. Ignoring it...")
return
# try:
if utils.is_allowed(data, self.session.settings, "%s-timeline" % (i,)):
data_ = self.session.check_quoted_status(data)
data_ = self.session.check_long_tweet(data_)
data = data_
# except ValueError:
# pass
if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s-timeline" % (i,)].append(data)
else: self.session.db["%s-timeline" % (i,)].insert(0, data)
pub.sendMessage("item-in-timeline", data= data, user= self.session.db["user_name"], who= i)
return
for i in self.session.lists:
try:
i.users.index(data["user"]["id"])
usr = data["in_reply_to_user_id"]
if (usr != None and usr in self.friends) or data.has_key("retweeted_status"):
data = self.session.check_quoted_status(data)
data = self.session.check_long_tweet(data)
if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s" % (i.name,)].append(data)
else: self.session.db["%s" % (i.name,)].insert(0, data)
pub.sendMessage("item-in-list", data=data, user=self.session.db["user_name"], where=i.name)
elif usr == None:
data = self.session.check_quoted_status(data)
data = self.session.check_long_tweet(data)
if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s" % (i.name,)].append(data)
else: self.session.db["%s" % (i.name,)].insert(0, data)
pub.sendMessage("item-in-list", data=data, user=self.session.db["user_name"], where=i.name)
except ValueError:
pass
def set_friends(self, friends):
self.friends = friends
def on_success(self, data):
if "text" in data:
if data.has_key("extended_tweet"):
data["full_text"] = data["extended_tweet"]["full_text"]
if data.has_key("retweeted_status"):
if data["retweeted_status"].has_key("extended_tweet"):
data["retweeted_status"]["full_text"] = data["retweeted_status"]["extended_tweet"]["full_text"]
data["retweeted_status"]["entities"] = data["retweeted_status"]["extended_tweet"]["entities"]
data["full_text"] = data["text"]
if data.has_key("quoted_status"):
if data["quoted_status"].has_key("extended_tweet"):
data["quoted_status"]["full_text"] = data["quoted_status"]["extended_tweet"]["full_text"]
data["quoted_status"]["entities"] = data["quoted_status"]["extended_tweet"]["entities"]
# data["full_text"] = data["text"]
self.check_tls(data)

View File

@ -1,193 +0,0 @@
# -*- coding: utf-8 -*-
import config
from requests.auth import HTTPProxyAuth
from twitter import utils
from twython import TwythonStreamer
from pubsub import pub
import logging as original_logger
log = original_logger.getLogger("MainStream")
class streamer(TwythonStreamer):
def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret, sessionObject, *a, **kw):
super(streamer, self).__init__(app_key, app_secret, oauth_token, oauth_token_secret, *a, **kw)
self.session = sessionObject
self.muted_users = self.session.db["muted_users"]
# self.blocked_users = []
def on_timeout(self, *args, **kwargs):
log.error("Twitter timeout Error")
# pub.sendMessage("stream-error", session=self.session.session_id)
def on_error(self, status_code, data):
log.error("Error %s: %s" % (status_code, data))
# pub.sendMessage("stream-error", session=self.session.session_id)
def get_user(self):
return self.session.db["user_name"]
def put_data(self, place, data):
if self.session.db.has_key(place):
if utils.find_item(data["id"], self.session.db[place]) != None:
log.error("duplicated tweet. Ignoring it...")
return False
# try:
if utils.is_allowed(data, self.session.settings, place):
data_ = self.session.check_quoted_status(data)
data_ = self.session.check_long_tweet(data_)
data = data_
# except:
# pass
if self.session.settings["general"]["reverse_timelines"] == False:
self.session.db[place].append(data)
else:
self.session.db[place].insert(0, data)
utils.is_audio(data)
return True
return False
def block_user(self, data):
id = data["target"]["id"]
if id in self.friends:
self.friends.remove(id)
if "blocks" in self.session.settings["general"]["buffer_order"]:
self.session.db["blocked"]["items"].append(data["target"])
pub.sendMessage("blocked-user", data=data["target"], user=self.get_user())
def unblock(self, data):
if "blocks" in self.session.settings["general"]["buffer_order"] == True:
item = utils.find_item(data["target"]["id"], self.session.db["blocked"]["items"])
self.session.db["blocked"]["items"].pop(item)
pub.sendMessage("unblocked-user", item=item, user=self.get_user())
def check_send(self, data):
if self.session.db["user_name"] == data["user"]["screen_name"]:
d = self.put_data("sent_tweets", data)
if d != False:
pub.sendMessage("sent-tweet", data=data, user=self.get_user())
def check_favs(self, data):
if data["source"]["screen_name"] == self.session.db["user_name"]:
d = self.put_data("favourites", data["target_object"])
if d != False:
pub.sendMessage("favourite", data=data["target_object"], user=self.get_user())
def check_mentions(self, data):
if "@%s" % (self.session.db["user_name"]) in data["text"]:
d = self.put_data("mentions", data)
if d != False:
pub.sendMessage("mention", data=data, user=self.get_user())
def set_quoted_tweet(self, data):
if data["source"]["screen_name"] != self.session.db["user_name"]:
d = self.put_data("mentions", data["target_object"])
if d != False:
pub.sendMessage("mention", data=data["target_object"], user=self.get_user())
def process_dm(self, data):
return
if self.session.db["user_name"] != data["direct_message"]["sender"]["screen_name"]:
# d = self.put_data("sent_direct_messages", data["direct_message"])
# if d != False:
# pub.sendMessage("sent-dm", data=data["direct_message"], user=self.get_user())
# else:
d = self.put_data("direct_messages", data["direct_message"])
if d != False:
pub.sendMessage("direct-message", data=data["direct_message"], user=self.get_user())
def check_follower(self, data):
if data["target"]["screen_name"] == self.session.db["user_name"]:
if self.session.settings["general"]["reverse_timelines"] == False:
self.session.db["followers"]["items"].append(data["source"])
else:
self.session.db["followers"]["items"].insert(0, data["source"])
pub.sendMessage("follower", data=data["source"], user = self.get_user())
else:
if self.session.settings["general"]["reverse_timelines"] == False:
self.session.db["friends"]["items"].append(data["target"])
else:
self.session.db["friends"]["items"].insert(0, data["target"])
pub.sendMessage("friend", data=data["target"], user=self.get_user())
###
def remove_fav(self, data):
if self.session.db["user_name"] == data["source"]["screen_name"]:
item = utils.find_item(data["target_object"]["id"], self.session.db["favourites"])
self.session.db["favourites"].pop(item)
pub.sendMessage("unfavourite", item=item, user=self.get_user())
def remove_friend(self, data):
if "friends" in self.session.settings["general"]["buffer_order"]:
item = utils.find_item(data["target"]["id"], self.session.db["friends"]["items"])
if item > 0:
self.friends.pop(item)
pub.sendMessage("unfollowing", item=item, user=self.get_user())
def on_success(self, data):
try:
# if "delete" in data:
# pub.sendMessage("tweet-deleted", data=data)
if "direct_message" in data:
self.process_dm(data)
elif "friends" in data:
self.friends = data["friends"]
pub.sendMessage("friends-receibed")
elif "text" in data:
if data.has_key("extended_tweet"):
data["full_text"] = data["extended_tweet"]["full_text"]
data["entities"] = data["extended_tweet"]["entities"]
if data.has_key("retweeted_status"):
if data["retweeted_status"].has_key("extended_tweet"):
data["retweeted_status"]["full_text"] = data["retweeted_status"]["extended_tweet"]["full_text"]
data["retweeted_status"]["entities"] = data["retweeted_status"]["extended_tweet"]["entities"]
data["full_text"] = data["text"]
if data.has_key("quoted_status"):
if data["quoted_status"].has_key("extended_tweet"):
data["quoted_status"]["full_text"] = data["quoted_status"]["extended_tweet"]["full_text"]
data["quoted_status"]["entities"] = data["quoted_status"]["extended_tweet"]["entities"]
# data["full_text"] = data["quoted_status"]["full_text"]
if data["user"]["id"] in self.muted_users: return
self.check_mentions(data)
self.check_send(data)
if data["user"]["id"] in self.friends or data["user"]["screen_name"] == self.session.db["user_name"]:
d = self.put_data("home_timeline", data)
if d != False:
pub.sendMessage("item-in-home", data=data, user=self.get_user())
elif data.has_key("event"):
if "favorite" == data["event"] and "favorites" in self.session.settings["general"]["buffer_order"]:
self.check_favs(data)
elif "unfavorite" == data["event"] and "favorites" in self.session.settings["general"]["buffer_order"]:
self.remove_fav(data)
elif "follow" == data["event"] and "followers" in self.session.settings["general"]["buffer_order"]:
self.check_follower(data)
elif "unfollow" == data["event"] and "friends" in self.session.settings["general"]["buffer_order"]:
self.remove_friend(data)
elif "block" == data["event"]:
self.block_user(data)
elif "unblock" == data["event"]:
self.unblock(data)
elif "list_created" == data["event"]:
item = utils.find_item(data["target_object"]["id"], self.session.db["lists"])
if item != None: self.session.db["lists"].append(data["target_object"])
elif "list_destroyed" == data["event"]:
item = utils.find_item(data["target_object"]["id"], self.session.db["lists"])
if item != None: self.session.db["lists"].pop(item)
self.parent.remove_list(data["target_object"]["id"])
elif "list_member_added" == data["event"] and data["source"]["screen_name"] == self.get_user():
pub.sendMessage("new-list-member-added", **{"id":str(data["target"]["id"]), "list":data["target_object"], "user":self.get_user()})
elif "list_member_added" == data["event"] and data["target"]["screen_name"] == self.get_user():
self.session.db["lists"].append(data["target_object"])
elif "list_member_removed" == data["event"] and data["source"]["screen_name"] == self.get_user():
pub.sendMessage("list-member-deleted", **{"id":str(data["target"]["id"]), "list":data["target_object"], "user":self.get_user()})
elif "list_member_removed" == data["event"] and data["target"] == self.get_user():
id = data["target_object"]["id"]
list = utils.find_item(id, self.session.db["lists"])
if list != None: self.session.db["lists"].pop(list)
pub.sendMessage("list-deleted", **{"item":list, "user":self.get_user()})
elif "quoted_tweet" == data["event"]:
self.set_quoted_tweet(data)
if "events" in self.session.settings["general"]["buffer_order"]:
pub.sendMessage("event", data= data, user= self.get_user())
# self.sound.play("new_event.ogg")
except KeyboardInterrupt:
pass