|  | @@ -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 | +
 | 
	
		
			
			| 1 | 11 |  from json import loads
 | 
	
		
			
			| 2 | 12 |  from urllib import urlencode
 | 
	
		
			
			| 3 | 13 |  from dateutil import parser
 | 
	
		
			
			| 4 | 14 |  
 | 
	
		
			
			|  | 15 | +# engine dependent config
 | 
	
		
			
			| 5 | 16 |  categories = ['videos']
 | 
	
		
			
			| 6 |  | -
 | 
	
		
			
			| 7 |  | -search_url = ('https://gdata.youtube.com/feeds/api/videos'
 | 
	
		
			
			| 8 |  | -              '?alt=json&{query}&start-index={index}&max-results=25')  # noqa
 | 
	
		
			
			| 9 |  | -
 | 
	
		
			
			| 10 | 17 |  paging = 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
 | 
	
		
			
			| 11 | 23 |  
 | 
	
		
			
			| 12 | 24 |  
 | 
	
		
			
			|  | 25 | +# do search-request
 | 
	
		
			
			| 13 | 26 |  def request(query, params):
 | 
	
		
			
			| 14 |  | -    index = (params['pageno'] - 1) * 25 + 1
 | 
	
		
			
			|  | 27 | +    index = (params['pageno'] - 1) * 5 + 1
 | 
	
		
			
			|  | 28 | +
 | 
	
		
			
			| 15 | 29 |      params['url'] = search_url.format(query=urlencode({'q': query}),
 | 
	
		
			
			| 16 | 30 |                                        index=index)
 | 
	
		
			
			|  | 31 | +
 | 
	
		
			
			|  | 32 | +    # add language tag if specified
 | 
	
		
			
			|  | 33 | +    if params['language'] != 'all':
 | 
	
		
			
			|  | 34 | +        params['url'] += '&lr=' + params['language'].split('_')[0]
 | 
	
		
			
			|  | 35 | +
 | 
	
		
			
			| 17 | 36 |      return params
 | 
	
		
			
			| 18 | 37 |  
 | 
	
		
			
			| 19 | 38 |  
 | 
	
		
			
			|  | 39 | +# get response from search-request
 | 
	
		
			
			| 20 | 40 |  def response(resp):
 | 
	
		
			
			| 21 | 41 |      results = []
 | 
	
		
			
			|  | 42 | +
 | 
	
		
			
			| 22 | 43 |      search_results = loads(resp.text)
 | 
	
		
			
			|  | 44 | +
 | 
	
		
			
			|  | 45 | +    # return empty array if there are no results
 | 
	
		
			
			| 23 | 46 |      if not 'feed' in search_results:
 | 
	
		
			
			| 24 |  | -        return results
 | 
	
		
			
			|  | 47 | +        return []
 | 
	
		
			
			|  | 48 | +
 | 
	
		
			
			| 25 | 49 |      feed = search_results['feed']
 | 
	
		
			
			| 26 | 50 |  
 | 
	
		
			
			|  | 51 | +    # parse results
 | 
	
		
			
			| 27 | 52 |      for result in feed['entry']:
 | 
	
		
			
			| 28 | 53 |          url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
 | 
	
		
			
			|  | 54 | +
 | 
	
		
			
			| 29 | 55 |          if not url:
 | 
	
		
			
			| 30 | 56 |              return
 | 
	
		
			
			|  | 57 | +
 | 
	
		
			
			| 31 | 58 |          # remove tracking
 | 
	
		
			
			| 32 | 59 |          url = url[0].replace('feature=youtube_gdata', '')
 | 
	
		
			
			| 33 | 60 |          if url.endswith('&'):
 | 
	
		
			
			| 34 | 61 |              url = url[:-1]
 | 
	
		
			
			|  | 62 | +
 | 
	
		
			
			| 35 | 63 |          title = result['title']['$t']
 | 
	
		
			
			| 36 | 64 |          content = ''
 | 
	
		
			
			| 37 | 65 |          thumbnail = ''
 | 
	
		
			
			| 38 | 66 |  
 | 
	
		
			
			| 39 |  | -#"2013-12-31T15:22:51.000Z"
 | 
	
		
			
			| 40 | 67 |          pubdate = result['published']['$t']
 | 
	
		
			
			| 41 | 68 |          publishedDate = parser.parse(pubdate)
 | 
	
		
			
			| 42 | 69 |  
 | 
	
	
		
			
			|  | @@ -49,6 +76,7 @@ def response(resp):
 | 
	
		
			
			| 49 | 76 |          else:
 | 
	
		
			
			| 50 | 77 |              content = result['content']['$t']
 | 
	
		
			
			| 51 | 78 |  
 | 
	
		
			
			|  | 79 | +        # append result
 | 
	
		
			
			| 52 | 80 |          results.append({'url': url,
 | 
	
		
			
			| 53 | 81 |                          'title': title,
 | 
	
		
			
			| 54 | 82 |                          'content': content,
 | 
	
	
		
			
			|  | @@ -56,4 +84,5 @@ def response(resp):
 | 
	
		
			
			| 56 | 84 |                          'publishedDate': publishedDate,
 | 
	
		
			
			| 57 | 85 |                          'thumbnail': thumbnail})
 | 
	
		
			
			| 58 | 86 |  
 | 
	
		
			
			|  | 87 | +    # return results
 | 
	
		
			
			| 59 | 88 |      return results
 |