From 3905e7e64059b45479894ba1fdfb0ef9cef64475 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Sat, 16 Feb 2019 23:41:52 -0800 Subject: basic subscriptions system --- youtube/channel.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'youtube/channel.py') diff --git a/youtube/channel.py b/youtube/channel.py index 9577525..c83d7d1 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -248,6 +248,7 @@ def channel_videos_html(polymer_json, current_page=1, current_sort=3, number_of_ return yt_channel_items_template.substitute( header = common.get_header(), channel_title = microformat['title'], + channel_id = channel_id, channel_tabs = channel_tabs_html(channel_id, 'Videos'), sort_buttons = channel_sort_buttons_html(channel_id, 'videos', current_sort), avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'], @@ -269,6 +270,7 @@ def channel_playlists_html(polymer_json, current_sort=3): return yt_channel_items_template.substitute( header = common.get_header(), channel_title = microformat['title'], + channel_id = channel_id, channel_tabs = channel_tabs_html(channel_id, 'Playlists'), sort_buttons = channel_sort_buttons_html(channel_id, 'playlists', current_sort), avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'], @@ -333,6 +335,7 @@ def channel_about_page(polymer_json): description = description, links = channel_links, stats = stats, + channel_id = channel_metadata['channelId'], channel_tabs = channel_tabs_html(channel_metadata['channelId'], 'About'), ) @@ -353,6 +356,7 @@ def channel_search_page(polymer_json, query, current_page=1, number_of_videos = return yt_channel_items_template.substitute( header = common.get_header(), channel_title = html.escape(microformat['title']), + channel_id = channel_id, channel_tabs = channel_tabs_html(channel_id, '', query), avatar = '/' + microformat['thumbnail']['thumbnails'][0]['url'], page_title = html.escape(query + ' - Channel search'), -- cgit v1.2.3 From 103b37030fcd073b5f44b9ddc79da0ce15325a96 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 10 Jun 2019 17:04:06 -0700 Subject: Unsubscribe button on channels if already subscribed --- youtube/channel.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'youtube/channel.py') diff --git a/youtube/channel.py b/youtube/channel.py index 55316e2..1b345b5 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -1,5 +1,5 @@ import base64 -from youtube import util, yt_data_extract, html_common +from youtube import util, yt_data_extract, html_common, subscriptions import http_errors import urllib @@ -241,6 +241,12 @@ def channel_videos_html(polymer_json, current_page=1, current_sort=3, number_of_ microformat = get_microformat(response) channel_url = microformat['urlCanonical'].rstrip('/') channel_id = channel_url[channel_url.rfind('/')+1:] + if subscriptions.is_subscribed(channel_id): + action_name = 'Unsubscribe' + action = 'unsubscribe' + else: + action_name = 'Subscribe' + action = 'subscribe' items = get_grid_items(response) items_html = grid_items_html(items, {'author': microformat['title']}) @@ -256,6 +262,8 @@ def channel_videos_html(polymer_json, current_page=1, current_sort=3, number_of_ items = items_html, page_buttons = html_common.page_buttons_html(current_page, math.ceil(number_of_videos/30), util.URL_ORIGIN + "/channel/" + channel_id + "/videos", current_query_string), number_of_results = '{:,}'.format(number_of_videos) + " videos", + action_name = action_name, + action = action, ) def channel_playlists_html(polymer_json, current_sort=3): @@ -264,6 +272,13 @@ def channel_playlists_html(polymer_json, current_sort=3): channel_url = microformat['urlCanonical'].rstrip('/') channel_id = channel_url[channel_url.rfind('/')+1:] + if subscriptions.is_subscribed(channel_id): + action_name = 'Unsubscribe' + action = 'unsubscribe' + else: + action_name = 'Subscribe' + action = 'subscribe' + items = get_grid_items(response) items_html = grid_items_html(items, {'author': microformat['title']}) @@ -278,6 +293,8 @@ def channel_playlists_html(polymer_json, current_sort=3): items = items_html, page_buttons = '', number_of_results = '', + action_name = action_name, + action = action, ) # Example channel where tabs do not have definite index: https://www.youtube.com/channel/UC4gQ8i3FD7YbhOgqUkeQEJg @@ -323,6 +340,16 @@ def channel_about_page(polymer_json): continue else: stats += stat_template.substitute(stat_value=stat_value) + + + channel_id = channel_metadata['channelId'] + if subscriptions.is_subscribed(channel_id): + action_name = 'Unsubscribe' + action = 'unsubscribe' + else: + action_name = 'Subscribe' + action = 'subscribe' + try: description = yt_data_extract.format_text_runs(yt_data_extract.get_formatted_text(channel_metadata['description'])) except KeyError: @@ -335,8 +362,10 @@ def channel_about_page(polymer_json): description = description, links = channel_links, stats = stats, - channel_id = channel_metadata['channelId'], + channel_id = channel_id, channel_tabs = channel_tabs_html(channel_metadata['channelId'], 'About'), + action_name = action_name, + action = action, ) def channel_search_page(polymer_json, query, current_page=1, number_of_videos = 1000, current_query_string=''): @@ -345,7 +374,14 @@ def channel_search_page(polymer_json, query, current_page=1, number_of_videos = channel_url = microformat['urlCanonical'].rstrip('/') channel_id = channel_url[channel_url.rfind('/')+1:] - + if subscriptions.is_subscribed(channel_id): + action_name = 'Unsubscribe' + action = 'unsubscribe' + else: + action_name = 'Subscribe' + action = 'subscribe' + + try: items = tab_with_content(response['contents']['twoColumnBrowseResultsRenderer']['tabs'])['sectionListRenderer']['contents'] except KeyError: @@ -364,6 +400,8 @@ def channel_search_page(polymer_json, query, current_page=1, number_of_videos = page_buttons = html_common.page_buttons_html(current_page, math.ceil(number_of_videos/29), util.URL_ORIGIN + "/channel/" + channel_id + "/search", current_query_string), number_of_results = '', sort_buttons = '', + action_name = action_name, + action = action, ) def get_channel_search_json(channel_id, query, page): params = proto.string(2, 'search') + proto.string(15, str(page)) -- cgit v1.2.3 From 2617f87e17b94646eb5a248360a7e0f9b7675d3e Mon Sep 17 00:00:00 2001 From: James Taylor Date: Sat, 10 Aug 2019 18:12:02 -0700 Subject: Add subscribe button on channels --- youtube/channel.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'youtube/channel.py') diff --git a/youtube/channel.py b/youtube/channel.py index 4c7d380..9d0532a 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -1,5 +1,5 @@ import base64 -from youtube import util, yt_data_extract, local_playlist +from youtube import util, yt_data_extract, local_playlist, subscriptions from youtube import yt_app import urllib @@ -312,7 +312,7 @@ def get_channel_page(channel_id, tab='videos'): info['current_sort'] = sort elif tab == 'search': info['search_box_value'] = query - + info['subscribed'] = subscriptions.is_subscribed(info['channel_id']) return flask.render_template('channel.html', parameters_dictionary = request.args, @@ -352,7 +352,7 @@ def get_channel_page_general_url(base_url, tab, request): info['current_sort'] = sort elif tab == 'search': info['search_box_value'] = query - + info['subscribed'] = subscriptions.is_subscribed(info['channel_id']) return flask.render_template('channel.html', parameters_dictionary = request.args, -- cgit v1.2.3 From 046d7226eaa3087159e64310bfa1b5e359cd2e93 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Wed, 14 Aug 2019 18:39:39 -0700 Subject: Subscriptions: improve checking messages, say how many new vids from channel --- youtube/channel.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'youtube/channel.py') diff --git a/youtube/channel.py b/youtube/channel.py index 9d0532a..de75eaa 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -83,13 +83,15 @@ def channel_ctoken(channel_id, page, sort, tab, view=1): return base64.urlsafe_b64encode(pointless_nest).decode('ascii') -def get_channel_tab(channel_id, page="1", sort=3, tab='videos', view=1): +def get_channel_tab(channel_id, page="1", sort=3, tab='videos', view=1, print_status=True): ctoken = channel_ctoken(channel_id, page, sort, tab, view).replace('=', '%3D') url = "https://www.youtube.com/browse_ajax?ctoken=" + ctoken - print("Sending channel tab ajax request") + if print_status: + print("Sending channel tab ajax request") content = util.fetch_url(url, util.desktop_ua + headers_1, debug_name='channel_tab') - print("Finished recieving channel tab response") + if print_status: + print("Finished recieving channel tab response") return content -- cgit v1.2.3