]> git.gir.st - subscriptionfeed.git/blob - app/dangerous/lib.py
clean up v1/v2 channel protobuf handling
[subscriptionfeed.git] / app / dangerous / lib.py
1 import requests
2 from datetime import datetime, timezone
3
4 def fetch_searchresults(q=None, page=1, sp=None):
5 today = datetime.now(timezone.utc).strftime("%Y%m%d")
6 r = requests.get(f"https://www.youtube.com/results", {
7 '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
8 'pbj': 1, # makes youtube return a json-response
9 'hl': 'en', #'en_US',
10 'page': page,
11 'sp': sp,
12 }, headers={
13 'x-youtube-client-name': '1',
14 '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)
15 })
16 if not r.ok:
17 return None
18
19 return r.json()
20
21 def fetch_ajax(params):
22 """
23 fetch data using a continuation protobuf
24 """
25 # TODO: handle auto_generated!
26 today = datetime.now(timezone.utc).strftime("%Y%m%d")
27
28 r = requests.get(f"https://www.youtube.com/browse_ajax", {
29 'continuation': params,
30 'gl': 'US',
31 'hl': 'en',
32 }, headers={
33 'x-youtube-client-name': '1',
34 'x-youtube-client-version': f'2.{today}.01.01', # see fetch_searchresults()
35 })
36
37 if not r.ok:
38 return None
39
40 return r.json()
Imprint / Impressum