google_videos.py 3.6 KB

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