diff options
author | James Taylor <user234683@users.noreply.github.com> | 2019-01-04 03:33:07 -0800 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2019-01-04 03:33:07 -0800 |
commit | ec2fb71a3701379246e3b9210cce25aa3f548fcc (patch) | |
tree | fe1b69c51fae5ffaada65312939c0d20d9a297a3 /youtube | |
parent | ea0705a98d69343e0b3a81168dee3d68a711550f (diff) | |
download | yt-local-ec2fb71a3701379246e3b9210cce25aa3f548fcc.tar.lz yt-local-ec2fb71a3701379246e3b9210cce25aa3f548fcc.tar.xz yt-local-ec2fb71a3701379246e3b9210cce25aa3f548fcc.zip |
WSGI for search
Diffstat (limited to 'youtube')
-rw-r--r-- | youtube/search.py | 33 | ||||
-rw-r--r-- | youtube/youtube.py | 30 |
2 files changed, 38 insertions, 25 deletions
diff --git a/youtube/search.py b/youtube/search.py index 2237290..2b90998 100644 --- a/youtube/search.py +++ b/youtube/search.py @@ -66,23 +66,24 @@ showing_results_for = Template(''' did_you_mean = Template(''' <div>Did you mean <a href="$corrected_query_url">$corrected_query</a></div> ''') -def get_search_page(query_string, parameters=()): - qs_query = urllib.parse.parse_qs(query_string) - if len(qs_query) == 0: +def get_search_page(env, start_response): + start_response('200 OK', [('Content-type','text/html'),]) + fields = env['fields'] + if len(fields) == 0: return common.yt_basic_template.substitute( page_title = "Search", header = common.get_header(), style = '', page = '', - ) - query = qs_query["query"][0] - page = qs_query.get("page", "1")[0] - autocorrect = int(qs_query.get("autocorrect", "1")[0]) - sort = int(qs_query.get("sort", "0")[0]) + ).encode('utf-8') + query = fields["query"][0] + page = fields.get("page", "1")[0] + autocorrect = int(fields.get("autocorrect", "1")[0]) + sort = int(fields.get("sort", "0")[0]) filters = {} - filters['time'] = int(qs_query.get("time", "0")[0]) - filters['type'] = int(qs_query.get("type", "0")[0]) - filters['duration'] = int(qs_query.get("duration", "0")[0]) + filters['time'] = int(fields.get("time", "0")[0]) + filters['type'] = int(fields.get("type", "0")[0]) + filters['duration'] = int(fields.get("duration", "0")[0]) info = get_search_json(query, page, autocorrect, sort, filters) estimated_results = int(info[1]['response']['estimatedResults']) @@ -97,7 +98,7 @@ def get_search_page(query_string, parameters=()): continue if type == 'didYouMeanRenderer': renderer = renderer[type] - corrected_query_string = urllib.parse.parse_qs(query_string) + corrected_query_string = fields.copy() corrected_query_string['query'] = [renderer['correctedQueryEndpoint']['searchEndpoint']['query']] corrected_query_url = URL_ORIGIN + '/search?' + urllib.parse.urlencode(corrected_query_string, doseq=True) corrections = did_you_mean.substitute( @@ -107,7 +108,7 @@ def get_search_page(query_string, parameters=()): continue if type == 'showingResultsForRenderer': renderer = renderer[type] - no_autocorrect_query_string = urllib.parse.parse_qs(query_string) + no_autocorrect_query_string = fields.copy() no_autocorrect_query_string['autocorrect'] = ['0'] no_autocorrect_query_url = URL_ORIGIN + '/search?' + urllib.parse.urlencode(no_autocorrect_query_string, doseq=True) corrections = showing_results_for.substitute( @@ -116,7 +117,7 @@ def get_search_page(query_string, parameters=()): original_query = html.escape(renderer['originalQuery']['simpleText']), ) continue - result_list_html += common.renderer_html(renderer, current_query_string=query_string) + result_list_html += common.renderer_html(renderer, current_query_string=env['QUERY_STRING']) page = int(page) if page <= 5: @@ -134,7 +135,7 @@ def get_search_page(query_string, parameters=()): search_box_value = html.escape(query), number_of_results = '{:,}'.format(estimated_results), number_of_pages = '{:,}'.format(estimated_pages), - page_buttons = common.page_buttons_html(page, estimated_pages, URL_ORIGIN + "/search", query_string), + page_buttons = common.page_buttons_html(page, estimated_pages, URL_ORIGIN + "/search", env['QUERY_STRING']), corrections = corrections ) - return result
\ No newline at end of file + return result.encode('utf-8')
\ No newline at end of file diff --git a/youtube/youtube.py b/youtube/youtube.py index c4a0e5b..44f9e3d 100644 --- a/youtube/youtube.py +++ b/youtube/youtube.py @@ -8,18 +8,31 @@ YOUTUBE_FILES = ( '/comments.css', '/favicon.ico', ) - +page_handlers = { + 'search': search.get_search_page, + '': search.get_search_page, +} def youtube(env, start_response): path, method, query_string = env['PATH_INFO'], env['REQUEST_METHOD'], env['QUERY_STRING'] + env['qs_fields'] = urllib.parse.parse_qs(query_string) + env['fields'] = dict(env['qs_fields']) + + path_parts = path.rstrip('/').lstrip('/').split('/') + env['path_parts'] = path_parts + if method == "GET": + try: + handler = page_handlers[path_parts[0]] + except KeyError: + pass + else: + return handler(env, start_response) + if path in YOUTUBE_FILES: 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() - elif path.lstrip('/') == "": - start_response('200 OK', (('Content-type','text/html'),) ) - return search.get_search_page(query_string).encode() elif path == "/comments": start_response('200 OK', (('Content-type','text/html'),) ) @@ -33,10 +46,6 @@ def youtube(env, start_response): start_response('200 OK', (('Content-type','text/html'),) ) return watch.get_watch_page(query_string).encode() - elif path == "/search": - start_response('200 OK', (('Content-type','text/html'),) ) - return search.get_search_page(query_string).encode() - elif path == "/playlist": start_response('200 OK', (('Content-type','text/html'),) ) return playlist.get_playlist_page(query_string).encode() @@ -95,7 +104,10 @@ def youtube(env, start_response): 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()) + post_fields = urllib.parse.parse_qs(env['wsgi.input'].read().decode()) + env['post_fields'] = post_fields + env['fields'].update(post_fields) + fields = post_fields if path == "/edit_playlist": if fields['action'][0] == 'add': local_playlist.add_to_playlist(fields['playlist_name'][0], fields['video_info_list']) |