2015-05-02 15:45:17 +02:00
|
|
|
# Youtube (Videos)
|
2014-12-07 16:37:56 +01:00
|
|
|
#
|
2014-09-02 21:19:20 +02:00
|
|
|
# @website https://www.youtube.com/
|
|
|
|
# @provide-api yes (http://gdata-samples-youtube-search-py.appspot.com/)
|
2014-12-07 16:37:56 +01:00
|
|
|
#
|
2014-09-02 21:19:20 +02:00
|
|
|
# @using-api yes
|
|
|
|
# @results JSON
|
|
|
|
# @stable yes
|
2015-01-05 02:04:23 +01:00
|
|
|
# @parse url, title, content, publishedDate, thumbnail, embedded
|
2014-09-02 21:19:20 +02:00
|
|
|
|
2013-10-19 20:46:10 +02:00
|
|
|
from json import loads
|
2013-10-23 23:55:37 +02:00
|
|
|
from urllib import urlencode
|
2014-03-18 13:19:50 +01:00
|
|
|
from dateutil import parser
|
2013-10-19 20:46:10 +02:00
|
|
|
|
2014-09-02 21:19:20 +02:00
|
|
|
# engine dependent config
|
2014-10-13 14:51:15 +02:00
|
|
|
categories = ['videos', 'music']
|
2014-01-30 00:50:47 +01:00
|
|
|
paging = True
|
2014-09-02 21:19:20 +02:00
|
|
|
language_support = True
|
|
|
|
|
|
|
|
# search-url
|
|
|
|
base_url = 'https://gdata.youtube.com/feeds/api/videos'
|
2015-01-05 02:04:23 +01:00
|
|
|
search_url = base_url + '?alt=json&{query}&start-index={index}&max-results=5'
|
|
|
|
|
|
|
|
embedded_url = '<iframe width="540" height="304" ' +\
|
|
|
|
'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\
|
|
|
|
'frameborder="0" allowfullscreen></iframe>'
|
2013-10-19 20:46:10 +02:00
|
|
|
|
2014-01-20 02:31:20 +01:00
|
|
|
|
2014-09-02 21:19:20 +02:00
|
|
|
# do search-request
|
2013-10-19 20:46:10 +02:00
|
|
|
def request(query, params):
|
2014-09-02 21:19:20 +02:00
|
|
|
index = (params['pageno'] - 1) * 5 + 1
|
|
|
|
|
2014-01-30 00:50:47 +01:00
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}),
|
|
|
|
index=index)
|
2014-09-02 21:19:20 +02:00
|
|
|
|
|
|
|
# add language tag if specified
|
|
|
|
if params['language'] != 'all':
|
|
|
|
params['url'] += '&lr=' + params['language'].split('_')[0]
|
|
|
|
|
2013-10-19 20:46:10 +02:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-02 21:19:20 +02:00
|
|
|
# get response from search-request
|
2013-10-19 20:46:10 +02:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-09-02 21:19:20 +02:00
|
|
|
|
2013-10-19 20:46:10 +02:00
|
|
|
search_results = loads(resp.text)
|
2014-09-02 21:19:20 +02:00
|
|
|
|
|
|
|
# return empty array if there are no results
|
2015-05-02 15:45:17 +02:00
|
|
|
if 'feed' not in search_results:
|
2014-09-02 21:19:20 +02:00
|
|
|
return []
|
|
|
|
|
2013-10-19 20:46:10 +02:00
|
|
|
feed = search_results['feed']
|
2014-02-11 13:13:51 +01:00
|
|
|
|
2014-09-02 21:19:20 +02:00
|
|
|
# parse results
|
2013-10-19 20:46:10 +02:00
|
|
|
for result in feed['entry']:
|
|
|
|
url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
|
2014-09-02 21:19:20 +02:00
|
|
|
|
2014-02-11 13:13:51 +01:00
|
|
|
if not url:
|
2015-01-26 18:24:08 +01:00
|
|
|
continue
|
2014-09-02 21:19:20 +02:00
|
|
|
|
2013-10-19 20:46:10 +02:00
|
|
|
# remove tracking
|
|
|
|
url = url[0].replace('feature=youtube_gdata', '')
|
|
|
|
if url.endswith('&'):
|
|
|
|
url = url[:-1]
|
2014-09-02 21:19:20 +02:00
|
|
|
|
2015-01-05 02:04:23 +01:00
|
|
|
videoid = url[32:]
|
|
|
|
|
2013-10-19 20:46:10 +02:00
|
|
|
title = result['title']['$t']
|
2013-10-22 19:36:30 +02:00
|
|
|
content = ''
|
2014-01-12 18:31:57 +01:00
|
|
|
thumbnail = ''
|
2014-02-11 13:13:51 +01:00
|
|
|
|
2014-03-18 13:19:50 +01:00
|
|
|
pubdate = result['published']['$t']
|
|
|
|
publishedDate = parser.parse(pubdate)
|
|
|
|
|
2015-01-26 18:24:08 +01:00
|
|
|
if 'media$thumbnail' in result['media$group']:
|
2014-01-12 18:31:57 +01:00
|
|
|
thumbnail = result['media$group']['media$thumbnail'][0]['url']
|
2014-02-11 13:13:51 +01:00
|
|
|
|
2014-09-27 12:33:22 +02:00
|
|
|
content = result['content']['$t']
|
2013-10-22 19:36:30 +02:00
|
|
|
|
2015-01-05 02:04:23 +01:00
|
|
|
embedded = embedded_url.format(videoid=videoid)
|
|
|
|
|
2014-09-02 21:19:20 +02:00
|
|
|
# append result
|
2014-01-20 02:31:20 +01:00
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content,
|
|
|
|
'template': 'videos.html',
|
2014-03-18 13:19:50 +01:00
|
|
|
'publishedDate': publishedDate,
|
2015-01-05 02:04:23 +01:00
|
|
|
'embedded': embedded,
|
2014-01-20 02:31:20 +01:00
|
|
|
'thumbnail': thumbnail})
|
2013-10-19 20:46:10 +02:00
|
|
|
|
2014-09-02 21:19:20 +02:00
|
|
|
# return results
|
2013-10-19 20:46:10 +02:00
|
|
|
return results
|