From 5b2cbd437d453acf01097fd15034d59ee32a49fb Mon Sep 17 00:00:00 2001 From: girst Date: Sun, 4 Oct 2020 15:30:57 +0200 Subject: [PATCH] fix JSONDecodeError on search for real this time example of a broken response: https://paste.ubuntu.com/p/NbpSMWKQcX/ --- app/browse/lib.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/browse/lib.py b/app/browse/lib.py index f7017a3..139fed8 100644 --- a/app/browse/lib.py +++ b/app/browse/lib.py @@ -2,6 +2,7 @@ import requests from datetime import datetime, timezone def fetch_searchresults(q=None, page=1, sp=None): + for _ in range(2): today = datetime.now(timezone.utc).strftime("%Y%m%d") r = requests.get(f"https://www.youtube.com/results", { 'q': q, # Note: if we use '?search_query=' (as yt.com does), we can't use '&page=', but have to use a continuation url that requires an api key @@ -13,9 +14,15 @@ def fetch_searchresults(q=None, page=1, sp=None): 'x-youtube-client-name': '1', 'x-youtube-client-version': f'2.{today}.01.01', # the version is parsed as a date, and if it's invalid (e.g. month>12 or even feb>=30), youtube throws an encrypted stacktrace :D (but any random date >= 20160323 as of 20200802 works (even year 3000) }) - if not r.ok or not r.text: + if not r.ok: return None + # Sometimes, youtube throws an exception after the response already begun. + # So the status code is 200, begins with JSON and switches to HTML half way + # through. WTF?! (This should be "fixed" by retrying, though) + if r.text.endswith(""): + continue # will return None once we break out of the loop + return r.json() def fetch_ajax(params): -- 2.39.3