google_videos.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """This is the implementation of the Google Videos engine.
  4. .. admonition:: Content-Security-Policy (CSP)
  5. This engine needs to allow images from the `data URLs`_ (prefixed with the
  6. ``data:`` scheme)::
  7. Header set Content-Security-Policy "img-src 'self' data: ;"
  8. .. _data URLs:
  9. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
  10. """
  11. from typing import TYPE_CHECKING
  12. from urllib.parse import urlencode
  13. from lxml import html
  14. from searx.utils import (
  15. eval_xpath,
  16. eval_xpath_list,
  17. eval_xpath_getindex,
  18. extract_text,
  19. )
  20. from searx.engines.google import fetch_traits # pylint: disable=unused-import
  21. from searx.engines.google import (
  22. get_google_info,
  23. time_range_dict,
  24. filter_mapping,
  25. suggestion_xpath,
  26. detect_google_sorry,
  27. )
  28. from searx.enginelib.traits import EngineTraits
  29. if TYPE_CHECKING:
  30. import logging
  31. logger: logging.Logger
  32. traits: EngineTraits
  33. # about
  34. about = {
  35. "website": 'https://www.google.com',
  36. "wikidata_id": 'Q219885',
  37. "official_api_documentation": 'https://developers.google.com/custom-search',
  38. "use_official_api": False,
  39. "require_api_key": False,
  40. "results": 'HTML',
  41. }
  42. # engine dependent config
  43. categories = ['videos', 'web']
  44. paging = True
  45. language_support = True
  46. time_range_support = True
  47. safesearch = True
  48. def request(query, params):
  49. """Google-Video search request"""
  50. google_info = get_google_info(params, traits)
  51. query_url = (
  52. 'https://'
  53. + google_info['subdomain']
  54. + '/search'
  55. + "?"
  56. + urlencode(
  57. {
  58. 'q': query,
  59. 'tbm': "vid",
  60. 'start': 10 * params['pageno'],
  61. **google_info['params'],
  62. 'asearch': 'arc',
  63. 'async': 'use_ac:true,_fmt:html',
  64. }
  65. )
  66. )
  67. if params['time_range'] in time_range_dict:
  68. query_url += '&' + urlencode({'tbs': 'qdr:' + time_range_dict[params['time_range']]})
  69. if params['safesearch']:
  70. query_url += '&' + urlencode({'safe': filter_mapping[params['safesearch']]})
  71. params['url'] = query_url
  72. params['cookies'] = google_info['cookies']
  73. params['headers'].update(google_info['headers'])
  74. return params
  75. def response(resp):
  76. """Get response from google's search request"""
  77. results = []
  78. detect_google_sorry(resp)
  79. # convert the text to dom
  80. dom = html.fromstring(resp.text)
  81. # parse results
  82. for result in eval_xpath_list(dom, '//div[contains(@class, "g ")]'):
  83. img_src = eval_xpath_getindex(result, './/img/@src', 0, None)
  84. if img_src is None:
  85. continue
  86. title = extract_text(eval_xpath_getindex(result, './/a/h3[1]', 0))
  87. url = eval_xpath_getindex(result, './/a/h3[1]/../@href', 0)
  88. c_node = eval_xpath_getindex(result, './/div[@class="ITZIwc"]', 0)
  89. content = extract_text(c_node)
  90. pub_info = extract_text(eval_xpath(result, './/div[@class="gqF9jc"]'))
  91. results.append(
  92. {
  93. 'url': url,
  94. 'title': title,
  95. 'content': content,
  96. 'author': pub_info,
  97. 'thumbnail': img_src,
  98. 'template': 'videos.html',
  99. }
  100. )
  101. # parse suggestion
  102. for suggestion in eval_xpath_list(dom, suggestion_xpath):
  103. # append suggestion
  104. results.append({'suggestion': extract_text(suggestion)})
  105. return results