2015-05-30 22:25:59 +00:00
|
|
|
# Youtube (Videos)
|
|
|
|
#
|
|
|
|
# @website https://www.youtube.com/
|
|
|
|
# @provide-api yes (https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list)
|
|
|
|
#
|
|
|
|
# @using-api yes
|
|
|
|
# @results JSON
|
|
|
|
# @stable yes
|
|
|
|
# @parse url, title, content, publishedDate, thumbnail, embedded
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# engine dependent config
|
|
|
|
categories = ['videos', 'music']
|
|
|
|
paging = False
|
|
|
|
language_support = True
|
|
|
|
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}'
|
|
|
|
|
|
|
|
embedded_url = '<iframe width="540" height="304" ' +\
|
2019-07-13 05:57:10 +00:00
|
|
|
'data-src="https://www.youtube-nocookie.com/embed/{videoid}" ' +\
|
2015-05-30 22:25:59 +00:00
|
|
|
'frameborder="0" allowfullscreen></iframe>'
|
|
|
|
|
|
|
|
base_youtube_url = 'https://www.youtube.com/watch?v='
|
|
|
|
|
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}),
|
|
|
|
api_key=api_key)
|
|
|
|
|
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
|
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'template': 'videos.html',
|
|
|
|
'publishedDate': publishedDate,
|
|
|
|
'embedded': embedded,
|
|
|
|
'thumbnail': thumbnail})
|
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|