From 2ec95adb5fa573e968098efe10571bd5c71fded6 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Fri, 27 Jul 2018 23:47:56 -0700 Subject: fix error with certain channels that have tabs hidden --- youtube/channel.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'youtube/channel.py') diff --git a/youtube/channel.py b/youtube/channel.py index 414ea16..960fb0e 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -169,7 +169,7 @@ def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, c except KeyError: items = [] else: - items = contents['twoColumnBrowseResultsRenderer']['tabs'][1]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items'] + items = tab_with_content(contents['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items'] items_html = grid_items_html(items, {'author': microformat['title']}) return yt_channel_items_template.substitute( @@ -196,7 +196,7 @@ def channel_playlists_html(polymer_json): except KeyError: items = [] else: - item_section = contents['twoColumnBrowseResultsRenderer']['tabs'][2]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0] + item_section = tab_with_content(contents['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0] try: items = item_section['gridRenderer']['items'] except KeyError: @@ -218,6 +218,20 @@ def channel_playlists_html(polymer_json): number_of_results = '', ) +# Example channel where tabs do not have definite index: https://www.youtube.com/channel/UC4gQ8i3FD7YbhOgqUkeQEJg +def tab_with_content(tabs): + for tab in tabs: + try: + renderer = tab['tabRenderer'] + except KeyError: + renderer = tab['expandableTabRenderer'] + try: + return renderer['content'] + except KeyError: + pass + + raise Exception("No tabs found with content") + channel_link_template = Template(''' $text''') stat_template = Template(''' @@ -225,7 +239,7 @@ stat_template = Template(''' def channel_about_page(polymer_json): avatar = '/' + polymer_json[1]['response']['microformat']['microformatDataRenderer']['thumbnail']['thumbnails'][0]['url'] # my goodness... - channel_metadata = polymer_json[1]['response']['contents']['twoColumnBrowseResultsRenderer']['tabs'][5]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['channelAboutFullMetadataRenderer'] + channel_metadata = tab_with_content(polymer_json[1]['response']['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['channelAboutFullMetadataRenderer'] channel_links = '' for link_json in channel_metadata['primaryLinks']: channel_links += channel_link_template.substitute( @@ -263,7 +277,7 @@ def channel_search_page(polymer_json, query, current_page=1, number_of_videos = response = polymer_json[1]['response'] try: - items = response['contents']['twoColumnBrowseResultsRenderer']['tabs'][6]['expandableTabRenderer']['content']['sectionListRenderer']['contents'] + items = tab_with_content(response['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'] except KeyError: items = response['continuationContents']['sectionListContinuation']['contents'] -- cgit v1.2.3