Quoted tweets support in buffers

This commit is contained in:
Manuel Cortez 2015-09-29 08:38:05 -05:00
parent fd9e4dc05d
commit cbee57aa30
7 changed files with 80 additions and 5 deletions

View File

@ -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)

View File

@ -1,6 +1,5 @@
import win32com.client
def fix():
if win32com.client.gencache.is_readonly == True:
win32com.client.gencache.is_readonly = False

View File

@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
############################################################
from twitter import utils
def get_id(url):
return url.split("/")[-1]
@ -26,3 +27,8 @@ def is_long(tweet):
if "twitter.com" in tweet["entities"]["urls"][url]["expanded_url"]:
long = get_id(tweet["entities"]["urls"][url]["expanded_url"])
return long
def clear_url(tweet):
urls = utils.find_urls_in_text(tweet["text"])
tweet["message"] = tweet["message"].replace(urls[-1]+".", "")
return tweet

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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")