1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/youtube_api.py

84 lines
2.3 KiB
Python
Raw Normal View History

2015-09-08 23:05:37 +02:00
# Youtube (Videos)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @website https://www.youtube.com/
2015-09-08 23:05:37 +02:00
# @provide-api yes (https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @using-api yes
# @results JSON
# @stable yes
2015-01-13 17:13:08 +01:00
# @parse url, title, content, publishedDate, thumbnail, embedded
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
from json import loads
from urllib import urlencode
from dateutil import parser
2014-12-01 12:26:38 +01:00
# engine dependent config
categories = ['videos', 'music']
2015-09-08 23:05:37 +02:00
paging = False
2014-12-01 12:26:38 +01:00
language_support = True
2015-09-08 23:05:37 +02:00
api_key = None
2014-12-01 12:26:38 +01:00
# search-url
2015-09-08 23:05:37 +02:00
base_url = 'https://www.googleapis.com/youtube/v3/search'
search_url = base_url + '?part=snippet&{query}&maxResults=20&key={api_key}'
2015-01-13 17:13:08 +01:00
embedded_url = '<iframe width="540" height="304" ' +\
'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\
'frameborder="0" allowfullscreen></iframe>'
2014-05-10 11:56:19 +02:00
2015-09-08 23:05:37 +02:00
base_youtube_url = 'https://www.youtube.com/watch?v='
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
params['url'] = search_url.format(query=urlencode({'q': query}),
2015-09-08 23:05:37 +02:00
api_key=api_key)
2014-12-01 12:26:38 +01:00
# add language tag if specified
if params['language'] != 'all':
2015-09-08 23:05:37 +02:00
params['url'] += '&relevanceLanguage=' + params['language'].split('_')[0]
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
search_results = loads(resp.text)
2014-12-01 12:26:38 +01:00
# return empty array if there are no results
2015-09-08 23:05:37 +02:00
if 'items' not in search_results:
2014-12-01 12:26:38 +01:00
return []
# parse results
2015-09-08 23:05:37 +02:00
for result in search_results['items']:
videoid = result['id']['videoId']
2014-12-01 12:26:38 +01:00
2015-09-08 23:05:37 +02:00
title = result['snippet']['title']
2014-05-10 11:56:19 +02:00
content = ''
thumbnail = ''
2015-09-08 23:05:37 +02:00
pubdate = result['snippet']['publishedAt']
2014-05-10 11:56:19 +02:00
publishedDate = parser.parse(pubdate)
2015-09-08 23:05:37 +02:00
thumbnail = result['snippet']['thumbnails']['high']['url']
content = result['snippet']['description']
2014-05-10 11:56:19 +02:00
2015-09-08 23:05:37 +02:00
url = base_youtube_url + videoid
2014-05-10 11:56:19 +02:00
2015-01-13 17:13:08 +01:00
embedded = embedded_url.format(videoid=videoid)
2014-12-01 12:26:38 +01:00
# append result
2014-05-10 11:56:19 +02:00
results.append({'url': url,
'title': title,
'content': content,
'template': 'videos.html',
'publishedDate': publishedDate,
2015-01-13 17:13:08 +01:00
'embedded': embedded,
2014-05-10 11:56:19 +02:00
'thumbnail': thumbnail})
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results