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

Music command recognition #20

Merged
merged 6 commits into from
Mar 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 64 additions & 47 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import queue
import threading
import random

import anim
from comment_list_brige import Comment
Expand Down Expand Up @@ -49,57 +50,73 @@ def process_tweets():
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 is not None) and (current_tweet.in_reply_to_status_id_str or hasattr(current_tweet, 'quoted_status_id_str')):
songs = ['PWR', 'JFA', 'TAT', 'rnd']

if 'music=' in tweet.full_text:
music_tweet = tweet.full_text.split('music=', 1)[1][:3]
else:
music_tweet = 'PWR'

if music_tweet == 'rnd':
music_tweet = random.choices(songs, [1, 1, 1, 0], k=1)[0]

if music_tweet not in songs: # If the music is written badly in the mention tweet, the bot will remind how to write it properly
try:
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:
api.update_status('@' + tweet.author.screen_name + ' The music argument format is incorrect. The posibilities are: \nPWR: Phoenix Wright Ace Attorney \nJFA: Justice for All \nTAT: Trials and Tribulations \nrnd: Random', in_reply_to_status_id=tweet.id_str)
except Exception as musicerror:
print(musicerror)
else:
# 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 is not None) and (current_tweet.in_reply_to_status_id_str or hasattr(current_tweet, 'quoted_status_id_str')):
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)
current_tweet = None
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=['xs'])
except tweepy.error.TweepError as e:
limit = False
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)
current_tweet = None
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, name_music = music_tweet, output_filename=output_filename)
# Give some time to the other thread
time.sleep(1)
try:
print(e.api_code)
if (e.api_code == 324):
print("I'm Rated-limited :(")
limit = True
mention_queue.put(tweet)
time.sleep(900)
except Exception as parsexc:
print(parsexc)
# 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=['xs'])
except tweepy.error.TweepError as e:
limit = False
try:
print(e.api_code)
if (e.api_code == 324):
print("I'm Rated-limited :(")
limit = True
mention_queue.put(tweet)
time.sleep(900)
except Exception as parsexc:
print(parsexc)
try:
if not limit:
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:
if not limit:
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(1)
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(1)
except Exception as e:
print(e)

Expand Down