Fixed some issues when determining the presence of long tweets based on entities present in objects

This commit is contained in:
Manuel Cortez 2021-01-04 11:15:27 -06:00
parent d0cc12ef5c
commit a37f339fea
2 changed files with 18 additions and 18 deletions

View File

@ -23,30 +23,30 @@ def is_long(tweet):
""" Check if the passed tweet contains a quote in its metadata. """ Check if the passed tweet contains a quote in its metadata.
tweet dict: a tweet dictionary. tweet dict: a tweet dictionary.
returns True if a quote is detected, False otherwise.""" returns True if a quote is detected, False otherwise."""
if "quoted_status_id" in tweet and "quoted_status" in tweet: if hasattr(tweet, "quoted_status_id") and hasattr(tweet, "quoted_status"):
return tweet["quoted_status_id"] return tweet.quoted_status_id
elif "retweeted_status" in tweet and "quoted_status_id" in tweet["retweeted_status"] and "quoted_status" in tweet["retweeted_status"]: elif hasattr(tweet, "retweeted_status") and hasattr(tweet, "quoted_status_id") and hasattr(tweet, "quoted_status"):
return tweet["retweeted_status"]["quoted_status_id"] return tweet.retweeted_status.quoted_status_id
return False return False
def clear_url(tweet): def clear_url(tweet):
""" Reads data from a quoted tweet and removes the link to the Status from the tweet's text. """ Reads data from a quoted tweet and removes the link to the Status from the tweet's text.
tweet dict: a tweet dictionary. tweet dict: a tweet dictionary.
returns a tweet dictionary without the URL to the status ID in its text to display.""" returns a tweet dictionary without the URL to the status ID in its text to display."""
if "retweeted_status" in tweet: if hasattr(tweet, "retweeted_status"):
if "full_text" in tweet["retweeted_status"]: if hasattr(tweet.retweeted_status, "full_text"):
value = "full_text" value = "full_text"
else: else:
value = "text" value = "text"
urls = utils.find_urls_in_text(tweet["retweeted_status"][value]) urls = utils.find_urls_in_text(getattr(tweet.retweeted_status, value))
try: tweet["message"] = tweet["message"].replace(urls[-1], "") try: tweet.message = tweet.message.replace(urls[-1], "")
except IndexError: pass except IndexError: pass
else: else:
if "full_text" in tweet: if hasattr(tweet, "full_text"):
value = "full_text" value = "full_text"
else: else:
value = "text" value = "text"
urls = utils.find_urls_in_text(tweet[value]) urls = utils.find_urls_in_text(getattr(tweet, value))
try: tweet["message"] = tweet["message"].replace(urls[-1], "") try: tweet.message = tweet.message.replace(urls[-1], "")
except IndexError: pass except IndexError: pass
return tweet return tweet

View File

@ -40,21 +40,21 @@ def is_long(tweet):
""" Check if the passed tweet is made with Twishort. """ Check if the passed tweet is made with Twishort.
returns True if is a long tweet, False otherwise.""" returns True if is a long tweet, False otherwise."""
long = False long = False
for url in range(0, len(tweet["entities"]["urls"])): for url in range(0, len(tweet.entities["urls"])):
try: try:
if tweet["entities"]["urls"][url] != None and "twishort.com" in tweet["entities"]["urls"][url]["expanded_url"]: if tweet.entities["urls"][url] != None and "twishort.com" in tweet.entities["urls"][url]["expanded_url"]:
long = get_twishort_uri(tweet["entities"]["urls"][url]["expanded_url"]) long = get_twishort_uri(tweet.entities["urls"][url]["expanded_url"])
except IndexError: except IndexError:
pass pass
# sometimes Twitter returns URL's with None objects, so let's take it. # sometimes Twitter returns URL's with None objects, so let's take it.
# see https://github.com/manuelcortez/TWBlue/issues/103 # see https://github.com/manuelcortez/TWBlue/issues/103
except TypeError: except TypeError:
pass pass
if long == False and "retweeted_status" in tweet: if long == False and hasattr(tweet, "retweeted_status"):
for url in range(0, len(tweet["retweeted_status"]["entities"]["urls"])): for url in range(0, len(tweet.retweeted_status.entities["urls"])):
try: try:
if tweet["retweeted_status"]["entities"]["urls"][url] != None and "twishort.com" in tweet["retweeted_status"]["entities"]["urls"][url]["expanded_url"]: if tweet.retweeted_status.entities["urls"][url] != None and "twishort.com" in tweet.retweeted_status.entities["urls"][url]["expanded_url"]:
long = get_twishort_uri(tweet["retweeted_status"]["entities"]["urls"][url]["expanded_url"]) long = get_twishort_uri(tweet.retweeted_status.entities["urls"][url]["expanded_url"])
except IndexError: except IndexError:
pass pass
except TypeError: except TypeError: