From a57fc774260f2940449880ec9b6aaf40fa2776c3 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Sat, 5 Jan 2019 00:20:39 -0800 Subject: WSGI for simple non-path GET pages --- youtube/accounts.py | 6 ++++-- youtube/comments.py | 7 ++++--- youtube/playlist.py | 9 +++++---- youtube/post_comment.py | 14 ++++++++------ youtube/watch.py | 21 +++++++++++++-------- youtube/youtube.py | 38 ++++++++------------------------------ 6 files changed, 42 insertions(+), 53 deletions(-) (limited to 'youtube') diff --git a/youtube/accounts.py b/youtube/accounts.py index e7336a4..ebb9eae 100644 --- a/youtube/accounts.py +++ b/youtube/accounts.py @@ -51,7 +51,9 @@ def cookiejar_from_lwp_str(lwp_str): def account_cookiejar(channel_id): return cookiejar_from_lwp_str('\n'.join(accounts[channel_id]['cookies'])) -def get_account_login_page(query_string): +def get_account_login_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),] ) + style = ''' main{ display: grid; @@ -114,7 +116,7 @@ Using Tor to log in should only be done if the account was created using a proxy style = style, header = common.get_header(), page = page, - ) + ).encode('utf-8') diff --git a/youtube/comments.py b/youtube/comments.py index efa1b26..4860636 100644 --- a/youtube/comments.py +++ b/youtube/comments.py @@ -356,8 +356,9 @@ $options $video_id_input ''') -def get_comments_page(query_string): - parameters = urllib.parse.parse_qs(query_string) +def get_comments_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),] ) + parameters = env['fields'] ctoken = default_multi_get(parameters, 'ctoken', 0, default='') replies = False if not ctoken: @@ -419,4 +420,4 @@ def get_comments_page(query_string): header = common.get_header(), comments_area = comments_area, page_title = page_title, - ) \ No newline at end of file + ).encode('utf-8') \ No newline at end of file diff --git a/youtube/playlist.py b/youtube/playlist.py index c16dc7f..9e3eb4b 100644 --- a/youtube/playlist.py +++ b/youtube/playlist.py @@ -78,8 +78,9 @@ def get_videos_ajax(playlist_id, page): playlist_stat_template = Template('''
$stat
''') -def get_playlist_page(query_string): - parameters = urllib.parse.parse_qs(query_string) +def get_playlist_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),]) + parameters = env['fields'] playlist_id = parameters['list'][0] page = parameters.get("page", "1")[0] if page == "1": @@ -105,7 +106,7 @@ def get_playlist_page(query_string): metadata = common.ajax_info(first_page_json['content']['playlist_header']) video_count = int(metadata['size'].replace(',', '')) - page_buttons = common.page_buttons_html(int(page), math.ceil(video_count/20), common.URL_ORIGIN + "/playlist", query_string) + page_buttons = common.page_buttons_html(int(page), math.ceil(video_count/20), common.URL_ORIGIN + "/playlist", env['QUERY_STRING']) html_ready = common.get_html_ready(metadata) html_ready['page_title'] = html_ready['title'] + ' - Page ' + str(page) @@ -119,4 +120,4 @@ def get_playlist_page(query_string): page_buttons = page_buttons, stats = stats, **html_ready - ) \ No newline at end of file + ).encode('utf-8') \ No newline at end of file diff --git a/youtube/post_comment.py b/youtube/post_comment.py index 587a258..fc4d4b1 100644 --- a/youtube/post_comment.py +++ b/youtube/post_comment.py @@ -152,8 +152,9 @@ def post_comment(parameters, fields): return response''' return code -def get_delete_comment_page(query_string): - parameters = urllib.parse.parse_qs(query_string) +def get_delete_comment_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),]) + parameters = env['fields'] style = ''' main{ @@ -180,10 +181,11 @@ def get_delete_comment_page(query_string): style = style, header = common.get_header(), page = page, - ) + ).encode('utf-8') -def get_post_comment_page(query_string): - parameters = urllib.parse.parse_qs(query_string) +def get_post_comment_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),]) + parameters = env['fields'] video_id = parameters['video_id'][0] parent_id = common.default_multi_get(parameters, 'parent_id', 0, default='') @@ -224,4 +226,4 @@ textarea{ style = style, header = common.get_header(), page = page, - ) \ No newline at end of file + ).encode('utf-8') \ No newline at end of file diff --git a/youtube/watch.py b/youtube/watch.py index 4051a3f..1e3e93a 100644 --- a/youtube/watch.py +++ b/youtube/watch.py @@ -228,18 +228,23 @@ music_list_table_row = Template(''' $attribute $value ''') -def get_watch_page(query_string): - parsed_qs = urllib.parse.parse_qs(query_string) - id = parsed_qs['v'][0] - lc = common.default_multi_get(parsed_qs, 'lc', 0, default='') +def get_watch_page(env, start_response): + video_id = env['fields']['v'][0] + if len(video_id) < 11: + start_response('404 Not Found', ()) + return b'Incomplete video id (too short): ' + video_id.encode('ascii') + + start_response('200 OK', [('Content-type','text/html'),]) + + lc = common.default_multi_get(env['fields'], 'lc', 0, default='') if settings.route_tor: proxy = 'socks5://127.0.0.1:9150/' else: proxy = '' downloader = YoutubeDL(params={'youtube_include_dash_manifest':False, 'proxy':proxy}) tasks = ( - gevent.spawn(comments.video_comments, id, int(settings.default_comment_sorting), lc=lc ), - gevent.spawn(extract_info, downloader, "https://www.youtube.com/watch?v=" + id, download=False) + gevent.spawn(comments.video_comments, video_id, int(settings.default_comment_sorting), lc=lc ), + gevent.spawn(extract_info, downloader, "https://www.youtube.com/watch?v=" + video_id, download=False) ) gevent.joinall(tasks) comments_html, info = tasks[0].value, tasks[1].value @@ -256,7 +261,7 @@ def get_watch_page(query_string): style = "", header = common.get_header(), page = html.escape(info), - ) + ).encode('utf-8') sorted_formats = sort_formats(info) @@ -351,4 +356,4 @@ def get_watch_page(query_string): music_list = music_list_html, is_unlisted = 'Unlisted' if info['unlisted'] else '', ) - return page \ No newline at end of file + return page.encode('utf-8') \ No newline at end of file diff --git a/youtube/youtube.py b/youtube/youtube.py index 44f9e3d..639d279 100644 --- a/youtube/youtube.py +++ b/youtube/youtube.py @@ -9,8 +9,14 @@ YOUTUBE_FILES = ( '/favicon.ico', ) page_handlers = { - 'search': search.get_search_page, - '': search.get_search_page, + 'search': search.get_search_page, + '': search.get_search_page, + 'comments': comments.get_comments_page, + 'watch': watch.get_watch_page, + 'playlist': playlist.get_playlist_page, + 'post_comment': post_comment.get_post_comment_page, + 'delete_comment': post_comment.get_delete_comment_page, + 'login': accounts.get_account_login_page, } def youtube(env, start_response): path, method, query_string = env['PATH_INFO'], env['REQUEST_METHOD'], env['QUERY_STRING'] @@ -33,22 +39,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 == "/comments": - start_response('200 OK', (('Content-type','text/html'),) ) - return comments.get_comments_page(query_string).encode() - - elif path == "/watch": - video_id = urllib.parse.parse_qs(query_string)['v'][0] - if len(video_id) < 11: - start_response('404 Not Found', ()) - return b'Incomplete video id (too short): ' + video_id.encode('ascii') - start_response('200 OK', (('Content-type','text/html'),) ) - return watch.get_watch_page(query_string).encode() - - elif path == "/playlist": - start_response('200 OK', (('Content-type','text/html'),) ) - return playlist.get_playlist_page(query_string).encode() elif path.startswith("/channel/"): start_response('200 OK', (('Content-type','text/html'),) ) @@ -73,24 +63,12 @@ def youtube(env, start_response): result = result.replace(b"align:start position:0%", b"") return result - elif path == "/post_comment": - start_response('200 OK', () ) - return post_comment.get_post_comment_page(query_string).encode() - elif path == "/opensearch.xml": with open("youtube" + path, 'rb') as f: mime_type = mimetypes.guess_type(path)[0] or 'application/octet-stream' start_response('200 OK', (('Content-type',mime_type),) ) return f.read().replace(b'$port_number', str(settings.port_number).encode()) - elif path == "/login": - start_response('200 OK', (('Content-type','text/html'),) ) - return accounts.get_account_login_page(query_string=query_string).encode() - - elif path == "/delete_comment": - start_response('200 OK', (('Content-type','text/html'),) ) - return post_comment.get_delete_comment_page(query_string).encode() - elif path == "/comment_delete_success": start_response('200 OK', () ) return b'Successfully deleted comment' -- cgit v1.2.3