Improvements in quoted tweets handling. Closes #190

This commit is contained in:
Manuel Cortez 2017-11-29 10:55:42 -06:00
parent a0e41a4450
commit 4d1732b3aa
3 changed files with 36 additions and 14 deletions

View File

@ -19,8 +19,10 @@
from twitter import utils from twitter import utils
def is_long(tweet): def is_long(tweet):
if tweet.has_key("quoted_status_id") and tweet["quoted_status_id"] != None: if tweet.has_key("quoted_status_id") and tweet.has_key("quoted_status"):
return tweet["quoted_status_id"] return tweet["quoted_status_id"]
elif tweet.has_key("retweeted_status") and tweet["retweeted_status"].has_key("quoted_status_id") and tweet["retweeted_status"].has_key("quoted_status"):
return tweet["retweeted_status"]["quoted_status_id"]
return False return False
def clear_url(tweet): def clear_url(tweet):
@ -33,6 +35,8 @@ def clear_url(tweet):
except IndexError: pass except IndexError: pass
try: try:
tweet["entities"]["urls"].remove(tweet["entities"]["urls"][-1]) tweet["entities"]["urls"].remove(tweet["entities"]["urls"][-1])
except ValueError: except IndexError:
tweet["retweeted_status"]["entities"]["urls"].remove(tweet["retweeted_status"]["entities"]["urls"][-1])
else:
pass pass
return tweet return tweet

View File

@ -75,8 +75,7 @@ class Session(object):
log.error("Ignoring an older tweet... Last id: {0}, tweet id: {1}".format(last_id, i["id"])) log.error("Ignoring an older tweet... Last id: {0}, tweet id: {1}".format(last_id, i["id"]))
continue continue
if utils.find_item(i["id"], self.db[name]) == None and utils.is_allowed(i, self.settings, name) == True: if utils.find_item(i["id"], self.db[name]) == None and utils.is_allowed(i, self.settings, name) == True:
try: i = self.check_quoted_status(i) i = self.check_quoted_status(i)
except: pass
i = self.check_long_tweet(i) i = self.check_long_tweet(i)
if i == False: continue if i == False: continue
if self.settings["general"]["reverse_timelines"] == False: self.db[name].append(i) if self.settings["general"]["reverse_timelines"] == False: self.db[name].append(i)
@ -367,7 +366,6 @@ class Session(object):
def add_friends(self): def add_friends(self):
try: try:
# print "setting friends"
self.timelinesStream.set_friends(self.main_stream.friends) self.timelinesStream.set_friends(self.main_stream.friends)
except AttributeError: except AttributeError:
pass pass
@ -463,7 +461,8 @@ class Session(object):
def check_quoted_status(self, tweet): def check_quoted_status(self, tweet):
status = tweets.is_long(tweet) status = tweets.is_long(tweet)
if status != False and config.app["app-settings"]["handle_longtweets"]: if status != False and config.app["app-settings"]["handle_longtweets"]:
tweet = self.get_quoted_tweet(tweet) quoted_tweet = self.get_quoted_tweet(tweet)
return quoted_tweet
return tweet return tweet
def get_quoted_tweet(self, tweet): def get_quoted_tweet(self, tweet):
@ -476,9 +475,12 @@ class Session(object):
for url in range(0, len(urls)): for url in range(0, len(urls)):
try: quoted_tweet[value] = quoted_tweet[value].replace(urls[url], quoted_tweet["entities"]["urls"][url]["expanded_url"]) try: quoted_tweet[value] = quoted_tweet[value].replace(urls[url], quoted_tweet["entities"]["urls"][url]["expanded_url"])
except IndexError: pass except IndexError: pass
id = tweets.is_long(quoted_tweet) if quoted_tweet.has_key("quoted_status"):
try: original_tweet = self.twitter.twitter.show_status(id=id, tweet_mode="extended") original_tweet = quoted_tweet["quoted_status"]
except: return quoted_tweet elif quoted_tweet.has_key("retweeted_status") and quoted_tweet["retweeted_status"].has_key("quoted_status"):
original_tweet = quoted_tweet["retweeted_status"]["quoted_status"]
else:
return quoted_tweet
original_tweet = self.check_long_tweet(original_tweet) original_tweet = self.check_long_tweet(original_tweet)
urls = utils.find_urls_in_text(original_tweet["full_text"]) urls = utils.find_urls_in_text(original_tweet["full_text"])
for url in range(0, len(urls)): for url in range(0, len(urls)):

View File

@ -23,17 +23,33 @@ def find_urls_in_text(text):
def find_urls (tweet): def find_urls (tweet):
urls = [] urls = []
# Let's add URLS from tweet entities.
for i in tweet["entities"]["urls"]:
if i["expanded_url"] not in urls:
urls.append(i["expanded_url"])
if tweet.has_key("quoted_status"):
for i in tweet["quoted_status"]["entities"]["urls"]:
if i["expanded_url"] not in urls:
urls.append(i["expanded_url"])
if tweet.has_key("retweeted_status"):
for i in tweet["retweeted_status"]["entities"]["urls"]:
if i["expanded_url"] not in urls:
urls.append(i["expanded_url"])
if tweet["retweeted_status"].has_key("quoted_status"):
for i in tweet["retweeted_status"]["quoted_status"]["entities"]["urls"]:
if i["expanded_url"] not in urls:
urls.append(i["expanded_url"])
if tweet.has_key("message"): if tweet.has_key("message"):
i = "message" i = "message"
elif tweet.has_key("full_text"): elif tweet.has_key("full_text"):
i = "full_text" i = "full_text"
else: else:
i = "text" i = "text"
shorten_urls = find_urls_in_text(tweet[i]) extracted_urls = find_urls_in_text(tweet[i])
for url in range(0, len(shorten_urls)): # Don't include t.co links (mostly they are photos or shortened versions of already added URLS).
try: for i in extracted_urls:
urls.append(tweet["entities"]["urls"][url]["expanded_url"]) if i not in urls and "https://t.co" not in i:
except: pass urls.append(i)
return urls return urls
def find_item(id, listItem): def find_item(id, listItem):