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