@@ -1,42 +1,69 @@
1+## Youtube (Videos)
2+#
3+# @website https://www.youtube.com/
4+# @provide-api yes (http://gdata-samples-youtube-search-py.appspot.com/)
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes
9+# @parse url, title, content, publishedDate, thumbnail
10+
111from json import loads
212from urllib import urlencode
313from dateutil import parser
414
15+# engine dependent config
516categories = ['videos']
6−
7−search_url = ('https://gdata.youtube.com/feeds/api/videos'
8− '?alt=json&{query}&start-index={index}&max-results=25') # noqa
9−
1017paging = True
18+language_support = True
19+
20+# search-url
21+base_url = 'https://gdata.youtube.com/feeds/api/videos'
22+search_url = base_url + '?alt=json&{query}&start-index={index}&max-results=5' # noqa
1123
1224
25+# do search-request
1326def request(query, params):
14− index = (params['pageno'] - 1) * 25 + 1
27+ index = (params['pageno'] - 1) * 5 + 1
28+
1529 params['url'] = search_url.format(query=urlencode({'q': query}),
1630 index=index)
31+
32+ # add language tag if specified
33+ if params['language'] != 'all':
34+ params['url'] += '&lr=' + params['language'].split('_')[0]
35+
1736 return params
1837
1938
39+# get response from search-request
2040def response(resp):
2141 results = []
42+
2243 search_results = loads(resp.text)
44+
45+ # return empty array if there are no results
2346 if not 'feed' in search_results:
24− return results
47+ return []
48+
2549 feed = search_results['feed']
2650
51+ # parse results
2752 for result in feed['entry']:
2853 url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
54+
2955 if not url:
3056 return
57+
3158 # remove tracking
3259 url = url[0].replace('feature=youtube_gdata', '')
3360 if url.endswith('&'):
3461 url = url[:-1]
62+
3563 title = result['title']['$t']
3664 content = ''
3765 thumbnail = ''
3866
39−#"2013-12-31T15:22:51.000Z"
4067 pubdate = result['published']['$t']
4168 publishedDate = parser.parse(pubdate)
4269
@@ -49,6 +76,7 @@ def response(resp):
4976 else:
5077 content = result['content']['$t']
5178
79+ # append result
5280 results.append({'url': url,
5381 'title': title,
5482 'content': content,
@@ -56,4 +84,5 @@ def response(resp):
5684 'publishedDate': publishedDate,
5785 'thumbnail': thumbnail})
5886
87+ # return results
5988 return results