fix youtube engine and add comments

* add language-support
* decrease search-results/site to 5
* add comments
This commit is contained in:
Thomas Pointhuber 2014-09-02 21:19:20 +02:00
parent bb628469d3
commit 629a05e149
2 changed files with 36 additions and 10 deletions

View file

@ -1,42 +1,69 @@
## Youtube (Videos)
#
# @website https://www.youtube.com/
# @provide-api yes (http://gdata-samples-youtube-search-py.appspot.com/)
#
# @using-api yes
# @results JSON
# @stable yes
# @parse url, title, content, publishedDate, thumbnail
from json import loads from json import loads
from urllib import urlencode from urllib import urlencode
from dateutil import parser from dateutil import parser
# engine dependent config
categories = ['videos'] categories = ['videos']
search_url = ('https://gdata.youtube.com/feeds/api/videos'
'?alt=json&{query}&start-index={index}&max-results=25') # noqa
paging = True paging = True
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
# do search-request
def request(query, params): def request(query, params):
index = (params['pageno'] - 1) * 25 + 1 index = (params['pageno'] - 1) * 5 + 1
params['url'] = search_url.format(query=urlencode({'q': query}), params['url'] = search_url.format(query=urlencode({'q': query}),
index=index) index=index)
# add language tag if specified
if params['language'] != 'all':
params['url'] += '&lr=' + params['language'].split('_')[0]
return params return params
# get response from search-request
def response(resp): def response(resp):
results = [] results = []
search_results = loads(resp.text) search_results = loads(resp.text)
# return empty array if there are no results
if not 'feed' in search_results: if not 'feed' in search_results:
return results return []
feed = search_results['feed'] feed = search_results['feed']
# parse results
for result in feed['entry']: for result in feed['entry']:
url = [x['href'] for x in result['link'] if x['type'] == 'text/html'] url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
if not url: if not url:
return return
# remove tracking # remove tracking
url = url[0].replace('feature=youtube_gdata', '') url = url[0].replace('feature=youtube_gdata', '')
if url.endswith('&'): if url.endswith('&'):
url = url[:-1] url = url[:-1]
title = result['title']['$t'] title = result['title']['$t']
content = '' content = ''
thumbnail = '' thumbnail = ''
#"2013-12-31T15:22:51.000Z"
pubdate = result['published']['$t'] pubdate = result['published']['$t']
publishedDate = parser.parse(pubdate) publishedDate = parser.parse(pubdate)
@ -49,6 +76,7 @@ def response(resp):
else: else:
content = result['content']['$t'] content = result['content']['$t']
# append result
results.append({'url': url, results.append({'url': url,
'title': title, 'title': title,
'content': content, 'content': content,
@ -56,4 +84,5 @@ def response(resp):
'publishedDate': publishedDate, 'publishedDate': publishedDate,
'thumbnail': thumbnail}) 'thumbnail': thumbnail})
# return results
return results return results

View file

@ -131,13 +131,10 @@ engines:
- name : youtube - name : youtube
engine : youtube engine : youtube
categories : videos
shortcut : yt shortcut : yt
- name : dailymotion - name : dailymotion
engine : dailymotion engine : dailymotion
locale : en_US
categories : videos
shortcut : dm shortcut : dm
- name : vimeo - name : vimeo