Browse Source

Attempt to retry if YouTube's having issues again

main
fluffy 6 months ago
parent
commit
c8f41490f2
1 changed files with 12 additions and 8 deletions
  1. +12
    -8
      beatbot.py

+ 12
- 8
beatbot.py View File

@@ -3,6 +3,7 @@ import logging.handlers
import pickle
import random
import re
import time

import mastodon
import requests
@@ -39,16 +40,19 @@ def bot():
except Exception:
seen_vids = set()

query = requests.get(
f'https://www.youtube.com/results?search_query={search_term}')
video_ids = re.findall(r'watch\?v=([^"]{11})"', query.text)
for attempt in range(5):
query = requests.get(
f'https://www.youtube.com/results?search_query={search_term}')
video_ids = re.findall(r'watch\?v=([^"]{11})"', query.text)

LOGGER.info("%s: got %d videos (%d new)", search_term,
len(video_ids), len(set(video_ids) - seen_vids))
if video_ids:
LOGGER.info("%s: got %d videos (%d new)", search_term,
len(video_ids), len(set(video_ids) - seen_vids))
break

if query.status_code != 200 or not video_ids:
LOGGER.warning("YouTube search %s returned %d",
query.url, query.status_code)
LOGGER.warning("YouTube search %s returned %d (attempt %d)",
query.url, query.status_code, attempt + 1)
time.sleep(random.randrange(5,25))

yt_id = None



Loading…
Cancel
Save