2013-10-19 18:46:10 +00:00
|
|
|
from json import loads
|
2013-10-23 21:55:37 +00:00
|
|
|
from urllib import urlencode
|
2013-10-19 18:46:10 +00:00
|
|
|
|
|
|
|
categories = ['videos']
|
|
|
|
|
2013-10-23 21:55:37 +00:00
|
|
|
search_url = 'https://gdata.youtube.com/feeds/api/videos?alt=json&{query}'
|
2013-10-19 18:46:10 +00:00
|
|
|
|
|
|
|
def request(query, params):
|
2013-10-23 21:55:37 +00:00
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}))
|
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']
|
|
|
|
for result in feed['entry']:
|
|
|
|
url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
|
|
|
|
if not len(url):
|
|
|
|
return
|
|
|
|
# remove tracking
|
|
|
|
url = url[0].replace('feature=youtube_gdata', '')
|
|
|
|
if url.endswith('&'):
|
|
|
|
url = url[:-1]
|
|
|
|
title = result['title']['$t']
|
2013-10-22 17:36:30 +00:00
|
|
|
content = ''
|
|
|
|
if len(result['media$group']['media$thumbnail']):
|
2013-11-02 17:18:45 +00:00
|
|
|
content += '<a href="{0}" title="{0}" ><img src="{1}" /></a>'.format(url, result['media$group']['media$thumbnail'][0]['url'])
|
2013-10-22 17:36:30 +00:00
|
|
|
if len(content):
|
|
|
|
content += '<br />' + result['content']['$t']
|
|
|
|
else:
|
|
|
|
content = result['content']['$t']
|
|
|
|
|
2013-10-19 18:46:10 +00:00
|
|
|
results.append({'url': url, 'title': title, 'content': content})
|
|
|
|
|
|
|
|
return results
|
|
|
|
|