]>
git.gir.st - subscriptionfeed.git/blob - app/browse/lib.py
3 from datetime
import datetime
, timezone
5 from .. common
. common
import fetch_xml
, parse_xml
7 def fetch_searchresults ( q
= None , sp
= None ):
9 today
= datetime
. now ( timezone
. utc
). strftime ( "%Y%m %d " )
10 r
= requests
. get ( f
"https://www.youtube.com/results" , {
12 'pbj' : 1 , # makes youtube return a json-response
16 'x-youtube-client-name' : '1' ,
17 '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)
22 # Sometimes, youtube throws an exception after the response already begun.
23 # This can manifest in two ways:
24 # 1) So the status code is 200, begins with JSON and switches to HTML half
25 # way through. WTF?! (This should be "fixed" by retrying, though)
26 # 2) The response just stopping mid-way through like this: response.text ==
27 # '[\r\n{"page": "search","rootVe": "4724"},\r\n{"page": "search",'
28 # hence, just try-catching the decoding step is the easiest way out.
32 continue # will return None once we break out of the loop
34 def fetch_ajax ( params
):
36 fetch data using a continuation protobuf
38 # TODO: handle auto_generated!
39 today
= datetime
. now ( timezone
. utc
). strftime ( "%Y%m %d " )
41 # TODO: this is not cached any more! -> https://github.com/reclosedev/requests-cache/issues/154
42 r
= requests
. post ( f
"https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" , json
={
43 'continuation' : params
,
44 'context' : { 'client' : {
48 'clientVersion' : f
'2. {today} .01.01' ,
57 def canonicalize_channel ( name
):
58 if re
. fullmatch ( r
"(UC[A-Za-z0-9_-] {22} )" , name
):
61 # try /user/ (legacy URLs):
62 xmlfeed
= fetch_xml ( "user" , name
)
64 _
, _
, _
, channel_id
, _
= parse_xml ( xmlfeed
)
67 # get UCID of /c/ (vanity URLs):
68 today
= datetime
. now ( timezone
. utc
). strftime ( "%Y%m %d " )
69 r
= requests
. get ( f
'https://www.youtube.com/c/ {name} /about?pbj=1&hl=en_US' , headers
={
70 'x-youtube-client-name' : '1' ,
71 'x-youtube-client-version' : f
'2. {today} .01.01' , # see fetch_searchresults()
74 return r
. json ()[ 1 ][ 'response' ][ 'metadata' ][ 'channelMetadataRenderer' ][ 'rssUrl' ]. split ( "=" )[ 1 ]