Skip to content

Commit

Permalink
Use new channel api endpoint now that browse_ajax is disabled
Browse files Browse the repository at this point in the history
Fixes channel pages > 1
  • Loading branch information
user234683 committed Mar 3, 2021
1 parent a3535d6 commit b9682f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
27 changes: 23 additions & 4 deletions youtube/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,29 @@ def get_channel_tab(channel_id, page="1", sort=3, tab='videos', view=1,
if not ctoken:
ctoken = channel_ctoken_v3(channel_id, page, sort, tab, view)
ctoken = ctoken.replace('=', '%3D')
url = 'https://www.youtube.com/browse_ajax?ctoken=' + ctoken
content = util.fetch_url(url,
headers_desktop + generic_cookie,
debug_name='channel_tab', report_text=message)

# Not sure what the purpose of the key is or whether it will change
# For now it seems to be constant for the API endpoint, not dependent
# on the browsing session or channel
key = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'
url = 'https://www.youtube.com/youtubei/v1/browse?key=' + key

data = {
'context': {
'client': {
'hl': 'en',
'gl': 'US',
'clientName': 'WEB',
'clientVersion': '2.20180830',
},
},
'continuation': ctoken,
}

content_type_header = (('Content-Type', 'application/json'),)
content = util.fetch_url(
url, headers_desktop + content_type_header,
data=json.dumps(data), debug_name='channel_tab', report_text=message)

return content

Expand Down
5 changes: 5 additions & 0 deletions youtube/yt_data_extract/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ def extract_item_info(item, additional_info={}):

def extract_response(polymer_json):
'''return response, error'''
# /youtubei/v1/browse endpoint returns response directly
if isinstance(polymer_json, dict) and 'responseContext' in polymer_json:
# this is the response
return polymer_json, None

response = multi_deep_get(polymer_json, [1, 'response'], ['response'])
if response is None:
return None, 'Failed to extract response'
Expand Down

0 comments on commit b9682f2

Please sign in to comment.