diff options
Diffstat (limited to 'youtube/search.py')
-rw-r--r-- | youtube/search.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/youtube/search.py b/youtube/search.py index 2307656..0f77595 100644 --- a/youtube/search.py +++ b/youtube/search.py @@ -20,6 +20,7 @@ current_page_button_template = Template('''<div class="page-button">$page</div>' # Upload date: 2 # View count: 3 # Rating: 1 + # Relevance: 0 # Offset: 9 # Filters: 2 # Upload date: 1 @@ -40,12 +41,13 @@ features = { 'location': 23, } -def page_number_to_sp_parameter(page, autocorrect=1): +def page_number_to_sp_parameter(page, autocorrect=1, sort = 0): offset = (int(page) - 1)*20 # 20 results per page autocorrect = proto.nested(8, proto.uint(1, 1 - int(autocorrect) )) - return base64.urlsafe_b64encode(proto.uint(9, offset) + proto.string(61, b'') + autocorrect).decode('ascii') + result = proto.uint(1, sort) + proto.uint(9, offset) + proto.string(61, b'') + autocorrect + return base64.urlsafe_b64encode(result).decode('ascii') -def get_search_json(query, page, autocorrect): +def get_search_json(query, page, autocorrect, sort): url = "https://www.youtube.com/results?search_query=" + urllib.parse.quote_plus(query) headers = { 'Host': 'www.youtube.com', @@ -55,7 +57,7 @@ def get_search_json(query, page, autocorrect): 'X-YouTube-Client-Name': '1', 'X-YouTube-Client-Version': '2.20180418', } - url += "&pbj=1&sp=" + page_number_to_sp_parameter(page, autocorrect) + url += "&pbj=1&sp=" + page_number_to_sp_parameter(page, autocorrect, sort) content = common.fetch_url(url, headers=headers) info = json.loads(content) return info @@ -84,9 +86,9 @@ def get_search_page(query_string, parameters=()): return yt_search_template query = qs_query["query"][0] page = qs_query.get("page", "1")[0] - autocorrect = qs_query.get("autocorrect", "1")[0] - - info = get_search_json(query, page, autocorrect) + autocorrect = int(qs_query.get("autocorrect", "1")[0]) + sort = int(qs_query.get("sort", "0")[0]) + info = get_search_json(query, page, autocorrect, sort) estimated_results = int(info[1]['response']['estimatedResults']) estimated_pages = ceil(estimated_results/20) |