diff options
Diffstat (limited to 'youtube')
-rw-r--r-- | youtube/channel.py | 58 | ||||
-rw-r--r-- | youtube/local_playlist.py | 24 | ||||
-rw-r--r-- | youtube/youtube.py | 42 |
3 files changed, 66 insertions, 58 deletions
diff --git a/youtube/channel.py b/youtube/channel.py index 25e4019..d4a1d47 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -359,15 +359,15 @@ def get_channel_search_json(channel_id, query, page): return polymer_json playlist_sort_codes = {'2': "da", '3': "dd", '4': "lad"} -def get_channel_page(url, query_string=''): - path_components = url.rstrip('/').lstrip('/').split('/') - channel_id = path_components[0] +def get_channel_page(env, start_response): + path_parts = env['path_parts'] + channel_id = path_parts[1] try: - tab = path_components[1] + tab = path_parts[2] except IndexError: tab = 'videos' - parameters = urllib.parse.parse_qs(query_string) + parameters = env['fields'] page_number = int(common.default_multi_get(parameters, 'page', 0, default='1')) sort = common.default_multi_get(parameters, 'sort', 0, default='3') view = common.default_multi_get(parameters, 'view', 0, default='1') @@ -381,17 +381,17 @@ def get_channel_page(url, query_string=''): gevent.joinall(tasks) number_of_videos, polymer_json = tasks[0].value, tasks[1].value - return channel_videos_html(polymer_json, page_number, sort, number_of_videos, query_string) + result = channel_videos_html(polymer_json, page_number, sort, number_of_videos, env['QUERY_STRING']) elif tab == 'about': polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/about?pbj=1', common.desktop_ua + headers_1) polymer_json = json.loads(polymer_json) - return channel_about_page(polymer_json) + result = channel_about_page(polymer_json) elif tab == 'playlists': polymer_json = common.fetch_url('https://www.youtube.com/channel/' + channel_id + '/playlists?pbj=1&view=1&sort=' + playlist_sort_codes[sort], common.desktop_ua + headers_1) '''with open('debug/channel_playlists_debug', 'wb') as f: f.write(polymer_json)''' polymer_json = json.loads(polymer_json) - return channel_playlists_html(polymer_json, sort) + result = channel_playlists_html(polymer_json, sort) elif tab == 'search': tasks = ( gevent.spawn(get_number_of_videos, channel_id ), @@ -400,44 +400,54 @@ def get_channel_page(url, query_string=''): gevent.joinall(tasks) number_of_videos, polymer_json = tasks[0].value, tasks[1].value - return channel_search_page(polymer_json, query, page_number, number_of_videos, query_string) + result = channel_search_page(polymer_json, query, page_number, number_of_videos, env['QUERY_STRING']) else: - raise http_errors.Error404('Unknown channel tab: ' + tab) + start_response('404 Not Found', ()) + return b'Unknown channel tab: ' + tab.encode('utf-8') + + start_response('200 OK', [('Content-type','text/html'),]) + return result.encode('utf-8') # 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('/') - 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 +def get_channel_page_general_url(env, start_response): + path_parts = env['path_parts'] + + is_toplevel = not path_parts[0] in ('user', 'c') + + if len(path_parts) + int(is_toplevel) == 3: # has /[page] after it + page = path_parts[2] + base_url = 'https://www.youtube.com/' + '/'.join(path_parts[0:-1]) + elif len(path_parts) + 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) + base_url = 'https://www.youtube.com/' + '/'.join(path_parts) else: - raise http_errors.Error404('Invalid channel url') + start_response('404 Not Found', ()) + return b'Invalid channel url' if page == 'videos': polymer_json = common.fetch_url(base_url + '/videos?pbj=1&view=0', common.desktop_ua + headers_1) '''with open('debug/user_page_videos', 'wb') as f: f.write(polymer_json)''' polymer_json = json.loads(polymer_json) - return channel_videos_html(polymer_json) + result = channel_videos_html(polymer_json) elif page == 'about': 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) + result = channel_about_page(polymer_json) elif page == 'playlists': 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) + result = channel_playlists_html(polymer_json) elif page == 'search': raise NotImplementedError() '''polymer_json = common.fetch_url('https://www.youtube.com/user' + username + '/search?pbj=1&' + query_string, common.desktop_ua + headers_1) polymer_json = json.loads(polymer_json) return channel_search_page(''' else: - raise http_errors.Error404('Unknown channel page: ' + page)
\ No newline at end of file + start_response('404 Not Found', ()) + return b'Unknown channel page: ' + page.encode('utf-8') + + start_response('200 OK', [('Content-type','text/html'),]) + return result.encode('utf-8')
\ No newline at end of file diff --git a/youtube/local_playlist.py b/youtube/local_playlist.py index 0ee76be..4d85a1f 100644 --- a/youtube/local_playlist.py +++ b/youtube/local_playlist.py @@ -137,15 +137,29 @@ def get_playlists_list_page(): ) -def get_playlist_page(url, query_string=''): - url = url.rstrip('/').lstrip('/') - if url == '': - return get_playlists_list_page() +def get_playlist_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),]) + path_parts = env['path_parts'] + if len(path_parts) == 1: + return get_playlists_list_page().encode('utf-8') else: - return get_local_playlist_page(url) + return get_local_playlist_page(path_parts[1]).encode('utf-8') +def path_edit_playlist(env, start_response): + '''Called when making changes to the playlist from that playlist's page''' + fields = env['fields'] + if fields['action'][0] == 'remove': + playlist_name = env['path_parts'][1] + remove_from_playlist(playlist_name, fields['video_info_list']) + start_response('303 See Other', [('Location', common.URL_ORIGIN + env['PATH_INFO']),] ) + return b'' + + else: + start_response('400 Bad Request', ()) + return b'400 Bad Request' def edit_playlist(env, start_response): + '''Called when adding videos to a playlist from elsewhere''' fields = env['fields'] if fields['action'][0] == 'add': add_to_playlist(fields['playlist_name'][0], fields['video_info_list']) diff --git a/youtube/youtube.py b/youtube/youtube.py index c00d9c5..57ea481 100644 --- a/youtube/youtube.py +++ b/youtube/youtube.py @@ -11,15 +11,24 @@ YOUTUBE_FILES = ( get_handlers = { 'search': search.get_search_page, '': search.get_search_page, - 'comments': comments.get_comments_page, 'watch': watch.get_watch_page, 'playlist': playlist.get_playlist_page, + + 'channel': channel.get_channel_page, + 'user': channel.get_channel_page_general_url, + 'c': channel.get_channel_page_general_url, + + 'playlists': local_playlist.get_playlist_page, + + 'comments': comments.get_comments_page, 'post_comment': post_comment.get_post_comment_page, 'delete_comment': post_comment.get_delete_comment_page, 'login': accounts.get_account_login_page, } post_handlers = { 'edit_playlist': local_playlist.edit_playlist, + 'playlists': local_playlist.path_edit_playlist, + 'login': accounts.add_account, 'comments': post_comment.post_comment, 'post_comment': post_comment.post_comment, @@ -47,18 +56,6 @@ def youtube(env, start_response): mime_type = mimetypes.guess_type(path)[0] or 'application/octet-stream' start_response('200 OK', (('Content-type',mime_type),) ) return f.read() - - elif path.startswith("/channel/"): - start_response('200 OK', (('Content-type','text/html'),) ) - return channel.get_channel_page(path[9:], query_string=query_string).encode() - - elif path.startswith("/user/") or path.startswith("/c/"): - start_response('200 OK', (('Content-type','text/html'),) ) - 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'),) ) - return local_playlist.get_playlist_page(path[10:], query_string=query_string).encode() elif path.startswith("/data/playlist_thumbnails/"): with open(os.path.join(settings.data_dir, os.path.normpath(path[6:])), 'rb') as f: @@ -86,8 +83,7 @@ def youtube(env, start_response): return b'Failed to deleted comment' else: - start_response('200 OK', (('Content-type','text/html'),) ) - return channel.get_channel_page_general_url(path, query_string=query_string).encode() + return channel.get_channel_page_general_url(env, start_response) elif method == "POST": post_fields = urllib.parse.parse_qs(env['wsgi.input'].read().decode()) @@ -102,20 +98,8 @@ def youtube(env, start_response): else: return handler(env, start_response) - if path.startswith("/playlists"): - if fields['action'][0] == 'remove': - playlist_name = path[11:] - local_playlist.remove_from_playlist(playlist_name, fields['video_info_list']) - start_response('303 See Other', (('Location', common.URL_ORIGIN + path),) ) - return local_playlist.get_playlist_page(playlist_name).encode() - - else: - start_response('400 Bad Request', ()) - return b'400 Bad Request' - - else: - start_response('404 Not Found', ()) - return b'404 Not Found' + start_response('404 Not Found', ()) + return b'404 Not Found' else: start_response('501 Not Implemented', ()) |