]>
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 r
= requests
. get ( f
"https://www.youtube.com/browse_ajax" , {
42 'continuation' : params
,
46 'x-youtube-client-name' : '1' ,
47 'x-youtube-client-version' : f
'2. {today} .01.01' , # see fetch_searchresults()
55 def canonicalize_channel ( name
):
56 if re
. fullmatch ( r
"(UC[A-Za-z0-9_-] {22} )" , name
):
59 # try /user/ (legacy URLs):
60 xmlfeed
= fetch_xml ( "user" , name
)
62 _
, _
, _
, channel_id
, _
= parse_xml ( xmlfeed
)
65 # get UCID of /c/ (vanity URLs):
66 today
= datetime
. now ( timezone
. utc
). strftime ( "%Y%m %d " )
67 r
= requests
. get ( f
'https://www.youtube.com/c/ {name} /about?pbj=1&hl=en_US' , headers
={
68 'x-youtube-client-name' : '1' ,
69 'x-youtube-client-version' : f
'2. {today} .01.01' , # see fetch_searchresults()
72 return r
. json ()[ 1 ][ 'response' ][ 'metadata' ][ 'channelMetadataRenderer' ][ 'rssUrl' ]. split ( "=" )[ 1 ]