WSGI for simple non-path GET pages

This commit is contained in:
James Taylor 2019-01-05 00:20:39 -08:00
parent ec2fb71a37
commit a57fc77426
6 changed files with 42 additions and 53 deletions

View File

@ -51,7 +51,9 @@ def cookiejar_from_lwp_str(lwp_str):
def account_cookiejar(channel_id): def account_cookiejar(channel_id):
return cookiejar_from_lwp_str('\n'.join(accounts[channel_id]['cookies'])) 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 = ''' style = '''
main{ main{
display: grid; 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, style = style,
header = common.get_header(), header = common.get_header(),
page = page, page = page,
) ).encode('utf-8')

View File

@ -356,8 +356,9 @@ $options
$video_id_input $video_id_input
<button type="submit" class="post-comment-button">$post_text</button> <button type="submit" class="post-comment-button">$post_text</button>
</form>''') </form>''')
def get_comments_page(query_string): def get_comments_page(env, start_response):
parameters = urllib.parse.parse_qs(query_string) start_response('200 OK', [('Content-type','text/html'),] )
parameters = env['fields']
ctoken = default_multi_get(parameters, 'ctoken', 0, default='') ctoken = default_multi_get(parameters, 'ctoken', 0, default='')
replies = False replies = False
if not ctoken: if not ctoken:
@ -419,4 +420,4 @@ def get_comments_page(query_string):
header = common.get_header(), header = common.get_header(),
comments_area = comments_area, comments_area = comments_area,
page_title = page_title, page_title = page_title,
) ).encode('utf-8')

View File

@ -78,8 +78,9 @@ def get_videos_ajax(playlist_id, page):
playlist_stat_template = Template(''' playlist_stat_template = Template('''
<div>$stat</div>''') <div>$stat</div>''')
def get_playlist_page(query_string): def get_playlist_page(env, start_response):
parameters = urllib.parse.parse_qs(query_string) start_response('200 OK', [('Content-type','text/html'),])
parameters = env['fields']
playlist_id = parameters['list'][0] playlist_id = parameters['list'][0]
page = parameters.get("page", "1")[0] page = parameters.get("page", "1")[0]
if page == "1": if page == "1":
@ -105,7 +106,7 @@ def get_playlist_page(query_string):
metadata = common.ajax_info(first_page_json['content']['playlist_header']) metadata = common.ajax_info(first_page_json['content']['playlist_header'])
video_count = int(metadata['size'].replace(',', '')) 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 = common.get_html_ready(metadata)
html_ready['page_title'] = html_ready['title'] + ' - Page ' + str(page) html_ready['page_title'] = html_ready['title'] + ' - Page ' + str(page)
@ -119,4 +120,4 @@ def get_playlist_page(query_string):
page_buttons = page_buttons, page_buttons = page_buttons,
stats = stats, stats = stats,
**html_ready **html_ready
) ).encode('utf-8')

View File

@ -152,8 +152,9 @@ def post_comment(parameters, fields):
return response''' return response'''
return code return code
def get_delete_comment_page(query_string): def get_delete_comment_page(env, start_response):
parameters = urllib.parse.parse_qs(query_string) start_response('200 OK', [('Content-type','text/html'),])
parameters = env['fields']
style = ''' style = '''
main{ main{
@ -180,10 +181,11 @@ def get_delete_comment_page(query_string):
style = style, style = style,
header = common.get_header(), header = common.get_header(),
page = page, page = page,
) ).encode('utf-8')
def get_post_comment_page(query_string): def get_post_comment_page(env, start_response):
parameters = urllib.parse.parse_qs(query_string) start_response('200 OK', [('Content-type','text/html'),])
parameters = env['fields']
video_id = parameters['video_id'][0] video_id = parameters['video_id'][0]
parent_id = common.default_multi_get(parameters, 'parent_id', 0, default='') parent_id = common.default_multi_get(parameters, 'parent_id', 0, default='')
@ -224,4 +226,4 @@ textarea{
style = style, style = style,
header = common.get_header(), header = common.get_header(),
page = page, page = page,
) ).encode('utf-8')

View File

@ -228,18 +228,23 @@ music_list_table_row = Template('''<tr>
<td>$attribute</td> <td>$attribute</td>
<td>$value</td> <td>$value</td>
''') ''')
def get_watch_page(query_string): def get_watch_page(env, start_response):
parsed_qs = urllib.parse.parse_qs(query_string) video_id = env['fields']['v'][0]
id = parsed_qs['v'][0] if len(video_id) < 11:
lc = common.default_multi_get(parsed_qs, 'lc', 0, default='') 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: if settings.route_tor:
proxy = 'socks5://127.0.0.1:9150/' proxy = 'socks5://127.0.0.1:9150/'
else: else:
proxy = '' proxy = ''
downloader = YoutubeDL(params={'youtube_include_dash_manifest':False, 'proxy':proxy}) downloader = YoutubeDL(params={'youtube_include_dash_manifest':False, 'proxy':proxy})
tasks = ( tasks = (
gevent.spawn(comments.video_comments, id, int(settings.default_comment_sorting), lc=lc ), 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=" + id, download=False) gevent.spawn(extract_info, downloader, "https://www.youtube.com/watch?v=" + video_id, download=False)
) )
gevent.joinall(tasks) gevent.joinall(tasks)
comments_html, info = tasks[0].value, tasks[1].value comments_html, info = tasks[0].value, tasks[1].value
@ -256,7 +261,7 @@ def get_watch_page(query_string):
style = "", style = "",
header = common.get_header(), header = common.get_header(),
page = html.escape(info), page = html.escape(info),
) ).encode('utf-8')
sorted_formats = sort_formats(info) sorted_formats = sort_formats(info)
@ -351,4 +356,4 @@ def get_watch_page(query_string):
music_list = music_list_html, music_list = music_list_html,
is_unlisted = '<span class="is-unlisted">Unlisted</span>' if info['unlisted'] else '', is_unlisted = '<span class="is-unlisted">Unlisted</span>' if info['unlisted'] else '',
) )
return page return page.encode('utf-8')

View File

@ -9,8 +9,14 @@ YOUTUBE_FILES = (
'/favicon.ico', '/favicon.ico',
) )
page_handlers = { page_handlers = {
'search': search.get_search_page, 'search': search.get_search_page,
'': 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): def youtube(env, start_response):
path, method, query_string = env['PATH_INFO'], env['REQUEST_METHOD'], env['QUERY_STRING'] 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' mime_type = mimetypes.guess_type(path)[0] or 'application/octet-stream'
start_response('200 OK', (('Content-type',mime_type),) ) start_response('200 OK', (('Content-type',mime_type),) )
return f.read() 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/"): elif path.startswith("/channel/"):
start_response('200 OK', (('Content-type','text/html'),) ) 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"") result = result.replace(b"align:start position:0%", b"")
return result return result
elif path == "/post_comment":
start_response('200 OK', () )
return post_comment.get_post_comment_page(query_string).encode()
elif path == "/opensearch.xml": elif path == "/opensearch.xml":
with open("youtube" + path, 'rb') as f: with open("youtube" + path, 'rb') as f:
mime_type = mimetypes.guess_type(path)[0] or 'application/octet-stream' mime_type = mimetypes.guess_type(path)[0] or 'application/octet-stream'
start_response('200 OK', (('Content-type',mime_type),) ) start_response('200 OK', (('Content-type',mime_type),) )
return f.read().replace(b'$port_number', str(settings.port_number).encode()) 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": elif path == "/comment_delete_success":
start_response('200 OK', () ) start_response('200 OK', () )
return b'Successfully deleted comment' return b'Successfully deleted comment'