diff options
author | James Taylor <user234683@users.noreply.github.com> | 2018-07-27 23:47:56 -0700 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2018-07-27 23:47:56 -0700 |
commit | 2ec95adb5fa573e968098efe10571bd5c71fded6 (patch) | |
tree | a3afd3bc2b823079c70a1298a8aa1aee26d20063 | |
parent | 0dc5f411053bf75c5c895ead9a2a01b79dc3c898 (diff) | |
download | yt-local-2ec95adb5fa573e968098efe10571bd5c71fded6.tar.lz yt-local-2ec95adb5fa573e968098efe10571bd5c71fded6.tar.xz yt-local-2ec95adb5fa573e968098efe10571bd5c71fded6.zip |
fix error with certain channels that have tabs hidden
-rw-r--r-- | youtube/channel.py | 22 |
1 files changed, 18 insertions, 4 deletions
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(''' <a href="$url">$text</a>''') 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'] |