google_videos.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. language_support = True
  50. time_range_support = True
  51. safesearch = True
  52. def request(query, params):
  53. """Google-Video search request"""
  54. google_info = get_google_info(params, traits)
  55. start = (params['pageno'] - 1) * 10
  56. query_url = (
  57. 'https://'
  58. + google_info['subdomain']
  59. + '/search'
  60. + "?"
  61. + urlencode(
  62. {
  63. 'q': query,
  64. 'tbm': "vid",
  65. 'start': 10 * params['pageno'],
  66. **google_info['params'],
  67. 'asearch': 'arc',
  68. 'async': ui_async(start),
  69. }
  70. )
  71. )
  72. if params['time_range'] in time_range_dict:
  73. query_url += '&' + urlencode({'tbs': 'qdr:' + time_range_dict[params['time_range']]})
  74. if 'safesearch' in params:
  75. query_url += '&' + urlencode({'safe': filter_mapping[params['safesearch']]})
  76. params['url'] = query_url
  77. params['cookies'] = google_info['cookies']
  78. params['headers'].update(google_info['headers'])
  79. return params
  80. def response(resp):
  81. """Get response from google's search request"""
  82. results = []
  83. detect_google_sorry(resp)
  84. data_image_map = parse_data_images(resp.text)
  85. # convert the text to dom
  86. dom = html.fromstring(resp.text)
  87. # parse results
  88. for result in eval_xpath_list(dom, '//div[contains(@class, "g ")]'):
  89. thumbnail = eval_xpath_getindex(result, './/img/@src', 0, None)
  90. if thumbnail:
  91. if thumbnail.startswith('data:image'):
  92. img_id = eval_xpath_getindex(result, './/img/@id', 0, None)
  93. if img_id:
  94. thumbnail = data_image_map.get(img_id)
  95. else:
  96. thumbnail = None
  97. title = extract_text(eval_xpath_getindex(result, './/a/h3[1]', 0))
  98. url = eval_xpath_getindex(result, './/a/h3[1]/../@href', 0)
  99. c_node = eval_xpath_getindex(result, './/div[contains(@class, "ITZIwc")]', 0)
  100. content = extract_text(c_node)
  101. pub_info = extract_text(eval_xpath(result, './/div[contains(@class, "gqF9jc")]'))
  102. results.append(
  103. {
  104. 'url': url,
  105. 'title': title,
  106. 'content': content,
  107. 'author': pub_info,
  108. 'thumbnail': thumbnail,
  109. 'iframe_src': get_embeded_stream_url(url),
  110. 'template': 'videos.html',
  111. }
  112. )
  113. # parse suggestion
  114. for suggestion in eval_xpath_list(dom, suggestion_xpath):
  115. # append suggestion
  116. results.append({'suggestion': extract_text(suggestion)})
  117. return results