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
|
|
|
|
|
|
|
from json import loads
|
|
|
|
from dateutil import parser
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlencode
|
2020-11-26 16:22:54 +00:00
|
|
|
from searx.exceptions import SearxEngineAPIException
|
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": True,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'JSON',
|
|
|
|
}
|
|
|
|
|
2015-05-30 22:25:59 +00:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['videos', 'music']
|
|
|
|
paging = False
|
|
|
|
api_key = None
|
|
|
|
|
|
|
|
# search-url
|
|
|
|
base_url = 'https://www.googleapis.com/youtube/v3/search'
|
|
|
|
search_url = base_url + '?part=snippet&{query}&maxResults=20&key={api_key}'
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
embedded_url = (
|
|
|
|
'<iframe width="540" height="304" '
|
|
|
|
+ 'data-src="https://www.youtube-nocookie.com/embed/{videoid}" '
|
|
|
|
+ 'frameborder="0" allowfullscreen></iframe>'
|
|
|
|
)
|
2015-05-30 22:25:59 +00:00
|
|
|
|
|
|
|
base_youtube_url = 'https://www.youtube.com/watch?v='
|
|
|
|
|
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
2021-12-27 08:26:22 +00:00
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}), api_key=api_key)
|
2015-05-30 22:25:59 +00:00
|
|
|
|
2019-01-06 14:27:46 +00:00
|
|
|
# add language tag if specified
|
|
|
|
if params['language'] != 'all':
|
|
|
|
params['url'] += '&relevanceLanguage=' + params['language'].split('-')[0]
|
2015-05-30 22:25:59 +00:00
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
|
|
|
|
|
|
|
search_results = loads(resp.text)
|
|
|
|
|
2020-10-09 15:34:26 +00:00
|
|
|
if 'error' in search_results and 'message' in search_results['error']:
|
2020-11-26 16:22:54 +00:00
|
|
|
raise SearxEngineAPIException(search_results['error']['message'])
|
2020-10-09 15:34:26 +00:00
|
|
|
|
2015-05-30 22:25:59 +00:00
|
|
|
# return empty array if there are no results
|
|
|
|
if 'items' not in search_results:
|
|
|
|
return []
|
|
|
|
|
|
|
|
# parse results
|
|
|
|
for result in search_results['items']:
|
|
|
|
videoid = result['id']['videoId']
|
|
|
|
|
|
|
|
title = result['snippet']['title']
|
|
|
|
content = ''
|
|
|
|
thumbnail = ''
|
|
|
|
|
|
|
|
pubdate = result['snippet']['publishedAt']
|
|
|
|
publishedDate = parser.parse(pubdate)
|
|
|
|
|
|
|
|
thumbnail = result['snippet']['thumbnails']['high']['url']
|
|
|
|
|
|
|
|
content = result['snippet']['description']
|
|
|
|
|
|
|
|
url = base_youtube_url + videoid
|
|
|
|
|
|
|
|
embedded = embedded_url.format(videoid=videoid)
|
|
|
|
|
|
|
|
# append result
|
2021-12-27 08:26:22 +00:00
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'template': 'videos.html',
|
|
|
|
'publishedDate': publishedDate,
|
|
|
|
'embedded': embedded,
|
|
|
|
'thumbnail': thumbnail,
|
|
|
|
}
|
|
|
|
)
|
2015-05-30 22:25:59 +00:00
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|