logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

youtube_noapi.py (2929B)


  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 lxml import html
  11. from searx.engines.xpath import extract_text
  12. from searx.utils import list_get
  13. from searx.url_utils import quote_plus
  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. 'year': 'BQ'}
  27. embedded_url = '<iframe width="540" height="304" ' +\
  28. 'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\
  29. 'frameborder="0" allowfullscreen></iframe>'
  30. base_youtube_url = 'https://www.youtube.com/watch?v='
  31. # specific xpath variables
  32. results_xpath = "//ol/li/div[contains(@class, 'yt-lockup yt-lockup-tile yt-lockup-video vve-check')]"
  33. url_xpath = './/h3/a/@href'
  34. title_xpath = './/div[@class="yt-lockup-content"]/h3/a'
  35. content_xpath = './/div[@class="yt-lockup-content"]/div[@class="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2"]'
  36. # returns extract_text on the first result selected by the xpath or None
  37. def extract_text_from_dom(result, xpath):
  38. r = result.xpath(xpath)
  39. if len(r) > 0:
  40. return extract_text(r[0])
  41. return None
  42. # do search-request
  43. def request(query, params):
  44. params['url'] = search_url.format(query=quote_plus(query),
  45. page=params['pageno'])
  46. if params['time_range'] in time_range_dict:
  47. params['url'] += time_range_url.format(time_range=time_range_dict[params['time_range']])
  48. return params
  49. # get response from search-request
  50. def response(resp):
  51. results = []
  52. dom = html.fromstring(resp.text)
  53. # parse results
  54. for result in dom.xpath(results_xpath):
  55. videoid = list_get(result.xpath('@data-context-item-id'), 0)
  56. if videoid is not None:
  57. url = base_youtube_url + videoid
  58. thumbnail = 'https://i.ytimg.com/vi/' + videoid + '/hqdefault.jpg'
  59. title = extract_text_from_dom(result, title_xpath) or videoid
  60. content = extract_text_from_dom(result, content_xpath)
  61. embedded = embedded_url.format(videoid=videoid)
  62. # append result
  63. results.append({'url': url,
  64. 'title': title,
  65. 'content': content,
  66. 'template': 'videos.html',
  67. 'embedded': embedded,
  68. 'thumbnail': thumbnail})
  69. # return results
  70. return results