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/watch.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'youtube/watch.py') 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 -- cgit v1.2.3