From 3c7063792c60cea8dcea1aba1756b09adc19a970 Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 2 Jul 2021 17:22:24 -0500 Subject: [PATCH] Separate all buffers in modules for an easier work with code --- src/controller/buffers/__init__.py | 9 +- src/controller/buffers/base/__init__.py | 4 + src/controller/buffers/base/account.py | 56 ++ .../buffers/{baseBuffers.py => base/base.py} | 81 +-- src/controller/buffers/base/empty.py | 19 + src/controller/buffers/twitter/__init__.py | 7 + .../{twitterBuffers.py => twitter/base.py} | 638 +----------------- .../buffers/twitter/directMessages.py | 158 +++++ src/controller/buffers/twitter/list.py | 44 ++ src/controller/buffers/twitter/people.py | 259 +++++++ src/controller/buffers/twitter/search.py | 113 ++++ src/controller/buffers/twitter/trends.py | 145 ++++ src/controller/mainController.py | 85 ++- 13 files changed, 856 insertions(+), 762 deletions(-) create mode 100644 src/controller/buffers/base/__init__.py create mode 100644 src/controller/buffers/base/account.py rename src/controller/buffers/{baseBuffers.py => base/base.py} (62%) create mode 100644 src/controller/buffers/base/empty.py create mode 100644 src/controller/buffers/twitter/__init__.py rename src/controller/buffers/{twitterBuffers.py => twitter/base.py} (50%) create mode 100644 src/controller/buffers/twitter/directMessages.py create mode 100644 src/controller/buffers/twitter/list.py create mode 100644 src/controller/buffers/twitter/people.py create mode 100644 src/controller/buffers/twitter/search.py create mode 100644 src/controller/buffers/twitter/trends.py diff --git a/src/controller/buffers/__init__.py b/src/controller/buffers/__init__.py index 4e7aa78c..10e8971c 100644 --- a/src/controller/buffers/__init__.py +++ b/src/controller/buffers/__init__.py @@ -1,8 +1,3 @@ # -*- coding: utf-8 -*- -""" this package contains logic related to buffers. A buffer is a virtual representation of a group of items retrieved through the Social network API'S. - Ideally, new social networks added to TWBlue will have its own "buffers", and these buffers should be defined within this package, following the Twitter example. - Currently, the package contains the following modules: - * baseBuffers: Define a set of functions and structure to be expected in all buffers. New buffers should inherit its classes from one of the classes present here. - * twitterBuffers: All other code, specific to Twitter. -""" -from __future__ import unicode_literals +from . import base as base +from . import twitter as twitter \ No newline at end of file diff --git a/src/controller/buffers/base/__init__.py b/src/controller/buffers/base/__init__.py new file mode 100644 index 00000000..293bbdd8 --- /dev/null +++ b/src/controller/buffers/base/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +from .account import AccountBuffer +from .base import Buffer +from .empty import EmptyBuffer \ No newline at end of file diff --git a/src/controller/buffers/base/account.py b/src/controller/buffers/base/account.py new file mode 100644 index 00000000..46ecbb6d --- /dev/null +++ b/src/controller/buffers/base/account.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +""" Common logic to all buffers in TWBlue.""" +import logging +import config +import widgetUtils +from pubsub import pub +from wxUI import buffers +from . import base + +log = logging.getLogger("controller.buffers.base.account") + +class AccountBuffer(base.Buffer): + def __init__(self, parent, name, account, account_id): + super(AccountBuffer, self).__init__(parent, None, name) + log.debug("Initializing buffer %s, account %s" % (name, account,)) + self.buffer = buffers.accountPanel(parent, name) + self.type = self.buffer.type + self.compose_function = None + self.session = None + self.needs_init = False + self.account = account + self.buffer.account = account + self.name = name + self.account_id = account_id + + def setup_account(self): + widgetUtils.connect_event(self.buffer, widgetUtils.CHECKBOX, self.autostart, menuitem=self.buffer.autostart_account) + if self.account_id in config.app["sessions"]["ignored_sessions"]: + self.buffer.change_autostart(False) + else: + self.buffer.change_autostart(True) + if not hasattr(self, "logged"): + self.buffer.change_login(login=False) + widgetUtils.connect_event(self.buffer.login, widgetUtils.BUTTON_PRESSED, self.logout) + else: + self.buffer.change_login(login=True) + widgetUtils.connect_event(self.buffer.login, widgetUtils.BUTTON_PRESSED, self.login) + + def login(self, *args, **kwargs): + del self.logged + self.setup_account() + pub.sendMessage("login", session_id=self.account_id) + + def logout(self, *args, **kwargs): + self.logged = False + self.setup_account() + pub.sendMessage("logout", session_id=self.account_id) + + def autostart(self, *args, **kwargs): + if self.account_id in config.app["sessions"]["ignored_sessions"]: + self.buffer.change_autostart(True) + config.app["sessions"]["ignored_sessions"].remove(self.account_id) + else: + self.buffer.change_autostart(False) + config.app["sessions"]["ignored_sessions"].append(self.account_id) + config.app.write() \ No newline at end of file diff --git a/src/controller/buffers/baseBuffers.py b/src/controller/buffers/base/base.py similarity index 62% rename from src/controller/buffers/baseBuffers.py rename to src/controller/buffers/base/base.py index 7437aa48..41b31219 100644 --- a/src/controller/buffers/baseBuffers.py +++ b/src/controller/buffers/base/base.py @@ -1,26 +1,14 @@ # -*- coding: utf-8 -*- """ Common logic to all buffers in TWBlue.""" -from __future__ import unicode_literals -from builtins import object import logging import wx import output -import config import sound import widgetUtils -from pubsub import pub -from wxUI import buffers -log = logging.getLogger("controller.buffers.baseBuffers") +log = logging.getLogger("controller.buffers.base.base") -def _items_exist(function): - """ A decorator to execute a function only if the selected buffer contains at least one item.""" - def function_(self, *args, **kwargs): - if self.buffer.list.get_count() > 0: - function(self, *args, **kwargs) - return function_ - -class buffer(object): +class Buffer(object): """ A basic buffer object. This should be the base class for all other derived buffers.""" def __init__(self, parent=None, function=None, session=None, *args, **kwargs): @@ -29,11 +17,11 @@ class buffer(object): @ function str or None: function to be called periodically and update items on this buffer. @ session sessionmanager.session object or None: Session handler for settings, database and data access. """ - super(buffer, self).__init__() + super(Buffer, self).__init__() self.function = function # Compose_function will be used to render an object on this buffer. Normally, signature is as follows: # compose_function(item, db, relative_times, show_screen_names=False, session=None) - # Read more about compose functions in twitter/compose.py. + # Read more about compose functions in sessions/twitter/compose.py. self.compose_function = None self.args = args self.kwargs = kwargs @@ -147,63 +135,4 @@ class buffer(object): try: self.session.db[self.name+"_pos"]=self.buffer.list.get_selected() except AttributeError: - pass - -class accountPanel(buffer): - def __init__(self, parent, name, account, account_id): - super(accountPanel, self).__init__(parent, None, name) - log.debug("Initializing buffer %s, account %s" % (name, account,)) - self.buffer = buffers.accountPanel(parent, name) - self.type = self.buffer.type - self.compose_function = None - self.session = None - self.needs_init = False - self.account = account - self.buffer.account = account - self.name = name - self.account_id = account_id - - def setup_account(self): - widgetUtils.connect_event(self.buffer, widgetUtils.CHECKBOX, self.autostart, menuitem=self.buffer.autostart_account) - if self.account_id in config.app["sessions"]["ignored_sessions"]: - self.buffer.change_autostart(False) - else: - self.buffer.change_autostart(True) - if not hasattr(self, "logged"): - self.buffer.change_login(login=False) - widgetUtils.connect_event(self.buffer.login, widgetUtils.BUTTON_PRESSED, self.logout) - else: - self.buffer.change_login(login=True) - widgetUtils.connect_event(self.buffer.login, widgetUtils.BUTTON_PRESSED, self.login) - - def login(self, *args, **kwargs): - del self.logged - self.setup_account() - pub.sendMessage("login", session_id=self.account_id) - - def logout(self, *args, **kwargs): - self.logged = False - self.setup_account() - pub.sendMessage("logout", session_id=self.account_id) - - def autostart(self, *args, **kwargs): - if self.account_id in config.app["sessions"]["ignored_sessions"]: - self.buffer.change_autostart(True) - config.app["sessions"]["ignored_sessions"].remove(self.account_id) - else: - self.buffer.change_autostart(False) - config.app["sessions"]["ignored_sessions"].append(self.account_id) - config.app.write() - -class emptyPanel(buffer): - def __init__(self, parent, name, account): - super(emptyPanel, self).__init__(parent=parent) - log.debug("Initializing buffer %s, account %s" % (name, account,)) - self.buffer = buffers.emptyPanel(parent, name) - self.type = self.buffer.type - self.compose_function = None - self.account = account - self.buffer.account = account - self.name = name - self.session = None - self.needs_init = True + pass \ No newline at end of file diff --git a/src/controller/buffers/base/empty.py b/src/controller/buffers/base/empty.py new file mode 100644 index 00000000..66373d7c --- /dev/null +++ b/src/controller/buffers/base/empty.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +import logging +from wxUI import buffers +from . import base + +log = logging.getLogger("controller.buffers.base.empty") + +class EmptyBuffer(base.Buffer): + def __init__(self, parent, name, account): + super(EmptyBuffer, self).__init__(parent=parent) + log.debug("Initializing buffer %s, account %s" % (name, account,)) + self.buffer = buffers.emptyPanel(parent, name) + self.type = self.buffer.type + self.compose_function = None + self.account = account + self.buffer.account = account + self.name = name + self.session = None + self.needs_init = True diff --git a/src/controller/buffers/twitter/__init__.py b/src/controller/buffers/twitter/__init__.py new file mode 100644 index 00000000..c893f6ba --- /dev/null +++ b/src/controller/buffers/twitter/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +from .base import BaseBuffer +from .directMessages import DirectMessagesBuffer, SentDirectMessagesBuffer +from .list import ListBuffer +from .people import PeopleBuffer +from .trends import TrendsBuffer +from .search import SearchBuffer, SearchPeopleBuffer, ConversationBuffer diff --git a/src/controller/buffers/twitterBuffers.py b/src/controller/buffers/twitter/base.py similarity index 50% rename from src/controller/buffers/twitterBuffers.py rename to src/controller/buffers/twitter/base.py index 3e09243f..5ccc0ae6 100644 --- a/src/controller/buffers/twitterBuffers.py +++ b/src/controller/buffers/twitter/base.py @@ -18,7 +18,7 @@ import sound import languageHandler import logging from audio_services import youtube_utils -from controller.buffers import baseBuffers +from controller.buffers.base import base from sessions.twitter import compose, utils, reduce from mysc.thread_utils import call_threaded from tweepy.error import TweepError @@ -35,9 +35,9 @@ def _tweets_exist(function): function(self, *args, **kwargs) return function_ -class baseBufferController(baseBuffers.buffer): +class BaseBuffer(base.Buffer): def __init__(self, parent, function, name, sessionObject, account, sound=None, bufferType=None, compose_func="compose_tweet", *args, **kwargs): - super(baseBufferController, self).__init__(parent, function, *args, **kwargs) + super(BaseBuffer, self).__init__(parent, function, *args, **kwargs) log.debug("Initializing buffer %s, account %s" % (name, account,)) if bufferType != None: self.buffer = getattr(buffers, bufferType)(parent, name) @@ -638,634 +638,4 @@ class baseBufferController(baseBuffers.buffer): tweet = self.get_tweet() output.speak(_(u"Opening item in web browser...")) url = "https://twitter.com/{screen_name}/status/{tweet_id}".format(screen_name=self.session.get_user(tweet.user).screen_name, tweet_id=tweet.id) - webbrowser.open(url) - -class directMessagesController(baseBufferController): - - def get_more_items(self): - # 50 results are allowed per API call, so let's assume max value can be 50. - # reference: https://developer.twitter.com/en/docs/twitter-api/v1/direct-messages/sending-and-receiving/api-reference/list-events - if self.session.settings["general"]["max_tweets_per_call"] > 50: - count = 50 - else: - count = self.session.settings["general"]["max_tweets_per_call"] - total = 0 - # try to retrieve the cursor for the current buffer. - cursor = self.session.db["cursors"].get(self.name) - try: - items = getattr(self.session.twitter, self.function)(return_cursors=True, cursor=cursor, count=count, *self.args, **self.kwargs) - if type(items) == tuple: - items, cursor = items - if type(cursor) == tuple: - cursor = cursor[1] - cursors = self.session.db["cursors"] - cursors[self.name] = cursor - self.session.db["cursors"] = cursors - results = [i for i in items] - items = results - log.debug("Retrieved %d items for cursored search in function %s" % (len(items), self.function)) - except TweepError as e: - log.error("Error %s: %s" % (e.api_code, e.reason)) - return - if items == None: - return - sent = [] - received = [] - sent_dms = self.session.db["sent_direct_messages"] - received_dms = self.session.db["direct_messages"] - for i in items: - if int(i.message_create["sender_id"]) == self.session.db["user_id"]: - if self.session.settings["general"]["reverse_timelines"] == False: - sent_dms.insert(0, i) - sent.append(i) - else: - sent_dms.append(i) - sent.insert(0, i) - else: - if self.session.settings["general"]["reverse_timelines"] == False: - received_dms.insert(0, i) - received.append(i) - else: - received_dms.append(i) - received.insert(0, i) - total = total+1 - self.session.db["direct_messages"] = received_dms - self.session.db["sent_direct_messages"] = sent_dms - user_ids = [item.message_create["sender_id"] for item in items] - self.session.save_users(user_ids) - pub.sendMessage("more-sent-dms", data=sent, account=self.session.db["user_name"]) - selected = self.buffer.list.get_selected() - if self.session.settings["general"]["reverse_timelines"] == True: - for i in received: - if int(i.message_create["sender_id"]) == self.session.db["user_id"]: - continue - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) - self.buffer.list.insert_item(True, *tweet) - self.buffer.list.select_item(selected) - else: - for i in received: - if int(i.message_create["sender_id"]) == self.session.db["user_id"]: - continue - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) - self.buffer.list.insert_item(True, *tweet) - output.speak(_(u"%s items retrieved") % (total), True) - - @_tweets_exist - def reply(self, *args, **kwargs): - tweet = self.get_right_tweet() - screen_name = self.session.get_user(tweet.message_create["sender_id"]).screen_name - message = messages.reply(self.session, _(u"Mention"), _(u"Mention to %s") % (screen_name,), "@%s " % (screen_name,), [screen_name,]) - if message.message.get_response() == widgetUtils.OK: - if config.app["app-settings"]["remember_mention_and_longtweet"]: - config.app["app-settings"]["longtweet"] = message.message.long_tweet.GetValue() - config.app.write() - if message.image == None: - item = self.session.api_call(call_name="update_status", _sound="reply_send.ogg", status=message.message.get_text(), tweet_mode="extended") - if item != None: - pub.sendMessage("sent-tweet", data=item, user=self.session.db["user_name"]) - else: - call_threaded(self.session.api_call, call_name="update_status_with_media", _sound="reply_send.ogg", status=message.message.get_text(), media=message.file) - if hasattr(message.message, "destroy"): message.message.destroy() - - def onFocus(self, *args, **kwargs): - tweet = self.get_tweet() - if platform.system() == "Windows" and self.session.settings["general"]["relative_times"] == True: - # fix this: - original_date = arrow.get(int(tweet.created_timestamp)) - ts = original_date.humanize(locale=languageHandler.getLanguage()) - self.buffer.list.list.SetItem(self.buffer.list.get_selected(), 2, ts) - if self.session.settings['sound']['indicate_audio'] and utils.is_audio(tweet): - self.session.sound.play("audio.ogg") - if self.session.settings['sound']['indicate_img'] and utils.is_media(tweet): - self.session.sound.play("image.ogg") - - def clear_list(self): - dlg = commonMessageDialogs.clear_list() - if dlg == widgetUtils.YES: - self.session.db[self.name] = [] - self.buffer.list.clear() - - def auto_read(self, number_of_items): - if number_of_items == 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: - if self.session.settings["general"]["reverse_timelines"] == False: - tweet = self.session.db[self.name][-1] - else: - tweet = self.session.db[self.name][0] - output.speak(_(u"New direct message")) - output.speak(" ".join(self.compose_function(tweet, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session))) - elif number_of_items > 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: - output.speak(_(u"{0} new direct messages.").format(number_of_items,)) - - def open_in_browser(self, *args, **kwargs): - output.speak(_(u"This action is not supported in the buffer yet.")) - -class sentDirectMessagesController(directMessagesController): - - def __init__(self, *args, **kwargs): - super(sentDirectMessagesController, self).__init__(*args, **kwargs) - if ("sent_direct_messages" in self.session.db) == False: - self.session.db["sent_direct_messages"] = [] - - def get_more_items(self): - output.speak(_(u"Getting more items cannot be done in this buffer. Use the direct messages buffer instead.")) - - def start_stream(self, *args, **kwargs): - pass - - def put_more_items(self, items): - if self.session.settings["general"]["reverse_timelines"] == True: - for i in items: - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) - self.buffer.list.insert_item(False, *tweet) - else: - for i in items: - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) - self.buffer.list.insert_item(False, *tweet) - -class listBufferController(baseBufferController): - def __init__(self, parent, function, name, sessionObject, account, sound=None, bufferType=None, list_id=None, *args, **kwargs): - super(listBufferController, self).__init__(parent, function, name, sessionObject, account, sound=None, bufferType=None, *args, **kwargs) - self.users = [] - self.list_id = list_id - self.kwargs["list_id"] = list_id - - def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): - self.get_user_ids() - super(listBufferController, self).start_stream(mandatory, play_sound, avoid_autoreading) - - def get_user_ids(self): - for i in Cursor(self.session.twitter.list_members, list_id=self.list_id, include_entities=False, skip_status=True, count=5000).items(): - if i.id not in self.users: - self.users.append(i.id) - - def remove_buffer(self, force=False): - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name[:-5] in self.session.settings["other_buffers"]["lists"]: - self.session.settings["other_buffers"]["lists"].remove(self.name[:-5]) - if self.name in self.session.db: - self.session.db.pop(self.name) - self.session.settings.write() - return True - elif dlg == widgetUtils.NO: - return False - -class peopleBufferController(baseBufferController): - def __init__(self, parent, function, name, sessionObject, account, bufferType=None, *args, **kwargs): - super(peopleBufferController, self).__init__(parent, function, name, sessionObject, account, bufferType="peoplePanel", *args, **kwargs) - log.debug("Initializing buffer %s, account %s" % (name, account,)) - self.compose_function = compose.compose_followers_list - log.debug("Compose_function: %s" % (self.compose_function,)) - self.get_tweet = self.get_right_tweet - self.url = self.interact - if "-followers" in self.name or "-friends" in self.name: - self.finished_timeline = False - # Add a compatibility layer for username based timelines from config. - # ToDo: Remove this in some new versions of the client, when user ID timelines become mandatory. - try: - int(self.kwargs["user_id"]) - except ValueError: - self.is_screen_name = True - self.kwargs["screen_name"] = self.kwargs["user_id"] - self.kwargs.pop("user_id") - - def remove_buffer(self, force=True): - if "-followers" in self.name: - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name[:-10] in self.session.settings["other_buffers"]["followers_timelines"]: - self.session.settings["other_buffers"]["followers_timelines"].remove(self.name[:-10]) - if self.name in self.session.db: - self.session.db.pop(self.name) - self.session.settings.write() - return True - elif dlg == widgetUtils.NO: - return False - elif "-friends" in self.name: - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name[:-8] in self.session.settings["other_buffers"]["friends_timelines"]: - self.session.settings["other_buffers"]["friends_timelines"].remove(self.name[:-8]) - if self.name in self.session.db: - self.session.db.pop(self.name) - self.session.settings.write() - return True - elif dlg == widgetUtils.NO: - return False - else: - output.speak(_(u"This buffer is not a timeline; it can't be deleted."), True) - return False - - def onFocus(self, ev): - pass - - def get_message(self): - return " ".join(self.compose_function(self.get_tweet(), self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session)) - - def delete_item(self): pass - - @_tweets_exist - def reply(self, *args, **kwargs): - tweet = self.get_right_tweet() - screen_name = tweet.screen_name - message = messages.reply(self.session, _(u"Mention"), _(u"Mention to %s") % (screen_name,), "@%s " % (screen_name,), [screen_name,]) - if message.message.get_response() == widgetUtils.OK: - if config.app["app-settings"]["remember_mention_and_longtweet"]: - config.app["app-settings"]["longtweet"] = message.message.long_tweet.GetValue() - config.app.write() - if message.image == None: - item = self.session.api_call(call_name="update_status", _sound="reply_send.ogg", status=message.message.get_text(), tweet_mode="extended") - if item != None: - pub.sendMessage("sent-tweet", data=item, user=self.session.db["user_name"]) - else: - call_threaded(self.session.api_call, call_name="update_status_with_media", _sound="reply_send.ogg", status=message.message.get_text(), media=message.file) - if hasattr(message.message, "destroy"): message.message.destroy() - - def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): - # starts stream every 3 minutes. - current_time = time.time() - if self.execution_time == 0 or current_time-self.execution_time >= 180 or mandatory==True: - self.execution_time = current_time - log.debug("Starting stream for %s buffer, %s account" % (self.name, self.account,)) - log.debug("args: %s, kwargs: %s" % (self.args, self.kwargs)) - try: - val = getattr(self.session.twitter, self.function)(return_cursors=True, count=self.session.settings["general"]["max_tweets_per_call"], *self.args, **self.kwargs) - if type(val) == tuple: - val, cursor = val - if type(cursor) == tuple: - cursor = cursor[1] - cursors = self.session.db["cursors"] - cursors[self.name] = cursor - self.session.db["cursors"] = cursors - results = [i for i in val] - val = results - val.reverse() - log.debug("Retrieved %d items from cursored search in function %s" % (len(val), self.function)) - except TweepError as e: - log.error("Error %s: %s" % (e.api_code, e.reason)) - return - number_of_items = self.session.order_people(self.name, val) - log.debug("Number of items retrieved: %d" % (number_of_items,)) - self.put_items_on_list(number_of_items) - if hasattr(self, "finished_timeline") and self.finished_timeline == False: - self.username = self.session.api_call("get_user", **self.kwargs).screen_name - self.finished_timeline = True - if number_of_items > 0 and self.sound != None and self.session.settings["sound"]["session_mute"] == False and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and play_sound == True: - self.session.sound.play(self.sound) - # Autoread settings - if avoid_autoreading == False and mandatory == True and number_of_items > 0 and self.name in self.session.settings["other_buffers"]["autoread_buffers"]: - self.auto_read(number_of_items) - return number_of_items - - def get_more_items(self): - try: - cursor = self.session.db["cursors"].get(self.name) - items = getattr(self.session.twitter, self.function)(return_cursors=True, users=True, cursor=cursor, count=self.session.settings["general"]["max_tweets_per_call"], *self.args, **self.kwargs) - if type(items) == tuple: - items, cursor = items - if type(cursor) == tuple: - cursor = cursor[1] - cursors = self.session.db["cursors"] - cursors[self.name] = cursor - self.session.db["cursors"] = cursors - results = [i for i in items] - items = results - log.debug("Retrieved %d items from cursored search in function %s" % (len(items), self.function)) - except TweepError as e: - log.error("Error %s: %s" % (e.api_code, e.reason)) - return - if items == None: - return - items_db = self.session.db[self.name] - for i in items: - if self.session.settings["general"]["reverse_timelines"] == False: - items_db.insert(0, i) - else: - items_db.append(i) - self.session.db[self.name] = items_db - selected = self.buffer.list.get_selected() - if self.session.settings["general"]["reverse_timelines"] == True: - for i in items: - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session) - self.buffer.list.insert_item(True, *tweet) - self.buffer.list.select_item(selected) - else: - for i in items: - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session) - self.buffer.list.insert_item(True, *tweet) - output.speak(_(u"%s items retrieved") % (len(items)), True) - - def put_items_on_list(self, number_of_items): - log.debug("The list contains %d items" % (self.buffer.list.get_count(),)) -# log.debug("Putting %d items on the list..." % (number_of_items,)) - if self.buffer.list.get_count() == 0: - for i in self.session.db[self.name]: - tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session) - self.buffer.list.insert_item(False, *tweet) - self.buffer.set_position(self.session.settings["general"]["reverse_timelines"]) -# self.buffer.set_list_position() - elif self.buffer.list.get_count() > 0: - if self.session.settings["general"]["reverse_timelines"] == False: - for i in self.session.db[self.name][len(self.session.db[self.name])-number_of_items:]: - tweet = self.compose_function(i, self.session.db) - self.buffer.list.insert_item(False, *tweet) - else: - items = self.session.db[self.name][0:number_of_items] - items.reverse() - for i in items: - tweet = self.compose_function(i, self.session.db) - self.buffer.list.insert_item(True, *tweet) - log.debug("now the list contains %d items" % (self.buffer.list.get_count(),)) - - def get_right_tweet(self): - tweet = self.session.db[self.name][self.buffer.list.get_selected()] - return tweet - - def add_new_item(self, item): - tweet = self.compose_function(item, self.session.db, self.session.settings["general"]["relative_times"], self.session) - if self.session.settings["general"]["reverse_timelines"] == False: - self.buffer.list.insert_item(False, *tweet) - else: - self.buffer.list.insert_item(True, *tweet) - if self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: - output.speak(" ".join(tweet)) - - def clear_list(self): - dlg = commonMessageDialogs.clear_list() - if dlg == widgetUtils.YES: - self.session.db[self.name] = [] - self.session.db["cursors"][self.name] = -1 - self.buffer.list.clear() - - def interact(self): - user.profileController(self.session, user=self.get_right_tweet().screen_name) - - def show_menu(self, ev, pos=0, *args, **kwargs): - menu = menus.peoplePanelMenu() - widgetUtils.connect_event(menu, widgetUtils.MENU, self.send_message, menuitem=menu.reply) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.user_actions, menuitem=menu.userActions) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.details, menuitem=menu.details) -# widgetUtils.connect_event(menu, widgetUtils.MENU, self.lists, menuitem=menu.lists) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.view, menuitem=menu.view) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.copy, menuitem=menu.copy) - if hasattr(menu, "openInBrowser"): - widgetUtils.connect_event(menu, widgetUtils.MENU, self.open_in_browser, menuitem=menu.openInBrowser) - if pos != 0: - self.buffer.PopupMenu(menu, pos) - else: - self.buffer.PopupMenu(menu, ev.GetPosition()) - - def details(self, *args, **kwargs): - pub.sendMessage("execute-action", action="user_details") - - def auto_read(self, number_of_items): - if number_of_items == 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: - if self.session.settings["general"]["reverse_timelines"] == False: - tweet = self.session.db[self.name][-1] - else: - tweet = self.session.db[self.name][0] - output.speak(" ".join(self.compose_function(tweet, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session))) - elif number_of_items > 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: - output.speak(_(u"{0} new followers.").format(number_of_items)) - - def open_in_browser(self, *args, **kwargs): - tweet = self.get_tweet() - output.speak(_(u"Opening item in web browser...")) - url = "https://twitter.com/{screen_name}".format(screen_name=tweet.screen_name) - webbrowser.open(url) - -class searchBufferController(baseBufferController): - - def remove_buffer(self, force=False): - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name[:-11] in self.session.settings["other_buffers"]["tweet_searches"]: - self.session.settings["other_buffers"]["tweet_searches"].remove(self.name[:-11]) - self.session.settings.write() - if self.name in self.session.db: - self.session.db.pop(self.name) - return True - elif dlg == widgetUtils.NO: - return False - -class searchPeopleBufferController(peopleBufferController): - """ This is identical to a normal peopleBufferController, except that uses the page parameter instead of a cursor.""" - def __init__(self, parent, function, name, sessionObject, account, bufferType="peoplePanel", *args, **kwargs): - super(searchPeopleBufferController, self).__init__(parent, function, name, sessionObject, account, bufferType="peoplePanel", *args, **kwargs) - if ("page" in self.kwargs) == False: - self.page = 1 - else: - self.page = self.kwargs.pop("page") - - def get_more_items(self, *args, **kwargs): - # Add 1 to the page parameter, put it in kwargs and calls to get_more_items in the parent buffer. - self.page = self.page +1 - self.kwargs["page"] = self.page - super(searchPeopleBufferController, self).get_more_items(*args, **kwargs) - # remove the parameter again to make sure start_stream won't fetch items for this page indefinitely. - self.kwargs.pop("page") - - def remove_buffer(self, force=False): - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name[:-11] in self.session.settings["other_buffers"]["tweet_searches"]: - self.session.settings["other_buffers"]["tweet_searches"].remove(self.name[:-11]) - self.session.settings.write() - if self.name in self.session.db: - self.session.db.pop(self.name) - return True - elif dlg == widgetUtils.NO: - return False - -class trendsBufferController(baseBuffers.buffer): - def __init__(self, parent, name, session, account, trendsFor, *args, **kwargs): - super(trendsBufferController, self).__init__(parent=parent, session=session) - self.trendsFor = trendsFor - self.session = session - self.account = account - self.invisible = True - self.buffer = buffers.trendsPanel(parent, name) - self.buffer.account = account - self.type = self.buffer.type - self.bind_events() - self.sound = "trends_updated.ogg" - self.trends = [] - self.name = name - self.buffer.name = name - self.compose_function = self.compose_function_ - self.get_formatted_message = self.get_message - self.reply = self.search_topic - - def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): - # starts stream every 3 minutes. - current_time = time.time() - if self.execution_time == 0 or current_time-self.execution_time >= 180 or mandatory == True: - self.execution_time = current_time - try: - data = self.session.twitter.trends_place(id=self.trendsFor) - except TweepError as err: - log.error("Error %s: %s" % (err.api_code, err.reason)) - if not hasattr(self, "name_"): - self.name_ = data[0]["locations"][0]["name"] - self.trends = data[0]["trends"] - self.put_items_on_the_list() - if self.sound != None and self.session.settings["sound"]["session_mute"] == False and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and play_sound == True: - self.session.sound.play(self.sound) - - def put_items_on_the_list(self): - selected_item = self.buffer.list.get_selected() - self.buffer.list.clear() - for i in self.trends: - tweet = self.compose_function(i) - self.buffer.list.insert_item(False, *tweet) - self.buffer.set_position(self.session.settings["general"]["reverse_timelines"]) - - def compose_function_(self, trend): - return [trend["name"]] - - def bind_events(self): - log.debug("Binding events...") - self.buffer.list.list.Bind(wx.EVT_CHAR_HOOK, self.get_event) - widgetUtils.connect_event(self.buffer, widgetUtils.BUTTON_PRESSED, self.tweet_about_this_trend, self.buffer.tweetTrendBtn) - widgetUtils.connect_event(self.buffer, widgetUtils.BUTTON_PRESSED, self.post_status, self.buffer.tweet) - widgetUtils.connect_event(self.buffer.list.list, wx.EVT_LIST_ITEM_RIGHT_CLICK, self.show_menu) - widgetUtils.connect_event(self.buffer.list.list, wx.EVT_LIST_KEY_DOWN, self.show_menu_by_key) - widgetUtils.connect_event(self.buffer, widgetUtils.BUTTON_PRESSED, self.search_topic, self.buffer.search_topic) - - def get_message(self): - return self.compose_function(self.trends[self.buffer.list.get_selected()])[0] - - def remove_buffer(self, force=False): - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name[:-3] in self.session.settings["other_buffers"]["trending_topic_buffers"]: - self.session.settings["other_buffers"]["trending_topic_buffers"].remove(self.name[:-3]) - self.session.settings.write() - if self.name in self.session.db: - self.session.db.pop(self.name) - return True - elif dlg == widgetUtils.NO: - return False - - def url(self, *args, **kwargs): - self.tweet_about_this_trend() - - def search_topic(self, *args, **kwargs): - topic = self.trends[self.buffer.list.get_selected()]["name"] - pub.sendMessage("search", term=topic) - - def show_menu(self, ev, pos=0, *args, **kwargs): - menu = menus.trendsPanelMenu() - widgetUtils.connect_event(menu, widgetUtils.MENU, self.search_topic, menuitem=menu.search_topic) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.tweet_about_this_trend, menuitem=menu.tweetThisTrend) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.view, menuitem=menu.view) - widgetUtils.connect_event(menu, widgetUtils.MENU, self.copy, menuitem=menu.copy) - if pos != 0: - self.buffer.PopupMenu(menu, pos) - else: - self.buffer.PopupMenu(menu, ev.GetPosition()) - - def view(self, *args, **kwargs): - pub.sendMessage("execute-action", action="view_item") - - def copy(self, *args, **kwargs): - pub.sendMessage("execute-action", action="copy_to_clipboard") - - def tweet_about_this_trend(self, *args, **kwargs): - if self.buffer.list.get_count() == 0: return - title = _(u"Tweet") - caption = _(u"Write the tweet here") - tweet = messages.tweet(self.session, title, caption, self.get_message()+ " ") - tweet.message.set_cursor_at_end() - if tweet.message.get_response() == widgetUtils.OK: - text = tweet.message.get_text() - if len(text) > 280 and tweet.message.get("long_tweet") == True: - if tweet.image == None: - text = twishort.create_tweet(self.session.settings["twitter"]["user_key"], self.session.settings["twitter"]["user_secret"], text) - else: - text = twishort.create_tweet(self.session.settings["twitter"]["user_key"], self.session.settings["twitter"]["user_secret"], text, 1) - if tweet.image == None: - call_threaded(self.session.api_call, call_name="update_status", status=text) - else: - call_threaded(self.session.api_call, call_name="update_status_with_media", status=text, media=tweet.image) - if hasattr(tweet.message, "destroy"): tweet.message.destroy() - - def show_menu_by_key(self, ev): - if self.buffer.list.get_count() == 0: - return - if ev.GetKeyCode() == wx.WXK_WINDOWS_MENU: - self.show_menu(widgetUtils.MENU, pos=self.buffer.list.list.GetPosition()) - - def open_in_browser(self, *args, **kwargs): - output.speak(_(u"This action is not supported in the buffer, yet.")) - -class conversationBufferController(searchBufferController): - - def start_stream(self, start=False, mandatory=False, play_sound=True, avoid_autoreading=False): - # starts stream every 3 minutes. - current_time = time.time() - if self.execution_time == 0 or current_time-self.execution_time >= 180 or mandatory == True: - self.execution_time = current_time - if start == True: - self.statuses = [] - self.ids = [] - self.statuses.append(self.tweet) - self.ids.append(self.tweet.id) - tweet = self.tweet - if not hasattr(tweet, "in_reply_to_status_id"): - tweet.in_reply_to_status_id = None - while tweet.in_reply_to_status_id != None: - try: - tweet = self.session.twitter.get_status(id=tweet.in_reply_to_status_id, tweet_mode="extended") - except TweepError as err: - break - self.statuses.insert(0, tweet) - self.ids.append(tweet.id) - if tweet.in_reply_to_status_id == None: - self.kwargs["since_id"] = tweet.id - self.ids.append(tweet.id) - val2 = self.session.search(self.name, tweet_mode="extended", *self.args, **self.kwargs) - for i in val2: - if i.in_reply_to_status_id in self.ids: - self.statuses.append(i) - self.ids.append(i.id) - tweet = i - number_of_items = self.session.order_buffer(self.name, self.statuses) - log.debug("Number of items retrieved: %d" % (number_of_items,)) - self.put_items_on_list(number_of_items) - if number_of_items > 0 and self.sound != None and self.session.settings["sound"]["session_mute"] == False and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and play_sound == True: - self.session.sound.play(self.sound) - # Autoread settings - if avoid_autoreading == False and mandatory == True and number_of_items > 0 and self.name in self.session.settings["other_buffers"]["autoread_buffers"]: - self.auto_read(number_of_items) - return number_of_items - - def remove_buffer(self, force=False): - if force == False: - dlg = commonMessageDialogs.remove_buffer() - else: - dlg = widgetUtils.YES - if dlg == widgetUtils.YES: - if self.name in self.session.db: - self.session.db.pop(self.name) - return True - elif dlg == widgetUtils.NO: - return False + webbrowser.open(url) \ No newline at end of file diff --git a/src/controller/buffers/twitter/directMessages.py b/src/controller/buffers/twitter/directMessages.py new file mode 100644 index 00000000..d5e556b6 --- /dev/null +++ b/src/controller/buffers/twitter/directMessages.py @@ -0,0 +1,158 @@ +# -*- coding: utf-8 -*- +import platform +import widgetUtils +import arrow +import webbrowser +import output +import config +import languageHandler +import logging +from controller import messages +from sessions.twitter import compose, utils +from mysc.thread_utils import call_threaded +from tweepy.error import TweepError +from pubsub import pub +from . import base + +log = logging.getLogger("controller.buffers.twitter.dmBuffer") + +class DirectMessagesBuffer(base.BaseBuffer): + + def get_more_items(self): + # 50 results are allowed per API call, so let's assume max value can be 50. + # reference: https://developer.twitter.com/en/docs/twitter-api/v1/direct-messages/sending-and-receiving/api-reference/list-events + if self.session.settings["general"]["max_tweets_per_call"] > 50: + count = 50 + else: + count = self.session.settings["general"]["max_tweets_per_call"] + total = 0 + # try to retrieve the cursor for the current buffer. + cursor = self.session.db["cursors"].get(self.name) + try: + items = getattr(self.session.twitter, self.function)(return_cursors=True, cursor=cursor, count=count, *self.args, **self.kwargs) + if type(items) == tuple: + items, cursor = items + if type(cursor) == tuple: + cursor = cursor[1] + cursors = self.session.db["cursors"] + cursors[self.name] = cursor + self.session.db["cursors"] = cursors + results = [i for i in items] + items = results + log.debug("Retrieved %d items for cursored search in function %s" % (len(items), self.function)) + except TweepError as e: + log.error("Error %s: %s" % (e.api_code, e.reason)) + return + if items == None: + return + sent = [] + received = [] + sent_dms = self.session.db["sent_direct_messages"] + received_dms = self.session.db["direct_messages"] + for i in items: + if int(i.message_create["sender_id"]) == self.session.db["user_id"]: + if self.session.settings["general"]["reverse_timelines"] == False: + sent_dms.insert(0, i) + sent.append(i) + else: + sent_dms.append(i) + sent.insert(0, i) + else: + if self.session.settings["general"]["reverse_timelines"] == False: + received_dms.insert(0, i) + received.append(i) + else: + received_dms.append(i) + received.insert(0, i) + total = total+1 + self.session.db["direct_messages"] = received_dms + self.session.db["sent_direct_messages"] = sent_dms + user_ids = [item.message_create["sender_id"] for item in items] + self.session.save_users(user_ids) + pub.sendMessage("more-sent-dms", data=sent, account=self.session.db["user_name"]) + selected = self.buffer.list.get_selected() + if self.session.settings["general"]["reverse_timelines"] == True: + for i in received: + if int(i.message_create["sender_id"]) == self.session.db["user_id"]: + continue + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) + self.buffer.list.insert_item(True, *tweet) + self.buffer.list.select_item(selected) + else: + for i in received: + if int(i.message_create["sender_id"]) == self.session.db["user_id"]: + continue + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) + self.buffer.list.insert_item(True, *tweet) + output.speak(_(u"%s items retrieved") % (total), True) + + def reply(self, *args, **kwargs): + tweet = self.get_right_tweet() + screen_name = self.session.get_user(tweet.message_create["sender_id"]).screen_name + message = messages.reply(self.session, _(u"Mention"), _(u"Mention to %s") % (screen_name,), "@%s " % (screen_name,), [screen_name,]) + if message.message.get_response() == widgetUtils.OK: + if config.app["app-settings"]["remember_mention_and_longtweet"]: + config.app["app-settings"]["longtweet"] = message.message.long_tweet.GetValue() + config.app.write() + if message.image == None: + item = self.session.api_call(call_name="update_status", _sound="reply_send.ogg", status=message.message.get_text(), tweet_mode="extended") + if item != None: + pub.sendMessage("sent-tweet", data=item, user=self.session.db["user_name"]) + else: + call_threaded(self.session.api_call, call_name="update_status_with_media", _sound="reply_send.ogg", status=message.message.get_text(), media=message.file) + if hasattr(message.message, "destroy"): message.message.destroy() + + def onFocus(self, *args, **kwargs): + tweet = self.get_tweet() + if platform.system() == "Windows" and self.session.settings["general"]["relative_times"] == True: + # fix this: + original_date = arrow.get(int(tweet.created_timestamp)) + ts = original_date.humanize(locale=languageHandler.getLanguage()) + self.buffer.list.list.SetItem(self.buffer.list.get_selected(), 2, ts) + if self.session.settings['sound']['indicate_audio'] and utils.is_audio(tweet): + self.session.sound.play("audio.ogg") + if self.session.settings['sound']['indicate_img'] and utils.is_media(tweet): + self.session.sound.play("image.ogg") + + def clear_list(self): + dlg = commonMessageDialogs.clear_list() + if dlg == widgetUtils.YES: + self.session.db[self.name] = [] + self.buffer.list.clear() + + def auto_read(self, number_of_items): + if number_of_items == 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: + if self.session.settings["general"]["reverse_timelines"] == False: + tweet = self.session.db[self.name][-1] + else: + tweet = self.session.db[self.name][0] + output.speak(_(u"New direct message")) + output.speak(" ".join(self.compose_function(tweet, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session))) + elif number_of_items > 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: + output.speak(_(u"{0} new direct messages.").format(number_of_items,)) + + def open_in_browser(self, *args, **kwargs): + output.speak(_(u"This action is not supported in the buffer yet.")) + +class SentDirectMessagesBuffer(DirectMessagesBuffer): + + def __init__(self, *args, **kwargs): + super(SentDirectMessagesBuffer, self).__init__(*args, **kwargs) + if ("sent_direct_messages" in self.session.db) == False: + self.session.db["sent_direct_messages"] = [] + + def get_more_items(self): + output.speak(_(u"Getting more items cannot be done in this buffer. Use the direct messages buffer instead.")) + + def start_stream(self, *args, **kwargs): + pass + + def put_more_items(self, items): + if self.session.settings["general"]["reverse_timelines"] == True: + for i in items: + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) + self.buffer.list.insert_item(False, *tweet) + else: + for i in items: + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session) + self.buffer.list.insert_item(False, *tweet) \ No newline at end of file diff --git a/src/controller/buffers/twitter/list.py b/src/controller/buffers/twitter/list.py new file mode 100644 index 00000000..766c34fb --- /dev/null +++ b/src/controller/buffers/twitter/list.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +import platform +if platform.system() == "Windows": + from wxUI import dialogs, commonMessageDialogs +elif platform.system() == "Linux": + from gi.repository import Gtk + from gtkUI import dialogs, commonMessageDialogs +import widgetUtils +import logging +from tweepy.cursor import Cursor +from . import base + +log = logging.getLogger("controller.buffers.twitter.listBuffer") + +class ListBuffer(base.BaseBuffer): + def __init__(self, parent, function, name, sessionObject, account, sound=None, bufferType=None, list_id=None, *args, **kwargs): + super(ListBuffer, self).__init__(parent, function, name, sessionObject, account, sound=None, bufferType=None, *args, **kwargs) + self.users = [] + self.list_id = list_id + self.kwargs["list_id"] = list_id + + def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): + self.get_user_ids() + super(ListBuffer, self).start_stream(mandatory, play_sound, avoid_autoreading) + + def get_user_ids(self): + for i in Cursor(self.session.twitter.list_members, list_id=self.list_id, include_entities=False, skip_status=True, count=5000).items(): + if i.id not in self.users: + self.users.append(i.id) + + def remove_buffer(self, force=False): + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name[:-5] in self.session.settings["other_buffers"]["lists"]: + self.session.settings["other_buffers"]["lists"].remove(self.name[:-5]) + if self.name in self.session.db: + self.session.db.pop(self.name) + self.session.settings.write() + return True + elif dlg == widgetUtils.NO: + return False diff --git a/src/controller/buffers/twitter/people.py b/src/controller/buffers/twitter/people.py new file mode 100644 index 00000000..09d89c5d --- /dev/null +++ b/src/controller/buffers/twitter/people.py @@ -0,0 +1,259 @@ +# -*- coding: utf-8 -*- +import time +import platform +if platform.system() == "Windows": + from wxUI import commonMessageDialogs, menus + from controller import user +elif platform.system() == "Linux": + from gi.repository import Gtk + from gtkUI import dialogs, commonMessageDialogs +from controller import messages +import widgetUtils +import webbrowser +import output +import config +import logging +from mysc.thread_utils import call_threaded +from tweepy.error import TweepError +from pubsub import pub +from sessions.twitter import compose +from . import base + +log = logging.getLogger("controller.buffers.twitter.peopleBuffer") + +def _tweets_exist(function): + """ A decorator to execute a function only if the selected buffer contains at least one item.""" + def function_(self, *args, **kwargs): + if self.buffer.list.get_count() > 0: + function(self, *args, **kwargs) + return function_ + +class PeopleBuffer(base.BaseBuffer): + def __init__(self, parent, function, name, sessionObject, account, bufferType=None, *args, **kwargs): + super(PeopleBuffer, self).__init__(parent, function, name, sessionObject, account, bufferType="peoplePanel", *args, **kwargs) + log.debug("Initializing buffer %s, account %s" % (name, account,)) + self.compose_function = compose.compose_followers_list + log.debug("Compose_function: %s" % (self.compose_function,)) + self.get_tweet = self.get_right_tweet + self.url = self.interact + if "-followers" in self.name or "-friends" in self.name: + self.finished_timeline = False + # Add a compatibility layer for username based timelines from config. + # ToDo: Remove this in some new versions of the client, when user ID timelines become mandatory. + try: + int(self.kwargs["user_id"]) + except ValueError: + self.is_screen_name = True + self.kwargs["screen_name"] = self.kwargs["user_id"] + self.kwargs.pop("user_id") + + def remove_buffer(self, force=True): + if "-followers" in self.name: + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name[:-10] in self.session.settings["other_buffers"]["followers_timelines"]: + self.session.settings["other_buffers"]["followers_timelines"].remove(self.name[:-10]) + if self.name in self.session.db: + self.session.db.pop(self.name) + self.session.settings.write() + return True + elif dlg == widgetUtils.NO: + return False + elif "-friends" in self.name: + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name[:-8] in self.session.settings["other_buffers"]["friends_timelines"]: + self.session.settings["other_buffers"]["friends_timelines"].remove(self.name[:-8]) + if self.name in self.session.db: + self.session.db.pop(self.name) + self.session.settings.write() + return True + elif dlg == widgetUtils.NO: + return False + else: + output.speak(_(u"This buffer is not a timeline; it can't be deleted."), True) + return False + + def onFocus(self, ev): + pass + + def get_message(self): + return " ".join(self.compose_function(self.get_tweet(), self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session)) + + def delete_item(self): pass + + @_tweets_exist + def reply(self, *args, **kwargs): + tweet = self.get_right_tweet() + screen_name = tweet.screen_name + message = messages.reply(self.session, _(u"Mention"), _(u"Mention to %s") % (screen_name,), "@%s " % (screen_name,), [screen_name,]) + if message.message.get_response() == widgetUtils.OK: + if config.app["app-settings"]["remember_mention_and_longtweet"]: + config.app["app-settings"]["longtweet"] = message.message.long_tweet.GetValue() + config.app.write() + if message.image == None: + item = self.session.api_call(call_name="update_status", _sound="reply_send.ogg", status=message.message.get_text(), tweet_mode="extended") + if item != None: + pub.sendMessage("sent-tweet", data=item, user=self.session.db["user_name"]) + else: + call_threaded(self.session.api_call, call_name="update_status_with_media", _sound="reply_send.ogg", status=message.message.get_text(), media=message.file) + if hasattr(message.message, "destroy"): message.message.destroy() + + def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): + # starts stream every 3 minutes. + current_time = time.time() + if self.execution_time == 0 or current_time-self.execution_time >= 180 or mandatory==True: + self.execution_time = current_time + log.debug("Starting stream for %s buffer, %s account" % (self.name, self.account,)) + log.debug("args: %s, kwargs: %s" % (self.args, self.kwargs)) + try: + val = getattr(self.session.twitter, self.function)(return_cursors=True, count=self.session.settings["general"]["max_tweets_per_call"], *self.args, **self.kwargs) + if type(val) == tuple: + val, cursor = val + if type(cursor) == tuple: + cursor = cursor[1] + cursors = self.session.db["cursors"] + cursors[self.name] = cursor + self.session.db["cursors"] = cursors + results = [i for i in val] + val = results + val.reverse() + log.debug("Retrieved %d items from cursored search in function %s" % (len(val), self.function)) + except TweepError as e: + log.error("Error %s: %s" % (e.api_code, e.reason)) + return + number_of_items = self.session.order_people(self.name, val) + log.debug("Number of items retrieved: %d" % (number_of_items,)) + self.put_items_on_list(number_of_items) + if hasattr(self, "finished_timeline") and self.finished_timeline == False: + self.username = self.session.api_call("get_user", **self.kwargs).screen_name + self.finished_timeline = True + if number_of_items > 0 and self.sound != None and self.session.settings["sound"]["session_mute"] == False and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and play_sound == True: + self.session.sound.play(self.sound) + # Autoread settings + if avoid_autoreading == False and mandatory == True and number_of_items > 0 and self.name in self.session.settings["other_buffers"]["autoread_buffers"]: + self.auto_read(number_of_items) + return number_of_items + + def get_more_items(self): + try: + cursor = self.session.db["cursors"].get(self.name) + items = getattr(self.session.twitter, self.function)(return_cursors=True, users=True, cursor=cursor, count=self.session.settings["general"]["max_tweets_per_call"], *self.args, **self.kwargs) + if type(items) == tuple: + items, cursor = items + if type(cursor) == tuple: + cursor = cursor[1] + cursors = self.session.db["cursors"] + cursors[self.name] = cursor + self.session.db["cursors"] = cursors + results = [i for i in items] + items = results + log.debug("Retrieved %d items from cursored search in function %s" % (len(items), self.function)) + except TweepError as e: + log.error("Error %s: %s" % (e.api_code, e.reason)) + return + if items == None: + return + items_db = self.session.db[self.name] + for i in items: + if self.session.settings["general"]["reverse_timelines"] == False: + items_db.insert(0, i) + else: + items_db.append(i) + self.session.db[self.name] = items_db + selected = self.buffer.list.get_selected() + if self.session.settings["general"]["reverse_timelines"] == True: + for i in items: + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session) + self.buffer.list.insert_item(True, *tweet) + self.buffer.list.select_item(selected) + else: + for i in items: + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session) + self.buffer.list.insert_item(True, *tweet) + output.speak(_(u"%s items retrieved") % (len(items)), True) + + def put_items_on_list(self, number_of_items): + log.debug("The list contains %d items" % (self.buffer.list.get_count(),)) +# log.debug("Putting %d items on the list..." % (number_of_items,)) + if self.buffer.list.get_count() == 0: + for i in self.session.db[self.name]: + tweet = self.compose_function(i, self.session.db, self.session.settings["general"]["relative_times"], self.session) + self.buffer.list.insert_item(False, *tweet) + self.buffer.set_position(self.session.settings["general"]["reverse_timelines"]) +# self.buffer.set_list_position() + elif self.buffer.list.get_count() > 0: + if self.session.settings["general"]["reverse_timelines"] == False: + for i in self.session.db[self.name][len(self.session.db[self.name])-number_of_items:]: + tweet = self.compose_function(i, self.session.db) + self.buffer.list.insert_item(False, *tweet) + else: + items = self.session.db[self.name][0:number_of_items] + items.reverse() + for i in items: + tweet = self.compose_function(i, self.session.db) + self.buffer.list.insert_item(True, *tweet) + log.debug("now the list contains %d items" % (self.buffer.list.get_count(),)) + + def get_right_tweet(self): + tweet = self.session.db[self.name][self.buffer.list.get_selected()] + return tweet + + def add_new_item(self, item): + tweet = self.compose_function(item, self.session.db, self.session.settings["general"]["relative_times"], self.session) + if self.session.settings["general"]["reverse_timelines"] == False: + self.buffer.list.insert_item(False, *tweet) + else: + self.buffer.list.insert_item(True, *tweet) + if self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: + output.speak(" ".join(tweet)) + + def clear_list(self): + dlg = commonMessageDialogs.clear_list() + if dlg == widgetUtils.YES: + self.session.db[self.name] = [] + self.session.db["cursors"][self.name] = -1 + self.buffer.list.clear() + + def interact(self): + user.profileController(self.session, user=self.get_right_tweet().screen_name) + + def show_menu(self, ev, pos=0, *args, **kwargs): + menu = menus.peoplePanelMenu() + widgetUtils.connect_event(menu, widgetUtils.MENU, self.send_message, menuitem=menu.reply) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.user_actions, menuitem=menu.userActions) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.details, menuitem=menu.details) +# widgetUtils.connect_event(menu, widgetUtils.MENU, self.lists, menuitem=menu.lists) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.view, menuitem=menu.view) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.copy, menuitem=menu.copy) + if hasattr(menu, "openInBrowser"): + widgetUtils.connect_event(menu, widgetUtils.MENU, self.open_in_browser, menuitem=menu.openInBrowser) + if pos != 0: + self.buffer.PopupMenu(menu, pos) + else: + self.buffer.PopupMenu(menu, ev.GetPosition()) + + def details(self, *args, **kwargs): + pub.sendMessage("execute-action", action="user_details") + + def auto_read(self, number_of_items): + if number_of_items == 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: + if self.session.settings["general"]["reverse_timelines"] == False: + tweet = self.session.db[self.name][-1] + else: + tweet = self.session.db[self.name][0] + output.speak(" ".join(self.compose_function(tweet, self.session.db, self.session.settings["general"]["relative_times"], self.session.settings["general"]["show_screen_names"], self.session))) + elif number_of_items > 1 and self.name in self.session.settings["other_buffers"]["autoread_buffers"] and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and self.session.settings["sound"]["session_mute"] == False: + output.speak(_(u"{0} new followers.").format(number_of_items)) + + def open_in_browser(self, *args, **kwargs): + tweet = self.get_tweet() + output.speak(_(u"Opening item in web browser...")) + url = "https://twitter.com/{screen_name}".format(screen_name=tweet.screen_name) + webbrowser.open(url) \ No newline at end of file diff --git a/src/controller/buffers/twitter/search.py b/src/controller/buffers/twitter/search.py new file mode 100644 index 00000000..0b34a4a1 --- /dev/null +++ b/src/controller/buffers/twitter/search.py @@ -0,0 +1,113 @@ +# -*- coding: utf-8 -*- +import platform +if platform.system() == "Windows": + from wxUI import commonMessageDialogs +elif platform.system() == "Linux": + from gi.repository import Gtk + from gtkUI import commonMessageDialogs +import logging +from . import base, people + +log = logging.getLogger("controller.buffers.twitter.searchBuffer") + +class SearchBuffer(base.BaseBuffer): + + def remove_buffer(self, force=False): + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name[:-11] in self.session.settings["other_buffers"]["tweet_searches"]: + self.session.settings["other_buffers"]["tweet_searches"].remove(self.name[:-11]) + self.session.settings.write() + if self.name in self.session.db: + self.session.db.pop(self.name) + return True + elif dlg == widgetUtils.NO: + return False + +class SearchPeopleBuffer(people.PeopleBuffer): + """ This is identical to a normal peopleBufferController, except that uses the page parameter instead of a cursor.""" + def __init__(self, parent, function, name, sessionObject, account, bufferType="peoplePanel", *args, **kwargs): + super(SearchPeopleBuffer, self).__init__(parent, function, name, sessionObject, account, bufferType="peoplePanel", *args, **kwargs) + if ("page" in self.kwargs) == False: + self.page = 1 + else: + self.page = self.kwargs.pop("page") + + def get_more_items(self, *args, **kwargs): + # Add 1 to the page parameter, put it in kwargs and calls to get_more_items in the parent buffer. + self.page = self.page +1 + self.kwargs["page"] = self.page + super(SearchPeopleBuffer, self).get_more_items(*args, **kwargs) + # remove the parameter again to make sure start_stream won't fetch items for this page indefinitely. + self.kwargs.pop("page") + + def remove_buffer(self, force=False): + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name[:-11] in self.session.settings["other_buffers"]["tweet_searches"]: + self.session.settings["other_buffers"]["tweet_searches"].remove(self.name[:-11]) + self.session.settings.write() + if self.name in self.session.db: + self.session.db.pop(self.name) + return True + elif dlg == widgetUtils.NO: + return False + +class ConversationBuffer(SearchBuffer): + + def start_stream(self, start=False, mandatory=False, play_sound=True, avoid_autoreading=False): + # starts stream every 3 minutes. + current_time = time.time() + if self.execution_time == 0 or current_time-self.execution_time >= 180 or mandatory == True: + self.execution_time = current_time + if start == True: + self.statuses = [] + self.ids = [] + self.statuses.append(self.tweet) + self.ids.append(self.tweet.id) + tweet = self.tweet + if not hasattr(tweet, "in_reply_to_status_id"): + tweet.in_reply_to_status_id = None + while tweet.in_reply_to_status_id != None: + try: + tweet = self.session.twitter.get_status(id=tweet.in_reply_to_status_id, tweet_mode="extended") + except TweepError as err: + break + self.statuses.insert(0, tweet) + self.ids.append(tweet.id) + if tweet.in_reply_to_status_id == None: + self.kwargs["since_id"] = tweet.id + self.ids.append(tweet.id) + val2 = self.session.search(self.name, tweet_mode="extended", *self.args, **self.kwargs) + for i in val2: + if i.in_reply_to_status_id in self.ids: + self.statuses.append(i) + self.ids.append(i.id) + tweet = i + number_of_items = self.session.order_buffer(self.name, self.statuses) + log.debug("Number of items retrieved: %d" % (number_of_items,)) + self.put_items_on_list(number_of_items) + if number_of_items > 0 and self.sound != None and self.session.settings["sound"]["session_mute"] == False and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and play_sound == True: + self.session.sound.play(self.sound) + # Autoread settings + if avoid_autoreading == False and mandatory == True and number_of_items > 0 and self.name in self.session.settings["other_buffers"]["autoread_buffers"]: + self.auto_read(number_of_items) + return number_of_items + + def remove_buffer(self, force=False): + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name in self.session.db: + self.session.db.pop(self.name) + return True + elif dlg == widgetUtils.NO: + return False diff --git a/src/controller/buffers/twitter/trends.py b/src/controller/buffers/twitter/trends.py new file mode 100644 index 00000000..23049818 --- /dev/null +++ b/src/controller/buffers/twitter/trends.py @@ -0,0 +1,145 @@ +# -*- coding: utf-8 -*- +import time +import platform +if platform.system() == "Windows": + import wx + from wxUI import buffers, commonMessageDialogs, menus + from controller import user +elif platform.system() == "Linux": + from gi.repository import Gtk + from gtkUI import buffers, commonMessageDialogs +from controller import messages +import widgetUtils +import output +import logging +from mysc.thread_utils import call_threaded +from tweepy.error import TweepError +from pubsub import pub +from controller.buffers import base + +log = logging.getLogger("controller.buffers.twitter.trends") + +class TrendsBuffer(base.Buffer): + def __init__(self, parent, name, session, account, trendsFor, *args, **kwargs): + super(TrendsBuffer, self).__init__(parent=parent, session=session) + self.trendsFor = trendsFor + self.session = session + self.account = account + self.invisible = True + self.buffer = buffers.trendsPanel(parent, name) + self.buffer.account = account + self.type = self.buffer.type + self.bind_events() + self.sound = "trends_updated.ogg" + self.trends = [] + self.name = name + self.buffer.name = name + self.compose_function = self.compose_function_ + self.get_formatted_message = self.get_message + self.reply = self.search_topic + + def start_stream(self, mandatory=False, play_sound=True, avoid_autoreading=False): + # starts stream every 3 minutes. + current_time = time.time() + if self.execution_time == 0 or current_time-self.execution_time >= 180 or mandatory == True: + self.execution_time = current_time + try: + data = self.session.twitter.trends_place(id=self.trendsFor) + except TweepError as err: + log.error("Error %s: %s" % (err.api_code, err.reason)) + if not hasattr(self, "name_"): + self.name_ = data[0]["locations"][0]["name"] + self.trends = data[0]["trends"] + self.put_items_on_the_list() + if self.sound != None and self.session.settings["sound"]["session_mute"] == False and self.name not in self.session.settings["other_buffers"]["muted_buffers"] and play_sound == True: + self.session.sound.play(self.sound) + + def put_items_on_the_list(self): + selected_item = self.buffer.list.get_selected() + self.buffer.list.clear() + for i in self.trends: + tweet = self.compose_function(i) + self.buffer.list.insert_item(False, *tweet) + self.buffer.set_position(self.session.settings["general"]["reverse_timelines"]) + + def compose_function_(self, trend): + return [trend["name"]] + + def bind_events(self): + log.debug("Binding events...") + self.buffer.list.list.Bind(wx.EVT_CHAR_HOOK, self.get_event) + widgetUtils.connect_event(self.buffer, widgetUtils.BUTTON_PRESSED, self.tweet_about_this_trend, self.buffer.tweetTrendBtn) + widgetUtils.connect_event(self.buffer, widgetUtils.BUTTON_PRESSED, self.post_status, self.buffer.tweet) + widgetUtils.connect_event(self.buffer.list.list, wx.EVT_LIST_ITEM_RIGHT_CLICK, self.show_menu) + widgetUtils.connect_event(self.buffer.list.list, wx.EVT_LIST_KEY_DOWN, self.show_menu_by_key) + widgetUtils.connect_event(self.buffer, widgetUtils.BUTTON_PRESSED, self.search_topic, self.buffer.search_topic) + + def get_message(self): + return self.compose_function(self.trends[self.buffer.list.get_selected()])[0] + + def remove_buffer(self, force=False): + if force == False: + dlg = commonMessageDialogs.remove_buffer() + else: + dlg = widgetUtils.YES + if dlg == widgetUtils.YES: + if self.name[:-3] in self.session.settings["other_buffers"]["trending_topic_buffers"]: + self.session.settings["other_buffers"]["trending_topic_buffers"].remove(self.name[:-3]) + self.session.settings.write() + if self.name in self.session.db: + self.session.db.pop(self.name) + return True + elif dlg == widgetUtils.NO: + return False + + def url(self, *args, **kwargs): + self.tweet_about_this_trend() + + def search_topic(self, *args, **kwargs): + topic = self.trends[self.buffer.list.get_selected()]["name"] + pub.sendMessage("search", term=topic) + + def show_menu(self, ev, pos=0, *args, **kwargs): + menu = menus.trendsPanelMenu() + widgetUtils.connect_event(menu, widgetUtils.MENU, self.search_topic, menuitem=menu.search_topic) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.tweet_about_this_trend, menuitem=menu.tweetThisTrend) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.view, menuitem=menu.view) + widgetUtils.connect_event(menu, widgetUtils.MENU, self.copy, menuitem=menu.copy) + if pos != 0: + self.buffer.PopupMenu(menu, pos) + else: + self.buffer.PopupMenu(menu, ev.GetPosition()) + + def view(self, *args, **kwargs): + pub.sendMessage("execute-action", action="view_item") + + def copy(self, *args, **kwargs): + pub.sendMessage("execute-action", action="copy_to_clipboard") + + def tweet_about_this_trend(self, *args, **kwargs): + if self.buffer.list.get_count() == 0: return + title = _(u"Tweet") + caption = _(u"Write the tweet here") + tweet = messages.tweet(self.session, title, caption, self.get_message()+ " ") + tweet.message.set_cursor_at_end() + if tweet.message.get_response() == widgetUtils.OK: + text = tweet.message.get_text() + if len(text) > 280 and tweet.message.get("long_tweet") == True: + if tweet.image == None: + text = twishort.create_tweet(self.session.settings["twitter"]["user_key"], self.session.settings["twitter"]["user_secret"], text) + else: + text = twishort.create_tweet(self.session.settings["twitter"]["user_key"], self.session.settings["twitter"]["user_secret"], text, 1) + if tweet.image == None: + call_threaded(self.session.api_call, call_name="update_status", status=text) + else: + call_threaded(self.session.api_call, call_name="update_status_with_media", status=text, media=tweet.image) + if hasattr(tweet.message, "destroy"): tweet.message.destroy() + + def show_menu_by_key(self, ev): + if self.buffer.list.get_count() == 0: + return + if ev.GetKeyCode() == wx.WXK_WINDOWS_MENU: + self.show_menu(widgetUtils.MENU, pos=self.buffer.list.list.GetPosition()) + + def open_in_browser(self, *args, **kwargs): + output.speak(_(u"This action is not supported in the buffer, yet.")) \ No newline at end of file diff --git a/src/controller/mainController.py b/src/controller/mainController.py index b405b3b2..6f6e21ea 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -22,8 +22,7 @@ elif system == "Linux": from gtkUI import (view, commonMessageDialogs) from sessions.twitter import utils, compose from sessionmanager import manager, sessionManager - -from controller.buffers import baseBuffers, twitterBuffers +from controller import buffers from . import messages import sessions from sessions.twitter import session as session_ @@ -274,7 +273,7 @@ class Controller(object): def create_ignored_session_buffer(self, session): self.accounts.append(session.settings["twitter"]["user_name"]) - account = baseBuffers.accountPanel(self.view.nb, session.settings["twitter"]["user_name"], session.settings["twitter"]["user_name"], session.session_id) + account = buffers.base.AccountBuffer(self.view.nb, session.settings["twitter"]["user_name"], session.settings["twitter"]["user_name"], session.session_id) account.logged = False account.setup_account() self.buffers.append(account) @@ -294,96 +293,96 @@ class Controller(object): session.get_user_info() if createAccounts == True: self.accounts.append(session.db["user_name"]) - account = baseBuffers.accountPanel(self.view.nb, session.db["user_name"], session.db["user_name"], session.session_id) + account = buffers.base.AccountBuffer(self.view.nb, session.db["user_name"], session.db["user_name"], session.session_id) account.setup_account() self.buffers.append(account) self.view.add_buffer(account.buffer , name=session.db["user_name"]) for i in session.settings['general']['buffer_order']: if i == 'home': - home = twitterBuffers.baseBufferController(self.view.nb, "home_timeline", "home_timeline", session, session.db["user_name"], sound="tweet_received.ogg", tweet_mode="extended") + home = buffers.twitter.BaseBuffer(self.view.nb, "home_timeline", "home_timeline", session, session.db["user_name"], sound="tweet_received.ogg", tweet_mode="extended") self.buffers.append(home) self.view.insert_buffer(home.buffer, name=_(u"Home"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'mentions': - mentions = twitterBuffers.baseBufferController(self.view.nb, "mentions_timeline", "mentions", session, session.db["user_name"], sound="mention_received.ogg", tweet_mode="extended") + mentions = buffers.twitter.BaseBuffer(self.view.nb, "mentions_timeline", "mentions", session, session.db["user_name"], sound="mention_received.ogg", tweet_mode="extended") self.buffers.append(mentions) self.view.insert_buffer(mentions.buffer, name=_(u"Mentions"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'dm': - dm = twitterBuffers.directMessagesController(self.view.nb, "list_direct_messages", "direct_messages", session, session.db["user_name"], bufferType="dmPanel", compose_func="compose_direct_message", sound="dm_received.ogg") + dm = buffers.twitter.DirectMessagesBuffer(self.view.nb, "list_direct_messages", "direct_messages", session, session.db["user_name"], bufferType="dmPanel", compose_func="compose_direct_message", sound="dm_received.ogg") self.buffers.append(dm) self.view.insert_buffer(dm.buffer, name=_(u"Direct messages"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'sent_dm': - sent_dm = twitterBuffers.sentDirectMessagesController(self.view.nb, "", "sent_direct_messages", session, session.db["user_name"], bufferType="dmPanel", compose_func="compose_direct_message") + sent_dm = buffers.twitter.SentDirectMessagesBuffer(self.view.nb, "", "sent_direct_messages", session, session.db["user_name"], bufferType="dmPanel", compose_func="compose_direct_message") self.buffers.append(sent_dm) self.view.insert_buffer(sent_dm.buffer, name=_(u"Sent direct messages"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'sent_tweets': - sent_tweets = twitterBuffers.baseBufferController(self.view.nb, "user_timeline", "sent_tweets", session, session.db["user_name"], screen_name=session.db["user_name"], tweet_mode="extended") + sent_tweets = buffers.twitter.BaseBuffer(self.view.nb, "user_timeline", "sent_tweets", session, session.db["user_name"], screen_name=session.db["user_name"], tweet_mode="extended") self.buffers.append(sent_tweets) self.view.insert_buffer(sent_tweets.buffer, name=_(u"Sent tweets"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'favorites': - favourites = twitterBuffers.baseBufferController(self.view.nb, "favorites", "favourites", session, session.db["user_name"], sound="favourite.ogg", tweet_mode="extended") + favourites = buffers.twitter.BaseBuffer(self.view.nb, "favorites", "favourites", session, session.db["user_name"], sound="favourite.ogg", tweet_mode="extended") self.buffers.append(favourites) self.view.insert_buffer(favourites.buffer, name=_(u"Likes"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'followers': - followers = twitterBuffers.peopleBufferController(self.view.nb, "followers", "followers", session, session.db["user_name"], sound="update_followers.ogg", screen_name=session.db["user_name"]) + followers = buffers.twitter.PeopleBuffer(self.view.nb, "followers", "followers", session, session.db["user_name"], sound="update_followers.ogg", screen_name=session.db["user_name"]) self.buffers.append(followers) self.view.insert_buffer(followers.buffer, name=_(u"Followers"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'friends': - friends = twitterBuffers.peopleBufferController(self.view.nb, "friends", "friends", session, session.db["user_name"], screen_name=session.db["user_name"]) + friends = buffers.twitter.PeopleBuffer(self.view.nb, "friends", "friends", session, session.db["user_name"], screen_name=session.db["user_name"]) self.buffers.append(friends) self.view.insert_buffer(friends.buffer, name=_(u"Friends"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'blocks': - blocks = twitterBuffers.peopleBufferController(self.view.nb, "blocks", "blocked", session, session.db["user_name"]) + blocks = buffers.twitter.PeopleBuffer(self.view.nb, "blocks", "blocked", session, session.db["user_name"]) self.buffers.append(blocks) self.view.insert_buffer(blocks.buffer, name=_(u"Blocked users"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) elif i == 'muted': - muted = twitterBuffers.peopleBufferController(self.view.nb, "mutes", "muted", session, session.db["user_name"]) + muted = buffers.twitter.PeopleBuffer(self.view.nb, "mutes", "muted", session, session.db["user_name"]) 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"])) - timelines = baseBuffers.emptyPanel(self.view.nb, "timelines", session.db["user_name"]) + timelines = buffers.base.EmptyBuffer(self.view.nb, "timelines", session.db["user_name"]) 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"])) for i in session.settings["other_buffers"]["timelines"]: - tl = twitterBuffers.baseBufferController(self.view.nb, "user_timeline", "%s-timeline" % (i,), session, session.db["user_name"], sound="tweet_timeline.ogg", bufferType=None, user_id=i, tweet_mode="extended") + tl = buffers.twitter.BaseBuffer(self.view.nb, "user_timeline", "%s-timeline" % (i,), session, session.db["user_name"], sound="tweet_timeline.ogg", bufferType=None, user_id=i, tweet_mode="extended") self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Timeline for {}").format(i,), pos=self.view.search("timelines", session.db["user_name"])) - favs_timelines = baseBuffers.emptyPanel(self.view.nb, "favs_timelines", session.db["user_name"]) + favs_timelines = buffers.base.EmptyBuffer(self.view.nb, "favs_timelines", session.db["user_name"]) self.buffers.append(favs_timelines) self.view.insert_buffer(favs_timelines.buffer , name=_(u"Likes timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["favourites_timelines"]: - tl = twitterBuffers.baseBufferController(self.view.nb, "favorites", "%s-favorite" % (i,), session, session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, tweet_mode="extended") + tl = buffers.twitter.BaseBuffer(self.view.nb, "favorites", "%s-favorite" % (i,), session, session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=i, tweet_mode="extended") self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Likes for {}").format(i,), pos=self.view.search("favs_timelines", session.db["user_name"])) - followers_timelines = baseBuffers.emptyPanel(self.view.nb, "followers_timelines", session.db["user_name"]) + followers_timelines = buffers.base.EmptyBuffer(self.view.nb, "followers_timelines", session.db["user_name"]) self.buffers.append(followers_timelines) self.view.insert_buffer(followers_timelines.buffer , name=_(u"Followers' Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["followers_timelines"]: - tl = twitterBuffers.peopleBufferController(self.view.nb, "followers", "%s-followers" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) + tl = buffers.twitter.PeopleBuffer(self.view.nb, "followers", "%s-followers" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Followers for {}").format(i,), pos=self.view.search("followers_timelines", session.db["user_name"])) - friends_timelines = baseBuffers.emptyPanel(self.view.nb, "friends_timelines", session.db["user_name"]) + friends_timelines = buffers.base.EmptyBuffer(self.view.nb, "friends_timelines", session.db["user_name"]) self.buffers.append(friends_timelines) self.view.insert_buffer(friends_timelines.buffer , name=_(u"Friends' Timelines"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["friends_timelines"]: - tl = twitterBuffers.peopleBufferController(self.view.nb, "friends", "%s-friends" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) + tl = buffers.twitter.PeopleBuffer(self.view.nb, "friends", "%s-friends" % (i,), session, session.db["user_name"], sound="new_event.ogg", user_id=i) self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Friends for {}").format(i,), pos=self.view.search("friends_timelines", session.db["user_name"])) - lists = baseBuffers.emptyPanel(self.view.nb, "lists", session.db["user_name"]) + lists = buffers.base.EmptyBuffer(self.view.nb, "lists", session.db["user_name"]) self.buffers.append(lists) self.view.insert_buffer(lists.buffer , name=_(u"Lists"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["lists"]: - tl = twitterBuffers.listBufferController(self.view.nb, "list_timeline", "%s-list" % (i,), session, session.db["user_name"], bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), tweet_mode="extended") + tl = buffers.twitter.ListBuffer(self.view.nb, "list_timeline", "%s-list" % (i,), session, session.db["user_name"], bufferType=None, sound="list_tweet.ogg", list_id=utils.find_list(i, session.db["lists"]), tweet_mode="extended") session.lists.append(tl) self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"List for {}").format(i), pos=self.view.search("lists", session.db["user_name"])) - searches = baseBuffers.emptyPanel(self.view.nb, "searches", session.db["user_name"]) + searches = buffers.base.EmptyBuffer(self.view.nb, "searches", session.db["user_name"]) self.buffers.append(searches) self.view.insert_buffer(searches.buffer , name=_(u"Searches"), pos=self.view.search(session.db["user_name"], session.db["user_name"])) for i in session.settings["other_buffers"]["tweet_searches"]: - tl = twitterBuffers.searchBufferController(self.view.nb, "search", "%s-searchterm" % (i,), session, session.db["user_name"], bufferType="searchPanel", sound="search_updated.ogg", q=i, tweet_mode="extended") + tl = buffers.twitter.SearchBuffer(self.view.nb, "search", "%s-searchterm" % (i,), session, session.db["user_name"], bufferType="searchPanel", sound="search_updated.ogg", q=i, tweet_mode="extended") self.buffers.append(tl) self.view.insert_buffer(tl.buffer, name=_(u"Search for {}").format(i), pos=self.view.search("searches", session.db["user_name"])) for i in session.settings["other_buffers"]["trending_topic_buffers"]: - buffer = twitterBuffers.trendsBufferController(self.view.nb, "%s_tt" % (i,), session, session.db["user_name"], i, sound="trends_updated.ogg") + buffer = buffers.twitter.TrendsBuffer(self.view.nb, "%s_tt" % (i,), session, session.db["user_name"], i, sound="trends_updated.ogg") buffer.start_stream(play_sound=False) buffer.searchfunction = self.search self.buffers.append(buffer) @@ -431,12 +430,12 @@ class Controller(object): buffer.session.settings["other_buffers"]["tweet_searches"].append(term) buffer.session.settings.write() args = {"lang": dlg.get_language(), "result_type": dlg.get_result_type()} - search = twitterBuffers.searchBufferController(self.view.nb, "search", "%s-searchterm" % (term,), buffer.session, buffer.session.db["user_name"], bufferType="searchPanel", sound="search_updated.ogg", q=term, tweet_mode="extended", **args) + search = buffers.twitter.SearchBuffer(self.view.nb, "search", "%s-searchterm" % (term,), buffer.session, buffer.session.db["user_name"], bufferType="searchPanel", sound="search_updated.ogg", q=term, tweet_mode="extended", **args) else: log.error("A buffer for the %s search term is already created. You can't create a duplicate buffer." % (term,)) return elif dlg.get("users") == True: - search = twitterBuffers.searchPeopleBufferController(self.view.nb, "search_users", "%s-searchUser" % (term,), buffer.session, buffer.session.db["user_name"], bufferType=None, sound="search_updated.ogg", q=term) + search = buffers.twitter.SearchBuffer(self.view.nb, "search_users", "%s-searchUser" % (term,), buffer.session, buffer.session.db["user_name"], bufferType=None, sound="search_updated.ogg", q=term) search.start_stream(mandatory=True) pos=self.view.search("searches", buffer.session.db["user_name"]) self.insert_buffer(search, pos) @@ -849,7 +848,7 @@ class Controller(object): if usr.id_str in buff.session.settings["other_buffers"]["timelines"]: commonMessageDialogs.timeline_exist() return - tl = twitterBuffers.baseBufferController(self.view.nb, "user_timeline", "%s-timeline" % (usr.id_str,), buff.session, buff.session.db["user_name"], bufferType=None, sound="tweet_timeline.ogg", user_id=usr.id_str, tweet_mode="extended") + tl = buffers.twitter.BaseBuffer(self.view.nb, "user_timeline", "%s-timeline" % (usr.id_str,), buff.session, buff.session.db["user_name"], bufferType=None, sound="tweet_timeline.ogg", user_id=usr.id_str, tweet_mode="extended") try: tl.start_stream(play_sound=False) except ValueError: @@ -868,7 +867,7 @@ class Controller(object): if usr.id_str in buff.session.settings["other_buffers"]["favourites_timelines"]: commonMessageDialogs.timeline_exist() return - tl = twitterBuffers.baseBufferController(self.view.nb, "favorites", "%s-favorite" % (usr.id_str,), buff.session, buff.session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=usr.id_str, tweet_mode="extended") + tl = buffers.twitter.BaseBuffer(self.view.nb, "favorites", "%s-favorite" % (usr.id_str,), buff.session, buff.session.db["user_name"], bufferType=None, sound="favourites_timeline_updated.ogg", user_id=usr.id_str, tweet_mode="extended") try: tl.start_stream(play_sound=False) except ValueError: @@ -887,7 +886,7 @@ class Controller(object): if usr.id_str in buff.session.settings["other_buffers"]["followers_timelines"]: commonMessageDialogs.timeline_exist() return - tl = twitterBuffers.peopleBufferController(self.view.nb, "followers", "%s-followers" % (usr.id_str,), buff.session, buff.session.db["user_name"], sound="new_event.ogg", user_id=usr.id_str) + tl = buffers.twitter.PeopleBuffer(self.view.nb, "followers", "%s-followers" % (usr.id_str,), buff.session, buff.session.db["user_name"], sound="new_event.ogg", user_id=usr.id_str) try: tl.start_stream(play_sound=False) except ValueError: @@ -906,7 +905,7 @@ class Controller(object): if usr.id_str in buff.session.settings["other_buffers"]["friends_timelines"]: commonMessageDialogs.timeline_exist() return - tl = twitterBuffers.peopleBufferController(self.view.nb, "friends", "%s-friends" % (usr.id_str,), buff.session, buff.session.db["user_name"], sound="new_event.ogg", user_id=usr.id_str) + tl = buffers.twitter.PeopleBuffer(self.view.nb, "friends", "%s-friends" % (usr.id_str,), buff.session, buff.session.db["user_name"], sound="new_event.ogg", user_id=usr.id_str) try: tl.start_stream(play_sound=False) except ValueError: @@ -926,7 +925,7 @@ class Controller(object): buffer = self.get_current_buffer() id = buffer.get_right_tweet().id user = buffer.session.get_user(buffer.get_right_tweet().user).screen_name - search = twitterBuffers.conversationBufferController(self.view.nb, "search", "%s-searchterm" % (id,), buffer.session, buffer.session.db["user_name"], bufferType="searchPanel", sound="search_updated.ogg", since_id=id, q="@{0}".format(user,)) + search = buffers.twitter.ConversationBuffer(self.view.nb, "search", "%s-searchterm" % (id,), buffer.session, buffer.session.db["user_name"], bufferType="searchPanel", sound="search_updated.ogg", since_id=id, q="@{0}".format(user,)) search.tweet = buffer.get_right_tweet() search.start_stream(start=True) pos=self.view.search("searches", buffer.session.db["user_name"]) @@ -953,7 +952,7 @@ class Controller(object): if trends.dialog.get_response() == widgetUtils.OK: woeid = trends.get_woeid() if woeid in buff.session.settings["other_buffers"]["trending_topic_buffers"]: return - buffer = twitterBuffers.trendsBufferController(self.view.nb, "%s_tt" % (woeid,), buff.session, buff.account, woeid, sound="trends_updated.ogg") + buffer = buffers.twitter.TrendsBuffer(self.view.nb, "%s_tt" % (woeid,), buff.session, buff.account, woeid, sound="trends_updated.ogg") buffer.searchfunction = self.search pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"]) self.view.insert_buffer(buffer.buffer, name=_(u"Trending topics for %s") % (trends.get_string()), pos=pos) @@ -1364,41 +1363,37 @@ class Controller(object): buff = self.search_buffer("home_timeline", account) if create == True: if buffer == "favourites": - favourites = twitterBuffers.baseBufferController(self.view.nb, "favorites", "favourites", buff.session, buff.session.db["user_name"], tweet_mode="extended") + favourites = buffers.twitter.BaseBuffer(self.view.nb, "favorites", "favourites", buff.session, buff.session.db["user_name"], tweet_mode="extended") self.buffers.append(favourites) self.view.insert_buffer(favourites.buffer, name=_(u"Likes"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) favourites.start_stream(play_sound=False) if buffer == "followers": - followers = twitterBuffers.peopleBufferController(self.view.nb, "followers", "followers", buff.session, buff.session.db["user_name"], screen_name=buff.session.db["user_name"]) + followers = buffers.twitter.PeopleBuffer(self.view.nb, "followers", "followers", buff.session, buff.session.db["user_name"], screen_name=buff.session.db["user_name"]) self.buffers.append(followers) self.view.insert_buffer(followers.buffer, name=_(u"Followers"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) followers.start_stream(play_sound=False) elif buffer == "friends": - friends = twitterBuffers.peopleBufferController(self.view.nb, "friends", "friends", buff.session, buff.session.db["user_name"], screen_name=buff.session.db["user_name"]) + friends = buffers.twitter.PeopleBuffer(self.view.nb, "friends", "friends", buff.session, buff.session.db["user_name"], screen_name=buff.session.db["user_name"]) self.buffers.append(friends) self.view.insert_buffer(friends.buffer, name=_(u"Friends"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) friends.start_stream(play_sound=False) elif buffer == "blocked": - blocks = twitterBuffers.peopleBufferController(self.view.nb, "blocks", "blocked", buff.session, buff.session.db["user_name"]) + blocks = buffers.twitter.PeopleBuffer(self.view.nb, "blocks", "blocked", buff.session, buff.session.db["user_name"]) self.buffers.append(blocks) self.view.insert_buffer(blocks.buffer, name=_(u"Blocked users"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) blocks.start_stream(play_sound=False) elif buffer == "muted": - muted = twitterBuffers.peopleBufferController(self.view.nb, "mutes", "muted", buff.session, buff.session.db["user_name"]) + muted = buffers.twitter.PeopleBuffer(self.view.nb, "mutes", "muted", buff.session, buff.session.db["user_name"]) self.buffers.append(muted) self.view.insert_buffer(muted.buffer, name=_(u"Muted users"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) muted.start_stream(play_sound=False) - elif buffer == "events": - events = twitterBuffers.eventsBufferController(self.view.nb, "events", buff.session, buff.session.db["user_name"], bufferType="dmPanel", screen_name=buff.session.db["user_name"]) - self.buffers.append(events) - self.view.insert_buffer(events.buffer, name=_(u"Events"), pos=self.view.search(buff.session.db["user_name"], buff.session.db["user_name"])) elif create == False: self.destroy_buffer(buffer, buff.session.db["user_name"]) elif buffer == "list": if create in buff.session.settings["other_buffers"]["lists"]: output.speak(_(u"This list is already opened"), True) return - tl = twitterBuffers.listBufferController(self.view.nb, "list_timeline", create+"-list", buff.session, buff.session.db["user_name"], bufferType=None, list_id=utils.find_list(create, buff.session.db["lists"]), tweet_mode="extended") + tl = buffers.twitter.ListBuffer(self.view.nb, "list_timeline", create+"-list", buff.session, buff.session.db["user_name"], bufferType=None, list_id=utils.find_list(create, buff.session.db["lists"]), tweet_mode="extended") buff.session.lists.append(tl) pos=self.view.search("lists", buff.session.db["user_name"]) self.insert_buffer(tl, pos)