forked from zaclys/searxng
ca93a01844
The language_support variable is set to True by default, and set to False in only 5 engines. Except the documentation and the /config URL, this variable is not used. This commit remove the variable definition in the engines, and set value according to supported_languages length: False when the length is 0, True otherwise. Close #2485
92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""
|
|
Youtube (Videos)
|
|
"""
|
|
|
|
from json import loads
|
|
from dateutil import parser
|
|
from urllib.parse import urlencode
|
|
from searx.exceptions import SearxEngineAPIException
|
|
|
|
# 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',
|
|
}
|
|
|
|
# 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}'
|
|
|
|
embedded_url = '<iframe width="540" height="304" ' +\
|
|
'data-src="https://www.youtube-nocookie.com/embed/{videoid}" ' +\
|
|
'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)
|
|
|
|
# add language tag if specified
|
|
if params['language'] != 'all':
|
|
params['url'] += '&relevanceLanguage=' + params['language'].split('-')[0]
|
|
|
|
return params
|
|
|
|
|
|
# get response from search-request
|
|
def response(resp):
|
|
results = []
|
|
|
|
search_results = loads(resp.text)
|
|
|
|
if 'error' in search_results and 'message' in search_results['error']:
|
|
raise SearxEngineAPIException(search_results['error']['message'])
|
|
|
|
# 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
|