Compare commits
1 Commits
a0cabc329e
...
418085c7ac
Author | SHA1 | Date |
---|---|---|
fluffy | 418085c7ac |
|
@ -1 +1,2 @@
|
||||||
token.secret
|
token.secret
|
||||||
|
seen-vids.dat
|
||||||
|
|
34
beatbot.py
34
beatbot.py
|
@ -2,6 +2,7 @@ import mastodon
|
||||||
import random
|
import random
|
||||||
import requests
|
import requests
|
||||||
import re
|
import re
|
||||||
|
import pickle
|
||||||
|
|
||||||
def randline(fname):
|
def randline(fname):
|
||||||
with open(fname) as afile:
|
with open(fname) as afile:
|
||||||
|
@ -19,11 +20,36 @@ def bot():
|
||||||
search_term = f'+lofi {genre} +beats {verb1} {verb2}'
|
search_term = f'+lofi {genre} +beats {verb1} {verb2}'
|
||||||
text = f"lo-fi {genre} beats to {verb1} and {verb2} to"
|
text = f"lo-fi {genre} beats to {verb1} and {verb2} to"
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open('seen-vids.dat', 'rb') as seen:
|
||||||
|
seen_vids = pickle.load(seen)
|
||||||
|
except Exception:
|
||||||
|
seen_vids = set()
|
||||||
|
|
||||||
query = requests.get(f'https://www.youtube.com/results?search_query={search_term}')
|
query = requests.get(f'https://www.youtube.com/results?search_query={search_term}')
|
||||||
vids = re.findall(r'watch\?v=([^"]{11})"', query.text)
|
video_ids = re.findall(r'watch\?v=([^"]{11})"', query.text)
|
||||||
if vids:
|
|
||||||
print(f'{query.url} : {len(vids)} results')
|
yt_id = None
|
||||||
text += f'\n\nhttps://youtube.com/watch?v={random.choice(vids)}'
|
|
||||||
|
random.shuffle(video_ids)
|
||||||
|
|
||||||
|
# try to find a random video we haven't used before
|
||||||
|
for vid in video_ids:
|
||||||
|
if vid not in seen_vids:
|
||||||
|
yt_id = vid
|
||||||
|
break
|
||||||
|
|
||||||
|
if not yt_id and video_ids:
|
||||||
|
# nothing new, so just select a random one (list is already shuffled)
|
||||||
|
yt_id = video_ids[0]
|
||||||
|
|
||||||
|
if yt_id:
|
||||||
|
text += f'\n\nhttps://youtube.com/watch?v={yt_id}'
|
||||||
|
|
||||||
|
# remember that we used it already
|
||||||
|
seen_vids.add(yt_id)
|
||||||
|
with open('seen-vids.dat', 'wb') as seen:
|
||||||
|
pickle.dump(seen_vids, seen)
|
||||||
|
|
||||||
print(text)
|
print(text)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue