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.py

85 lines
2.2 KiB
Python
Raw Normal View History

2014-12-01 12:26:38 +01:00
## Youtube (Videos)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @website https://www.youtube.com/
# @provide-api yes (http://gdata-samples-youtube-search-py.appspot.com/)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @using-api yes
# @results JSON
# @stable yes
# @parse url, title, content, publishedDate, thumbnail
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']
2014-05-10 11:56:19 +02:00
paging = True
2014-12-01 12:26:38 +01:00
language_support = True
# search-url
base_url = 'https://gdata.youtube.com/feeds/api/videos'
search_url = base_url + '?alt=json&{query}&start-index={index}&max-results=5' # noqa
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):
2014-12-01 12:26:38 +01:00
index = (params['pageno'] - 1) * 5 + 1
2014-05-10 11:56:19 +02:00
params['url'] = search_url.format(query=urlencode({'q': query}),
index=index)
2014-12-01 12:26:38 +01:00
# add language tag if specified
if params['language'] != 'all':
params['url'] += '&lr=' + params['language'].split('_')[0]
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
2014-05-10 11:56:19 +02:00
if not 'feed' in search_results:
2014-12-01 12:26:38 +01:00
return []
2014-05-10 11:56:19 +02:00
feed = search_results['feed']
2014-12-01 12:26:38 +01:00
# parse results
2014-05-10 11:56:19 +02:00
for result in feed['entry']:
url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
if not url:
return
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
# remove tracking
url = url[0].replace('feature=youtube_gdata', '')
if url.endswith('&'):
url = url[:-1]
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
title = result['title']['$t']
content = ''
thumbnail = ''
pubdate = result['published']['$t']
publishedDate = parser.parse(pubdate)
if result['media$group']['media$thumbnail']:
thumbnail = result['media$group']['media$thumbnail'][0]['url']
2014-12-01 12:26:38 +01:00
content = result['content']['$t']
2014-05-10 11:56:19 +02:00
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,
'thumbnail': thumbnail})
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results