Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
More robust exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisMayo committed Mar 6, 2021
1 parent 99f44a2 commit 1456cff
Showing 1 changed file with 49 additions and 40 deletions.
89 changes: 49 additions & 40 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,57 @@ def check_mentions():
def process_tweets():
global mention_queue
while True:
if not mention_queue.empty():
tweet = mention_queue.get()
update_id(tweet.id_str)
thread = []
users_to_names = {} # This will serve to link @display_names with usernames
counter = Counter()
current_tweet = tweet
# In the case of Quotes I have to check for its presence instead of whether its None because Twitter API designers felt creative that week
while current_tweet.in_reply_to_status_id_str or hasattr(current_tweet, 'quoted_status_id_str'):
current_tweet = api.get_status(current_tweet.in_reply_to_status_id_str or current_tweet.quoted_status_id_str, tweet_mode="extended")
sanitize_tweet(current_tweet)
users_to_names[current_tweet.author.screen_name] = current_tweet.author.name
counter.update({current_tweet.author.screen_name: 1})
thread.insert(0, Comment(current_tweet))
if (len(users_to_names) >= 2):
most_common = [users_to_names[t[0]] for t in counter.most_common()]
characters = anim.get_characters(most_common)
output_filename = tweet.id_str + '.mp4'
anim.comments_to_scene(thread, characters, output_filename=output_filename)
# Give some time to the other thread
time.sleep(1)
try:
uploaded_media = api.media_upload(output_filename, media_category='TWEET_VIDEO')
while (uploaded_media.processing_info['state'] == 'pending'):
time.sleep(uploaded_media.processing_info['check_after_secs'])
uploaded_media = api.get_media_upload_status(uploaded_media.media_id_string)
api.update_status('@' + tweet.author.screen_name + ' ', in_reply_to_status_id=tweet.id_str, media_ids=[uploaded_media.media_id_string])
except tweepy.error.TweepError as e:
try:
if not mention_queue.empty():
tweet = mention_queue.get()
update_id(tweet.id_str)
thread = []
users_to_names = {} # This will serve to link @display_names with usernames
counter = Counter()
current_tweet = tweet
# In the case of Quotes I have to check for its presence instead of whether its None because Twitter API designers felt creative that week
while current_tweet.in_reply_to_status_id_str or hasattr(current_tweet, 'quoted_status_id_str'):
try:
api.update_status('@' + tweet.author.screen_name + ' ' + str(e), in_reply_to_status_id=tweet.id_str)
except Exception as second_error:
print (second_error)
print(e)
os.remove(output_filename)
current_tweet = api.get_status(current_tweet.in_reply_to_status_id_str or current_tweet.quoted_status_id_str, tweet_mode="extended")
sanitize_tweet(current_tweet)
users_to_names[current_tweet.author.screen_name] = current_tweet.author.name
counter.update({current_tweet.author.screen_name: 1})
thread.insert(0, Comment(current_tweet))
except tweepy.error.TweepError as e:
try:
api.update_status('@' + tweet.author.screen_name + ' I\'m sorry. I wasn\'t able to retrieve the full thread. Deleted tweets or private accounts may exist', in_reply_to_status_id=tweet.id_str)
except Exception as second_error:
print (second_error)
if (len(users_to_names) >= 2):
most_common = [users_to_names[t[0]] for t in counter.most_common()]
characters = anim.get_characters(most_common)
output_filename = tweet.id_str + '.mp4'
anim.comments_to_scene(thread, characters, output_filename=output_filename)
# Give some time to the other thread
time.sleep(1)
try:
uploaded_media = api.media_upload(output_filename, media_category='TWEET_VIDEO')
while (uploaded_media.processing_info['state'] == 'pending'):
time.sleep(uploaded_media.processing_info['check_after_secs'])
uploaded_media = api.get_media_upload_status(uploaded_media.media_id_string)
api.update_status('@' + tweet.author.screen_name + ' ', in_reply_to_status_id=tweet.id_str, media_ids=[uploaded_media.media_id_string])
except tweepy.error.TweepError as e:
try:
api.update_status('@' + tweet.author.screen_name + ' ' + str(e), in_reply_to_status_id=tweet.id_str)
except Exception as second_error:
print (second_error)
print(e)
os.remove(output_filename)
else:
try:
api.update_status('@' + tweet.author.screen_name + " There should be at least two people in the conversation", in_reply_to_status_id=tweet.id_str)
except Exception as e:
print(e)
time.sleep(2)
else:
try:
api.update_status('@' + tweet.author.screen_name + " There should be at least two people in the conversation", in_reply_to_status_id=tweet.id_str)
except Exception as e:
print(e)
time.sleep(2)
else:
time.sleep(10)
time.sleep(10)
except Exception as e:
print(e)


################################## Main
Expand Down

0 comments on commit 1456cff

Please sign in to comment.