2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
"""
|
|
|
|
Youtube (Videos)
|
|
|
|
"""
|
2015-05-30 22:25:59 +00:00
|
|
|
|
2021-04-21 11:09:50 +00:00
|
|
|
from datetime import datetime
|
2019-03-27 02:33:36 +00:00
|
|
|
from functools import reduce
|
2021-03-15 16:21:46 +00:00
|
|
|
from json import loads, dumps
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import quote_plus
|
2015-05-30 22:25:59 +00:00
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://www.youtube.com/',
|
|
|
|
"wikidata_id": 'Q866',
|
|
|
|
"official_api_documentation": 'https://developers.google.com/youtube/v3/docs/search/list?apix=true',
|
|
|
|
"use_official_api": False,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'HTML',
|
|
|
|
}
|
|
|
|
|
2015-05-30 22:25:59 +00:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['videos', 'music']
|
|
|
|
paging = True
|
2021-03-15 16:21:46 +00:00
|
|
|
language_support = False
|
2016-10-30 19:26:38 +00:00
|
|
|
time_range_support = True
|
2015-05-30 22:25:59 +00:00
|
|
|
|
|
|
|
# search-url
|
|
|
|
base_url = 'https://www.youtube.com/results'
|
|
|
|
search_url = base_url + '?search_query={query}&page={page}'
|
2016-10-30 19:26:38 +00:00
|
|
|
time_range_url = '&sp=EgII{time_range}%253D%253D'
|
2021-03-15 16:21:46 +00:00
|
|
|
# the key seems to be constant
|
|
|
|
next_page_url = 'https://www.youtube.com/youtubei/v1/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'
|
2021-12-27 08:26:22 +00:00
|
|
|
time_range_dict = {'day': 'Ag', 'week': 'Aw', 'month': 'BA', 'year': 'BQ'}
|
2015-05-30 22:25:59 +00:00
|
|
|
|
|
|
|
base_youtube_url = 'https://www.youtube.com/watch?v='
|
|
|
|
|
2015-06-03 08:12:30 +00:00
|
|
|
|
2015-05-30 22:25:59 +00:00
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
2021-03-15 16:21:46 +00:00
|
|
|
if not params['engine_data'].get('next_page_token'):
|
|
|
|
params['url'] = search_url.format(query=quote_plus(query), page=params['pageno'])
|
|
|
|
if params['time_range'] in time_range_dict:
|
|
|
|
params['url'] += time_range_url.format(time_range=time_range_dict[params['time_range']])
|
|
|
|
else:
|
|
|
|
params['url'] = next_page_url
|
|
|
|
params['method'] = 'POST'
|
2021-12-27 08:26:22 +00:00
|
|
|
params['data'] = dumps(
|
|
|
|
{
|
|
|
|
'context': {"client": {"clientName": "WEB", "clientVersion": "2.20210310.12.01"}},
|
|
|
|
'continuation': params['engine_data']['next_page_token'],
|
|
|
|
}
|
|
|
|
)
|
2021-03-15 16:21:46 +00:00
|
|
|
params['headers']['Content-Type'] = 'application/json'
|
2015-05-30 22:25:59 +00:00
|
|
|
|
2021-04-21 11:09:50 +00:00
|
|
|
params['headers']['Cookie'] = "CONSENT=YES+cb.%s-17-p0.en+F+941;" % datetime.now().strftime("%Y%m%d")
|
2015-05-30 22:25:59 +00:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
2021-03-15 16:21:46 +00:00
|
|
|
if resp.search_params.get('engine_data'):
|
|
|
|
return parse_next_page_response(resp.text)
|
|
|
|
return parse_first_page_response(resp.text)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_next_page_response(response_text):
|
2015-05-30 22:25:59 +00:00
|
|
|
results = []
|
2021-03-15 16:21:46 +00:00
|
|
|
result_json = loads(response_text)
|
2021-12-27 08:26:22 +00:00
|
|
|
for section in (
|
|
|
|
result_json['onResponseReceivedCommands'][0]
|
|
|
|
.get('appendContinuationItemsAction')['continuationItems'][0]
|
|
|
|
.get('itemSectionRenderer')['contents']
|
|
|
|
):
|
2021-03-15 16:21:46 +00:00
|
|
|
if 'videoRenderer' not in section:
|
|
|
|
continue
|
|
|
|
section = section['videoRenderer']
|
|
|
|
content = "-"
|
|
|
|
if 'descriptionSnippet' in section:
|
|
|
|
content = ' '.join(x['text'] for x in section['descriptionSnippet']['runs'])
|
2021-12-27 08:26:22 +00:00
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
'url': base_youtube_url + section['videoId'],
|
|
|
|
'title': ' '.join(x['text'] for x in section['title']['runs']),
|
|
|
|
'content': content,
|
|
|
|
'author': section['ownerText']['runs'][0]['text'],
|
|
|
|
'length': section['lengthText']['simpleText'],
|
|
|
|
'template': 'videos.html',
|
2022-02-07 15:16:57 +00:00
|
|
|
'data_src': 'https://www.youtube-nocookie.com/embed/' + section['videoId'],
|
2021-12-27 08:26:22 +00:00
|
|
|
'thumbnail': section['thumbnail']['thumbnails'][-1]['url'],
|
|
|
|
}
|
|
|
|
)
|
2021-03-15 16:21:46 +00:00
|
|
|
try:
|
2021-12-27 08:26:22 +00:00
|
|
|
token = (
|
|
|
|
result_json['onResponseReceivedCommands'][0]
|
|
|
|
.get('appendContinuationItemsAction')['continuationItems'][1]
|
|
|
|
.get('continuationItemRenderer')['continuationEndpoint']
|
2021-03-15 16:21:46 +00:00
|
|
|
.get('continuationCommand')['token']
|
2021-12-27 08:26:22 +00:00
|
|
|
)
|
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
"engine_data": token,
|
|
|
|
"key": "next_page_token",
|
|
|
|
}
|
|
|
|
)
|
2021-03-15 16:21:46 +00:00
|
|
|
except:
|
|
|
|
pass
|
2015-05-30 22:25:59 +00:00
|
|
|
|
2021-03-15 16:21:46 +00:00
|
|
|
return results
|
2019-03-27 02:33:36 +00:00
|
|
|
|
2021-03-15 16:21:46 +00:00
|
|
|
|
|
|
|
def parse_first_page_response(response_text):
|
|
|
|
results = []
|
2021-12-27 08:26:22 +00:00
|
|
|
results_data = response_text[response_text.find('ytInitialData') :]
|
|
|
|
results_data = results_data[results_data.find('{') : results_data.find(';</script>')]
|
2019-03-27 02:33:36 +00:00
|
|
|
results_json = loads(results_data) if results_data else {}
|
2021-12-27 08:26:22 +00:00
|
|
|
sections = (
|
|
|
|
results_json.get('contents', {})
|
|
|
|
.get('twoColumnSearchResultsRenderer', {})
|
|
|
|
.get('primaryContents', {})
|
|
|
|
.get('sectionListRenderer', {})
|
|
|
|
.get('contents', [])
|
|
|
|
)
|
2019-03-27 02:33:36 +00:00
|
|
|
|
|
|
|
for section in sections:
|
2021-03-15 16:21:46 +00:00
|
|
|
if "continuationItemRenderer" in section:
|
2021-12-27 08:26:22 +00:00
|
|
|
next_page_token = (
|
|
|
|
section["continuationItemRenderer"]
|
|
|
|
.get("continuationEndpoint", {})
|
|
|
|
.get("continuationCommand", {})
|
2021-03-15 16:21:46 +00:00
|
|
|
.get("token", "")
|
2021-12-27 08:26:22 +00:00
|
|
|
)
|
2021-03-15 16:21:46 +00:00
|
|
|
if next_page_token:
|
2021-12-27 08:26:22 +00:00
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
"engine_data": next_page_token,
|
|
|
|
"key": "next_page_token",
|
|
|
|
}
|
|
|
|
)
|
2019-03-27 02:33:36 +00:00
|
|
|
for video_container in section.get('itemSectionRenderer', {}).get('contents', []):
|
|
|
|
video = video_container.get('videoRenderer', {})
|
|
|
|
videoid = video.get('videoId')
|
|
|
|
if videoid is not None:
|
|
|
|
url = base_youtube_url + videoid
|
|
|
|
thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
|
2019-07-31 06:39:40 +00:00
|
|
|
title = get_text_from_json(video.get('title', {}))
|
|
|
|
content = get_text_from_json(video.get('descriptionSnippet', {}))
|
2020-06-09 18:31:51 +00:00
|
|
|
author = get_text_from_json(video.get('ownerText', {}))
|
|
|
|
length = get_text_from_json(video.get('lengthText', {}))
|
2019-03-27 02:33:36 +00:00
|
|
|
|
|
|
|
# append result
|
2021-12-27 08:26:22 +00:00
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'author': author,
|
|
|
|
'length': length,
|
|
|
|
'template': 'videos.html',
|
2022-02-07 15:16:57 +00:00
|
|
|
'data_src': 'https://www.youtube-nocookie.com/embed/' + videoid,
|
2021-12-27 08:26:22 +00:00
|
|
|
'thumbnail': thumbnail,
|
|
|
|
}
|
|
|
|
)
|
2015-05-30 22:25:59 +00:00
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|
2019-07-31 06:39:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_text_from_json(element):
|
|
|
|
if 'runs' in element:
|
|
|
|
return reduce(lambda a, b: a + b.get('text', ''), element.get('runs'), '')
|
|
|
|
else:
|
|
|
|
return element.get('simpleText', '')
|