From cbee57aa301ec63f0d9967d5caeacbfe6a5bb83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Tue, 29 Sep 2015 08:38:05 -0500 Subject: [PATCH] Quoted tweets support in buffers --- src/controller/buffersController.py | 23 ++++++++++++++++++++++- src/fixes/fix_win32com.py | 1 - src/long_tweets/tweets.py | 8 +++++++- src/sessionmanager/session.py | 28 +++++++++++++++++++++++++++- src/twitter/buffers/indibidual.py | 2 ++ src/twitter/buffers/stream.py | 1 + src/twitter/compose.py | 22 +++++++++++++++++++++- 7 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/controller/buffersController.py b/src/controller/buffersController.py index 64687bb1..b07d69d3 100644 --- a/src/controller/buffersController.py +++ b/src/controller/buffersController.py @@ -231,7 +231,8 @@ class baseBufferController(bufferController): return self.get_message() def get_message(self): - return " ".join(self.compose_function(self.get_right_tweet(), self.session.db, self.session.settings["general"]["relative_times"])) + tweet = self.get_right_tweet() + return " ".join(self.compose_function(tweet, self.session.db, self.session.settings["general"]["relative_times"])) def get_full_tweet(self): tweet = self.get_right_tweet() @@ -604,6 +605,26 @@ class baseBufferController(bufferController): user.profileController(session=self.session, user=dlg.get_user()) if hasattr(dlg, "destroy"): dlg.destroy() + def get_quoted_tweet(self, tweet): +# try: + quoted_tweet = self.session.twitter.twitter.show_status(id=tweet["id"]) + urls = utils.find_urls_in_text(quoted_tweet["text"]) + for url in range(0, len(urls)): + try: quoted_tweet["text"] = quoted_tweet["text"].replace(urls[url], quoted_tweet["entities"]["urls"][url]["expanded_url"]) + except IndexError: pass +# except TwythonError as e: +# utils.twitter_error(e) +# return + l = tweets.is_long(quoted_tweet) + id = tweets.get_id(l) +# try: + original_tweet = self.session.twitter.twitter.show_status(id=id) + urls = utils.find_urls_in_text(original_tweet["text"]) + for url in range(0, len(urls)): + try: original_tweet["text"] = original_tweet["text"].replace(urls[url], original_tweet["entities"]["urls"][url]["expanded_url"]) + except IndexError: pass + return compose.compose_quoted_tweet(quoted_tweet, original_tweet, self.session.db, self.session.settings["general"]["relative_times"]) + 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) diff --git a/src/fixes/fix_win32com.py b/src/fixes/fix_win32com.py index 04990eea..698d7d3c 100644 --- a/src/fixes/fix_win32com.py +++ b/src/fixes/fix_win32com.py @@ -1,6 +1,5 @@ import win32com.client - def fix(): if win32com.client.gencache.is_readonly == True: win32com.client.gencache.is_readonly = False diff --git a/src/long_tweets/tweets.py b/src/long_tweets/tweets.py index a2d97828..e254693a 100644 --- a/src/long_tweets/tweets.py +++ b/src/long_tweets/tweets.py @@ -16,6 +16,7 @@ # along with this program. If not, see . # ############################################################ +from twitter import utils def get_id(url): return url.split("/")[-1] @@ -25,4 +26,9 @@ def is_long(tweet): for url in range(0, len(tweet["entities"]["urls"])): if "twitter.com" in tweet["entities"]["urls"][url]["expanded_url"]: long = get_id(tweet["entities"]["urls"][url]["expanded_url"]) - return long \ No newline at end of file + return long + +def clear_url(tweet): + urls = utils.find_urls_in_text(tweet["text"]) + tweet["message"] = tweet["message"].replace(urls[-1]+".", "") + return tweet \ No newline at end of file diff --git a/src/sessionmanager/session.py b/src/sessionmanager/session.py index 64065301..66cc66c8 100644 --- a/src/sessionmanager/session.py +++ b/src/sessionmanager/session.py @@ -9,7 +9,7 @@ import output import time import sound import logging -from twitter import utils +from twitter import utils, compose from twython import TwythonError, TwythonRateLimitError, TwythonAuthError import config_utils import shelve @@ -18,6 +18,7 @@ import os from mysc.thread_utils import stream_threaded from pubsub import pub log = logging.getLogger("sessionmanager.session") +from long_tweets import tweets sessions = {} @@ -61,6 +62,7 @@ class Session(object): self.db[name] = [] for i in data: if utils.find_item(i["id"], self.db[name]) == None and utils.is_allowed(i, self.settings["twitter"]["ignored_clients"]) == True: + i = self.check_quoted_status(i) if self.settings["general"]["reverse_timelines"] == False: self.db[name].append(i) else: self.db[name].insert(0, i) num = num+1 @@ -413,3 +415,27 @@ class Session(object): os.remove(shelfname) except: pass + + def check_quoted_status(self, tweet): + status = tweets.is_long(tweet) + if status != False: + tweet["quoted"] = 1 + tweet = self.get_quoted_tweet(tweet) + return tweet + + + def get_quoted_tweet(self, tweet): + quoted_tweet = self.twitter.twitter.show_status(id=tweet["id"]) + urls = utils.find_urls_in_text(quoted_tweet["text"]) + for url in range(0, len(urls)): + try: quoted_tweet["text"] = quoted_tweet["text"].replace(urls[url], quoted_tweet["entities"]["urls"][url]["expanded_url"]) + except IndexError: pass + l = tweets.is_long(quoted_tweet) + id = tweets.get_id(l) + original_tweet = self.twitter.twitter.show_status(id=id) + urls = utils.find_urls_in_text(original_tweet["text"]) + for url in range(0, len(urls)): + try: original_tweet["text"] = original_tweet["text"].replace(urls[url], original_tweet["entities"]["urls"][url]["expanded_url"]) + except IndexError: pass + return compose.compose_quoted_tweet(quoted_tweet, original_tweet) + diff --git a/src/twitter/buffers/indibidual.py b/src/twitter/buffers/indibidual.py index 5c3d9781..9df12962 100644 --- a/src/twitter/buffers/indibidual.py +++ b/src/twitter/buffers/indibidual.py @@ -33,6 +33,7 @@ class timelinesStreamer(TwythonStreamer): if utils.find_item(data["id"], self.session.db["%s-timeline" % (i,)]) != None: log.error("duplicated tweet. Ignoring it...") return + data = self.session.check_quoted_status(data) if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s-timeline" % (i,)].append(data) else: self.session.db["%s-timeline" % (i,)].insert(0, data) pub.sendMessage("item-in-timeline", data= data, user= self.session.db["user_name"], who= i) @@ -41,6 +42,7 @@ class timelinesStreamer(TwythonStreamer): i.users.index(data["user"]["id"]) usr = data["in_reply_to_user_id"] if (usr != None and usr in self.friends) or data.has_key("retweeted_status"): + data = self.session.check_quoted_status(data) if self.session.settings["general"]["reverse_timelines"] == False: self.session.db["%s" % (i.name,)].append(data) else: self.session.db["%s" % (i,)].insert(0, data) pub.sendMessage("item-in-list", data= data, user= self.session.db["user_name"], where= i.name) diff --git a/src/twitter/buffers/stream.py b/src/twitter/buffers/stream.py index 936d5b2c..c33b0492 100644 --- a/src/twitter/buffers/stream.py +++ b/src/twitter/buffers/stream.py @@ -36,6 +36,7 @@ class streamer(TwythonStreamer): if utils.find_item(data["id"], self.session.db[place]) != None: log.error("duplicated tweet. Ignoring it...") return + data = self.session.check_quoted_status(data) if self.session.settings["general"]["reverse_timelines"] == False: self.session.db[place].append(data) else: diff --git a/src/twitter/compose.py b/src/twitter/compose.py index f9041abf..7356aac6 100644 --- a/src/twitter/compose.py +++ b/src/twitter/compose.py @@ -10,7 +10,7 @@ import languageHandler import arrow import logging import config -from long_tweets import twishort +from long_tweets import twishort, tweets log = logging.getLogger("compose") def StripChars(s): @@ -64,9 +64,29 @@ def compose_tweet(tweet, db, relative_times): except KeyError: pass except: text=oldtext + if tweet.has_key("message"): + text = tweet["message"] + return [user+", ", text, ts+", ", source] + tweet["text"] = text return [user+", ", tweet["text"], ts+", ", source] +def compose_quoted_tweet(quoted_tweet, original_tweet): + """ It receives a tweet and returns a list with the user, text for the tweet or message, date and the client where user is.""" + text = StripChars(quoted_tweet["text"]) + quoting_user = quoted_tweet["user"]["name"] + source = re.sub(r"(?s)<.*?>", "", quoted_tweet["source"]) + try: text = "rt @%s: %s" % (quoted_tweet["retweeted_status"]["user"]["screen_name"], StripChars(quoted_tweet["retweeted_status"]["text"])) + except KeyError: text = "%s" % (StripChars(quoted_tweet["text"])) + if text[-1] in chars: text=text+"." + original_user = original_tweet["user"]["screen_name"] + original_text = StripChars(original_tweet["text"]) + try: original_text = "rt @%s: %s" % (original_tweet["retweeted_status"]["user"]["screen_name"], StripChars(original_tweet["retweeted_status"]["text"])) + except KeyError: original_text = "%s" % (StripChars(original_tweet["text"])) + quoted_tweet["message"] = _(u"{0}. Quoted tweet from @{1}: {2}".format( quoted_tweet["text"], original_user, original_text)) + quoted_tweet = tweets.clear_url(quoted_tweet) + return quoted_tweet + def compose_followers_list(tweet, db, relative_times=True): if system == "Windows": original_date = arrow.get(tweet["created_at"], "ddd MMM D H:m:s Z YYYY", locale="en")