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

Commit 1456cff

Browse files
committed
More robust exception handling
1 parent 99f44a2 commit 1456cff

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

main.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,57 @@ def check_mentions():
3939
def process_tweets():
4040
global mention_queue
4141
while True:
42-
if not mention_queue.empty():
43-
tweet = mention_queue.get()
44-
update_id(tweet.id_str)
45-
thread = []
46-
users_to_names = {} # This will serve to link @display_names with usernames
47-
counter = Counter()
48-
current_tweet = tweet
49-
# 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
50-
while current_tweet.in_reply_to_status_id_str or hasattr(current_tweet, 'quoted_status_id_str'):
51-
current_tweet = api.get_status(current_tweet.in_reply_to_status_id_str or current_tweet.quoted_status_id_str, tweet_mode="extended")
52-
sanitize_tweet(current_tweet)
53-
users_to_names[current_tweet.author.screen_name] = current_tweet.author.name
54-
counter.update({current_tweet.author.screen_name: 1})
55-
thread.insert(0, Comment(current_tweet))
56-
if (len(users_to_names) >= 2):
57-
most_common = [users_to_names[t[0]] for t in counter.most_common()]
58-
characters = anim.get_characters(most_common)
59-
output_filename = tweet.id_str + '.mp4'
60-
anim.comments_to_scene(thread, characters, output_filename=output_filename)
61-
# Give some time to the other thread
62-
time.sleep(1)
63-
try:
64-
uploaded_media = api.media_upload(output_filename, media_category='TWEET_VIDEO')
65-
while (uploaded_media.processing_info['state'] == 'pending'):
66-
time.sleep(uploaded_media.processing_info['check_after_secs'])
67-
uploaded_media = api.get_media_upload_status(uploaded_media.media_id_string)
68-
api.update_status('@' + tweet.author.screen_name + ' ', in_reply_to_status_id=tweet.id_str, media_ids=[uploaded_media.media_id_string])
69-
except tweepy.error.TweepError as e:
42+
try:
43+
if not mention_queue.empty():
44+
tweet = mention_queue.get()
45+
update_id(tweet.id_str)
46+
thread = []
47+
users_to_names = {} # This will serve to link @display_names with usernames
48+
counter = Counter()
49+
current_tweet = tweet
50+
# 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
51+
while current_tweet.in_reply_to_status_id_str or hasattr(current_tweet, 'quoted_status_id_str'):
7052
try:
71-
api.update_status('@' + tweet.author.screen_name + ' ' + str(e), in_reply_to_status_id=tweet.id_str)
72-
except Exception as second_error:
73-
print (second_error)
74-
print(e)
75-
os.remove(output_filename)
53+
current_tweet = api.get_status(current_tweet.in_reply_to_status_id_str or current_tweet.quoted_status_id_str, tweet_mode="extended")
54+
sanitize_tweet(current_tweet)
55+
users_to_names[current_tweet.author.screen_name] = current_tweet.author.name
56+
counter.update({current_tweet.author.screen_name: 1})
57+
thread.insert(0, Comment(current_tweet))
58+
except tweepy.error.TweepError as e:
59+
try:
60+
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)
61+
except Exception as second_error:
62+
print (second_error)
63+
if (len(users_to_names) >= 2):
64+
most_common = [users_to_names[t[0]] for t in counter.most_common()]
65+
characters = anim.get_characters(most_common)
66+
output_filename = tweet.id_str + '.mp4'
67+
anim.comments_to_scene(thread, characters, output_filename=output_filename)
68+
# Give some time to the other thread
69+
time.sleep(1)
70+
try:
71+
uploaded_media = api.media_upload(output_filename, media_category='TWEET_VIDEO')
72+
while (uploaded_media.processing_info['state'] == 'pending'):
73+
time.sleep(uploaded_media.processing_info['check_after_secs'])
74+
uploaded_media = api.get_media_upload_status(uploaded_media.media_id_string)
75+
api.update_status('@' + tweet.author.screen_name + ' ', in_reply_to_status_id=tweet.id_str, media_ids=[uploaded_media.media_id_string])
76+
except tweepy.error.TweepError as e:
77+
try:
78+
api.update_status('@' + tweet.author.screen_name + ' ' + str(e), in_reply_to_status_id=tweet.id_str)
79+
except Exception as second_error:
80+
print (second_error)
81+
print(e)
82+
os.remove(output_filename)
83+
else:
84+
try:
85+
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)
86+
except Exception as e:
87+
print(e)
88+
time.sleep(2)
7689
else:
77-
try:
78-
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)
79-
except Exception as e:
80-
print(e)
81-
time.sleep(2)
82-
else:
83-
time.sleep(10)
90+
time.sleep(10)
91+
except Exception as e:
92+
print(e)
8493

8594

8695
################################## Main

0 commit comments

Comments
 (0)