-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
179 lines (141 loc) · 5.03 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Modules
import tweepy, os, sys, requests
from dotenv import load_dotenv
from time import sleep
from acrcloud.recognizer import ACRCloudRecognizer
def get_last_seen_id():
text_file = open('last_seen_id.txt', 'r')
last_seen_id = int(text_file.read().strip())
text_file.close()
return last_seen_id
def set_last_seen_id(last_seen_id_str):
text_file = open('last_seen_id.txt', 'w')
text_file.write(last_seen_id_str)
text_file.close()
return
def save_video(video_link):
req = requests.get(video_link, allow_redirects=True)
video_file = open('video.mp4', 'wb')
video_file.write(req.content)
video_file.close()
return
def video_song_finder():
# API vars
acr_config = {
'host': os.getenv('ACR_HOST_NAME'),
'access_key': os.getenv('ACR_ACCESS_KEY'),
'access_secret': os.getenv('ACR_ACCESS_SECRET'),
'timeout': 10
}
# ACR object
acr = ACRCloudRecognizer(acr_config)
# recognize, 0s offset from start
result = eval(acr.recognize_by_file('video.mp4', 0))
if result['status']['msg'] == 'Success':
# Recognized the song
print("success")
# extract the metadata/details
metadata = result['metadata']
all_finds = metadata['music']
best_find = all_finds[0]
title = best_find['title']
artist = best_find['artists'][0]['name']
# if more than artist then we need to append
if len(best_find['artists']) > 1:
for index in range(1, len(best_find['artists'])):
artist += ", " + best_find['artists'][index]['name']
# get youtube video link if possible
youtube = ""
try:
for listing in all_finds:
if 'youtube' in listing['external_metadata']:
youtube = 'https://www.youtube.com/watch?v=' + listing['external_metadata']['youtube']['vid']
except:
youtube = ""
# get spotify link if possible
spotify = ""
try:
for listing in all_finds:
if 'spotify' in listing['external_metadata']:
spotify = 'https://open.spotify.com/track/' + listing['external_metadata']['spotify']['track']['id']
except:
spotify = ""
# create tweet
tweet = f"Yay, we found it!\nTitle: {title}\n"
if len(best_find['artists']) > 1:
tweet += f"Artists: {artist}\n"
else:
tweet += f"Artist: {artist}\n"
if youtube != "":
tweet += f"Youtube: {youtube}\n"
if spotify != "":
tweet += f"Spotify: {spotify}"
else:
# Failed to recognize
print("Failure")
tweet = "Cannot recognize this song :("
# return tweet
return tweet
def response(api):
print("in response")
# retrieve last seen id
last_seen_id = get_last_seen_id()
# get all the mentions after the last seen tweet
mentions_list = api.mentions_timeline(since_id=last_seen_id, tweet_mode='extended')
# loop through all mentions (oldest first)
for mention in reversed(mentions_list):
# get the tweet id for current new tweet
last_seen_id = mention.id
print("last seen id: ", last_seen_id)
# store this new id in text file for next iteration
set_last_seen_id(mention.id_str)
# get the tweet id of the base tweet
base_tweet_id = mention.in_reply_to_status_id
# get the base tweet using the base_tweet_id
base_tweet = api.get_status(base_tweet_id)
# get the video link
try:
variants = base_tweet._json['extended_entities']['media'][0]['video_info']['variants']
for index in range(len(variants)):
if variants[index]['content_type'] == 'video/mp4':
video_link = base_tweet._json['extended_entities']['media'][0]['video_info']['variants'][index]['url']
break
print(video_link)
except:
print("exception no video") #? probably another mention as reply/thanks
continue
# save the video for use
save_video(video_link)
# Perform song recognition
tweet = video_song_finder()
tweet = f"@{mention.user.screen_name} " + tweet
# post tweet
api.update_status(tweet, mention.id)
def main():
# Load all environment variables from the .env file
load_dotenv()
# create last_seen_id.txt file if not exists
if os.path.exists("last_seen_id.txt") == False:
#! on crash please put last tweet id from heroku
#! need to be fixed
#? or during maintenence put it manually before deploying again
temporary = open("last_seen_id.txt", "w+")
temporary.write("1269995043655503874")
temporary.close()
# Store all API keys in variables
TWITTER_CONSUMER_KEY = os.getenv('TWITTER_CONSUMER_KEY')
TWITTER_CONSUMER_SECRET = os.getenv('TWITTER_CONSUMER_SECRET')
TWITTER_ACCESS_KEY = os.getenv('TWITTER_ACCESS_KEY')
TWITTER_ACCESS_SECRET = os.getenv('TWITTER_ACCESS_SECRET')
# Authorization to consumer key and consumer secret
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
# Access to user's access key and access secret
auth.set_access_token(TWITTER_ACCESS_KEY, TWITTER_ACCESS_SECRET)
# Calling api
api = tweepy.API(auth)
# Keep running and refresh every 30 seconds
while True:
response(api)
sleep(30)
if __name__ == '__main__':
main()