From 60fa26f459e8d19fe434ec37fd22fa4f1baa3f29 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Thu, 5 Jul 2018 20:46:18 -0700 Subject: use unified item code for channel grid --- youtube/channel.py | 10 +++++++--- youtube/common.py | 17 ++++++++++------- yt_channel_items_template.html | 2 -- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/youtube/channel.py b/youtube/channel.py index 3cec09d..98014a3 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -151,6 +151,12 @@ def get_channel_id(username): response = common.fetch_url(url, common.mobile_ua + headers_1).decode('utf-8') return re.search(r'"channel_id":\s*"([a-zA-Z0-9_-]*)"', response).group(1) +def grid_items_html(items, additional_info={}): + result = ''' ''' + return result def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, current_query_string=''): microformat = polymer_json[1]['response']['microformat']['microformatDataRenderer'] @@ -166,9 +172,7 @@ def channel_videos_html(polymer_json, current_page=1, number_of_videos = 1000, c items = [] else: items = contents['twoColumnBrowseResultsRenderer']['tabs'][1]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['gridRenderer']['items'] - items_html = '' - for video in items: - items_html += grid_video_item_html(grid_video_item_info(video['gridVideoRenderer'], microformat['title'])) + items_html = grid_items_html(items, {'author': microformat['title']}) return yt_channel_items_template.substitute( channel_title = microformat['title'], diff --git a/youtube/common.py b/youtube/common.py index 2340a0c..b8fe751 100644 --- a/youtube/common.py +++ b/youtube/common.py @@ -434,7 +434,7 @@ def get_html_ready(item): author_template_url = Template('''
By $author
''') -author_template = Template('''
By $author
''') +author_template = Template('''
$author
''') stat_templates = ( Template('''$views'''), Template(''''''), @@ -453,7 +453,7 @@ def get_video_stats(html_ready): pass return ' | '.join(stats) -def video_item_html(item, template): +def video_item_html(item, template, html_exclude=set()): html_ready = get_html_ready(item) video_info = {} for key in ('id', 'title', 'author'): @@ -470,12 +470,14 @@ def video_item_html(item, template): html_ready['url'] = URL_ORIGIN + "/watch?v=" + html_ready['id'] html_ready['datetime'] = '' #TODO + for key in html_exclude: + del html_ready[key] html_ready['stats'] = get_video_stats(html_ready) return template.substitute(html_ready) -def playlist_item_html(item, template): +def playlist_item_html(item, template, html_exlude=set()): html_ready = get_html_ready(item) html_ready['url'] = URL_ORIGIN + "/playlist?list=" + html_ready['id'] @@ -540,14 +542,15 @@ def renderer_html(renderer, additional_info={}, current_query_string=''): if type in ('videoRenderer', 'playlistRenderer', 'radioRenderer', 'compactVideoRenderer', 'compactPlaylistRenderer', 'compactRadioRenderer', 'gridVideoRenderer', 'gridPlaylistRenderer', 'gridRadioRenderer'): info = renderer_info(renderer) info.update(additional_info) + html_exclude = set(additional_info.keys()) if type == 'compactVideoRenderer': - return video_item_html(info, small_video_item_template) + return video_item_html(info, small_video_item_template, html_exclude=html_exclude) if type in ('compactPlaylistRenderer', 'compactRadioRenderer'): - return playlist_item_html(info, small_playlist_item_template) + return playlist_item_html(info, small_playlist_item_template, html_exclude=html_exclude) if type in ('videoRenderer', 'gridVideoRenderer'): - return video_item_html(info, medium_video_item_template) + return video_item_html(info, medium_video_item_template, html_exclude=html_exclude) if type in ('playlistRenderer', 'gridPlaylistRenderer', 'radioRenderer', 'gridRadioRenderer'): - return playlist_item_html(info, medium_playlist_item_template) + return playlist_item_html(info, medium_playlist_item_template, html_exclude=html_exclude) if type == 'channelRenderer': info = renderer_info(renderer) diff --git a/yt_channel_items_template.html b/yt_channel_items_template.html index 3d22b50..955f886 100644 --- a/yt_channel_items_template.html +++ b/yt_channel_items_template.html @@ -118,9 +118,7 @@ About
$number_of_results
- -- cgit v1.2.3