searxng/searx/engines/youtube.py

61 lines
1.8 KiB
Python
Raw Normal View History

2013-10-19 18:46:10 +00:00
from json import loads
2013-10-23 21:55:37 +00:00
from urllib import urlencode
2014-03-18 12:19:50 +00:00
from dateutil import parser
from datetime import datetime
2013-10-19 18:46:10 +00:00
categories = ['videos']
2014-02-05 19:24:31 +00:00
search_url = ('https://gdata.youtube.com/feeds/api/videos'
'?alt=json&{query}&start-index={index}&max-results=25') # noqa
2014-01-29 23:50:47 +00:00
paging = True
2013-10-19 18:46:10 +00:00
2014-01-20 01:31:20 +00:00
2013-10-19 18:46:10 +00:00
def request(query, params):
2014-01-29 23:50:47 +00:00
index = (params['pageno'] - 1) * 25 + 1
params['url'] = search_url.format(query=urlencode({'q': query}),
index=index)
2013-10-19 18:46:10 +00:00
return params
def response(resp):
results = []
search_results = loads(resp.text)
if not 'feed' in search_results:
return results
feed = search_results['feed']
2014-02-11 12:13:51 +00:00
2013-10-19 18:46:10 +00:00
for result in feed['entry']:
url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
2014-02-11 12:13:51 +00:00
if not url:
2013-10-19 18:46:10 +00:00
return
# remove tracking
url = url[0].replace('feature=youtube_gdata', '')
if url.endswith('&'):
url = url[:-1]
title = result['title']['$t']
content = ''
thumbnail = ''
2014-02-11 12:13:51 +00:00
2014-03-18 12:19:50 +00:00
#"2013-12-31T15:22:51.000Z"
pubdate = result['published']['$t']
publishedDate = parser.parse(pubdate)
2014-02-11 12:13:51 +00:00
if result['media$group']['media$thumbnail']:
thumbnail = result['media$group']['media$thumbnail'][0]['url']
2014-01-20 01:31:20 +00:00
content += '<a href="{0}" title="{0}" ><img src="{1}" /></a>'.format(url, thumbnail) # noqa
2014-02-11 12:13:51 +00:00
if content:
content += '<br />' + result['content']['$t']
else:
content = result['content']['$t']
2014-01-20 01:31:20 +00:00
results.append({'url': url,
'title': title,
'content': content,
'template': 'videos.html',
2014-03-18 12:19:50 +00:00
'publishedDate': publishedDate,
2014-01-20 01:31:20 +00:00
'thumbnail': thumbnail})
2013-10-19 18:46:10 +00:00
return results