youtube.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, embedded
  10. from json import loads
  11. from urllib import urlencode
  12. from dateutil import parser
  13. # engine dependent config
  14. categories = ['videos', 'music']
  15. paging = True
  16. language_support = True
  17. # search-url
  18. base_url = 'https://gdata.youtube.com/feeds/api/videos'
  19. search_url = base_url + '?alt=json&{query}&start-index={index}&max-results=5'
  20. embedded_url = '<iframe width="540" height="304" ' +\
  21. 'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\
  22. 'frameborder="0" allowfullscreen></iframe>'
  23. # do search-request
  24. def request(query, params):
  25. index = (params['pageno'] - 1) * 5 + 1
  26. params['url'] = search_url.format(query=urlencode({'q': query}),
  27. index=index)
  28. # add language tag if specified
  29. if params['language'] != 'all':
  30. params['url'] += '&lr=' + params['language'].split('_')[0]
  31. return params
  32. # get response from search-request
  33. def response(resp):
  34. results = []
  35. search_results = loads(resp.text)
  36. # return empty array if there are no results
  37. if not 'feed' in search_results:
  38. return []
  39. feed = search_results['feed']
  40. # parse results
  41. for result in feed['entry']:
  42. url = [x['href'] for x in result['link'] if x['type'] == 'text/html']
  43. if not url:
  44. continue
  45. # remove tracking
  46. url = url[0].replace('feature=youtube_gdata', '')
  47. if url.endswith('&'):
  48. url = url[:-1]
  49. videoid = url[32:]
  50. title = result['title']['$t']
  51. content = ''
  52. thumbnail = ''
  53. pubdate = result['published']['$t']
  54. publishedDate = parser.parse(pubdate)
  55. if 'media$thumbnail' in result['media$group']:
  56. thumbnail = result['media$group']['media$thumbnail'][0]['url']
  57. content = result['content']['$t']
  58. embedded = embedded_url.format(videoid=videoid)
  59. # append result
  60. results.append({'url': url,
  61. 'title': title,
  62. 'content': content,
  63. 'template': 'videos.html',
  64. 'publishedDate': publishedDate,
  65. 'embedded': embedded,
  66. 'thumbnail': thumbnail})
  67. # return results
  68. return results