From 7c97a1705301219783d901332951a2f1e86ed26f Mon Sep 17 00:00:00 2001 From: James Taylor Date: Sun, 16 Dec 2018 18:49:09 -0800 Subject: Support for custom channel urls --- youtube/channel.py | 27 ++++++++++++++++++--------- youtube/youtube.py | 8 ++++---- 2 files changed, 22 insertions(+), 13 deletions(-) (limited to 'youtube') diff --git a/youtube/channel.py b/youtube/channel.py index ce0ecba..a2acb23 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -358,24 +358,33 @@ def get_channel_page(url, query_string=''): return channel_search_page(polymer_json, query, page_number, number_of_videos, query_string) else: raise ValueError('Unknown channel tab: ' + tab) - -def get_user_page(url, query_string=''): + +# youtube.com/user/[username]/[page] +# youtube.com/c/[custom]/[page] +# youtube.com/[custom]/[page] +def get_channel_page_general_url(url, query_string=''): path_components = url.rstrip('/').lstrip('/').split('/') - username = path_components[0] - try: - page = path_components[1] - except IndexError: + is_toplevel = not path_components[0] in ('user', 'c') + + if len(path_components) + int(is_toplevel) == 3: # has /[page] after it + page = path_components[2] + base_url = 'https://www.youtube.com/' + '/'.join(path_components[0:-1]) + elif len(path_components) + int(is_toplevel) == 2: # does not have /[page] after it, use /videos by default page = 'videos' + base_url = 'https://www.youtube.com/' + '/'.join(path_components) + else: + raise ValueError() + if page == 'videos': - polymer_json = common.fetch_url('https://www.youtube.com/user/' + username + '/videos?pbj=1&view=0', common.desktop_ua + headers_1) + polymer_json = common.fetch_url(base_url + '/videos?pbj=1&view=0', common.desktop_ua + headers_1) polymer_json = json.loads(polymer_json) return channel_videos_html(polymer_json) elif page == 'about': - polymer_json = common.fetch_url('https://www.youtube.com/user/' + username + '/about?pbj=1', common.desktop_ua + headers_1) + polymer_json = common.fetch_url(base_url + '/about?pbj=1', common.desktop_ua + headers_1) polymer_json = json.loads(polymer_json) return channel_about_page(polymer_json) elif page == 'playlists': - polymer_json = common.fetch_url('https://www.youtube.com/user/' + username + '/playlists?pbj=1&view=1', common.desktop_ua + headers_1) + polymer_json = common.fetch_url(base_url+ '/playlists?pbj=1&view=1', common.desktop_ua + headers_1) polymer_json = json.loads(polymer_json) return channel_playlists_html(polymer_json) elif page == 'search': diff --git a/youtube/youtube.py b/youtube/youtube.py index 74d03e9..cb35eac 100644 --- a/youtube/youtube.py +++ b/youtube/youtube.py @@ -41,9 +41,9 @@ def youtube(env, start_response): start_response('200 OK', (('Content-type','text/html'),) ) return channel.get_channel_page(path[9:], query_string=query_string).encode() - elif path.startswith("/user/"): + elif path.startswith("/user/") or path.startswith("/c/"): start_response('200 OK', (('Content-type','text/html'),) ) - return channel.get_user_page(path[6:], query_string=query_string).encode() + return channel.get_channel_page_general_url(path, query_string=query_string).encode() elif path.startswith("/playlists"): start_response('200 OK', (('Content-type','text/html'),) ) @@ -71,8 +71,8 @@ def youtube(env, start_response): return f.read().replace(b'$port_number', str(settings.port_number).encode()) else: - start_response('404 Not Found', () ) - return b'404 Not Found' + start_response('200 OK', (('Content-type','text/html'),) ) + return channel.get_channel_page_general_url(path, query_string=query_string).encode() elif method == "POST": fields = urllib.parse.parse_qs(env['wsgi.input'].read().decode()) -- cgit v1.2.3