google_videos.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 __future__ import annotations
  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. ui_async,
  28. parse_data_images,
  29. )
  30. from searx.enginelib.traits import EngineTraits
  31. from searx.utils import get_embeded_stream_url
  32. if TYPE_CHECKING:
  33. import logging
  34. logger: logging.Logger
  35. traits: EngineTraits
  36. # about
  37. about = {
  38. "website": 'https://www.google.com',
  39. "wikidata_id": 'Q219885',
  40. "official_api_documentation": 'https://developers.google.com/custom-search',
  41. "use_official_api": False,
  42. "require_api_key": False,
  43. "results": 'HTML',
  44. }
  45. # engine dependent config
  46. categories = ['videos', 'web']
  47. paging = True
  48. max_page = 50
  49. """`Google: max 50 pages`
  50. .. _Google: max 50 pages: https://github.com/searxng/searxng/issues/2982
  51. """
  52. language_support = True
  53. time_range_support = True
  54. safesearch = True
  55. def request(query, params):
  56. """Google-Video search request"""
  57. google_info = get_google_info(params, traits)
  58. start = (params['pageno'] - 1) * 10
  59. query_url = (
  60. 'https://'
  61. + google_info['subdomain']
  62. + '/search'
  63. + "?"
  64. + urlencode(
  65. {
  66. 'q': query,
  67. 'tbm': "vid",
  68. 'start': 10 * params['pageno'],
  69. **google_info['params'],
  70. 'asearch': 'arc',
  71. 'async': ui_async(start),
  72. }
  73. )
  74. )
  75. if params['time_range'] in time_range_dict:
  76. query_url += '&' + urlencode({'tbs': 'qdr:' + time_range_dict[params['time_range']]})
  77. if 'safesearch' in params:
  78. query_url += '&' + urlencode({'safe': filter_mapping[params['safesearch']]})
  79. params['url'] = query_url
  80. params['cookies'] = google_info['cookies']
  81. params['headers'].update(google_info['headers'])
  82. return params
  83. def response(resp):
  84. """Get response from google's search request"""
  85. results = []
  86. detect_google_sorry(resp)
  87. data_image_map = parse_data_images(resp.text)
  88. # convert the text to dom
  89. dom = html.fromstring(resp.text)
  90. # parse results
  91. for result in eval_xpath_list(dom, '//div[contains(@class, "g ")]'):
  92. thumbnail = eval_xpath_getindex(result, './/img/@src', 0, None)
  93. if thumbnail:
  94. if thumbnail.startswith('data:image'):
  95. img_id = eval_xpath_getindex(result, './/img/@id', 0, None)
  96. if img_id:
  97. thumbnail = data_image_map.get(img_id)
  98. else:
  99. thumbnail = None
  100. title = extract_text(eval_xpath_getindex(result, './/a/h3[1]', 0))
  101. url = eval_xpath_getindex(result, './/a/h3[1]/../@href', 0)
  102. c_node = eval_xpath_getindex(result, './/div[contains(@class, "ITZIwc")]', 0)
  103. content = extract_text(c_node)
  104. pub_info = extract_text(eval_xpath(result, './/div[contains(@class, "gqF9jc")]'))
  105. results.append(
  106. {
  107. 'url': url,
  108. 'title': title,
  109. 'content': content,
  110. 'author': pub_info,
  111. 'thumbnail': thumbnail,
  112. 'iframe_src': get_embeded_stream_url(url),
  113. 'template': 'videos.html',
  114. }
  115. )
  116. # parse suggestion
  117. for suggestion in eval_xpath_list(dom, suggestion_xpath):
  118. # append suggestion
  119. results.append({'suggestion': extract_text(suggestion)})
  120. return results