youtube_noapi.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Youtube (Videos)
  2. #
  3. # @website https://www.youtube.com/
  4. # @provide-api yes (https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list)
  5. #
  6. # @using-api no
  7. # @results HTML
  8. # @stable no
  9. # @parse url, title, content, publishedDate, thumbnail, embedded
  10. from urllib import quote_plus
  11. from lxml import html
  12. from searx.engines.xpath import extract_text
  13. from searx.utils import list_get
  14. # engine dependent config
  15. categories = ['videos', 'music']
  16. paging = True
  17. language_support = False
  18. time_range_support = True
  19. # search-url
  20. base_url = 'https://www.youtube.com/results'
  21. search_url = base_url + '?search_query={query}&page={page}'
  22. time_range_url = '&sp=EgII{time_range}%253D%253D'
  23. time_range_dict = {'day': 'Ag',
  24. 'week': 'Aw',
  25. 'month': 'BA'}
  26. embedded_url = '<iframe width="540" height="304" ' +\
  27. 'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\
  28. 'frameborder="0" allowfullscreen></iframe>'
  29. base_youtube_url = 'https://www.youtube.com/watch?v='
  30. # specific xpath variables
  31. results_xpath = "//ol/li/div[contains(@class, 'yt-lockup yt-lockup-tile yt-lockup-video vve-check')]"
  32. url_xpath = './/h3/a/@href'
  33. title_xpath = './/div[@class="yt-lockup-content"]/h3/a'
  34. content_xpath = './/div[@class="yt-lockup-content"]/div[@class="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2"]'
  35. # returns extract_text on the first result selected by the xpath or None
  36. def extract_text_from_dom(result, xpath):
  37. r = result.xpath(xpath)
  38. if len(r) > 0:
  39. return extract_text(r[0])
  40. return None
  41. # do search-request
  42. def request(query, params):
  43. params['url'] = search_url.format(query=quote_plus(query),
  44. page=params['pageno'])
  45. if params['time_range'] in time_range_dict:
  46. params['url'] += time_range_url.format(time_range=time_range_dict[params['time_range']])
  47. return params
  48. # get response from search-request
  49. def response(resp):
  50. results = []
  51. dom = html.fromstring(resp.text)
  52. # parse results
  53. for result in dom.xpath(results_xpath):
  54. videoid = list_get(result.xpath('@data-context-item-id'), 0)
  55. if videoid is not None:
  56. url = base_youtube_url + videoid
  57. thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
  58. title = extract_text_from_dom(result, title_xpath) or videoid
  59. content = extract_text_from_dom(result, content_xpath)
  60. embedded = embedded_url.format(videoid=videoid)
  61. # append result
  62. results.append({'url': url,
  63. 'title': title,
  64. 'content': content,
  65. 'template': 'videos.html',
  66. 'embedded': embedded,
  67. 'thumbnail': thumbnail})
  68. # return results
  69. return results