| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-04-26 20:18:20 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  | """This is the implementation of the Google Videos engine.
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 18:15:40 +02:00
										 |  |  | .. admonition:: Content-Security-Policy (CSP) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This engine needs to allow images from the `data URLs`_ (prefixed with the | 
					
						
							| 
									
										
										
										
											2021-06-21 18:15:40 +02:00
										 |  |  |    ``data:`` scheme):: | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |      Header set Content-Security-Policy "img-src 'self' data: ;" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _data URLs: | 
					
						
							|  |  |  |    https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  | from typing import TYPE_CHECKING | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | from lxml import html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from searx.utils import ( | 
					
						
							|  |  |  |     eval_xpath, | 
					
						
							|  |  |  |     eval_xpath_list, | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     eval_xpath_getindex, | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |     extract_text, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  | from searx.engines.google import fetch_traits  # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | from searx.engines.google import ( | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |     get_google_info, | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |     time_range_dict, | 
					
						
							|  |  |  |     filter_mapping, | 
					
						
							|  |  |  |     suggestion_xpath, | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     detect_google_sorry, | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  | from searx.enginelib.traits import EngineTraits | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  | if TYPE_CHECKING: | 
					
						
							|  |  |  |     import logging | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |     logger: logging.Logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | traits: EngineTraits | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.google.com', | 
					
						
							|  |  |  |     "wikidata_id": 'Q219885', | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     "official_api_documentation": 'https://developers.google.com/custom-search', | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  |     "use_official_api": False, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'HTML', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-22 16:58:52 +01:00
										 |  |  | categories = ['videos', 'web'] | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | language_support = True | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | time_range_support = True | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | safesearch = True | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | def request(query, params): | 
					
						
							|  |  |  |     """Google-Video search request""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |     google_info = get_google_info(params, traits) | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     query_url = ( | 
					
						
							|  |  |  |         'https://' | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |         + google_info['subdomain'] | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         + '/search' | 
					
						
							|  |  |  |         + "?" | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |         + urlencode( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'q': query, | 
					
						
							|  |  |  |                 'tbm': "vid", | 
					
						
							|  |  |  |                 'start': 10 * params['pageno'], | 
					
						
							|  |  |  |                 **google_info['params'], | 
					
						
							|  |  |  |                 'asearch': 'arc', | 
					
						
							|  |  |  |                 'async': 'use_ac:true,_fmt:html', | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if params['time_range'] in time_range_dict: | 
					
						
							|  |  |  |         query_url += '&' + urlencode({'tbs': 'qdr:' + time_range_dict[params['time_range']]}) | 
					
						
							|  |  |  |     if params['safesearch']: | 
					
						
							|  |  |  |         query_url += '&' + urlencode({'safe': filter_mapping[params['safesearch']]}) | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     params['url'] = query_url | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |     params['cookies'] = google_info['cookies'] | 
					
						
							|  |  |  |     params['headers'].update(google_info['headers']) | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |     """Get response from google's search request""" | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     detect_google_sorry(resp) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # convert the text to dom | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # parse results | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |     for result in eval_xpath_list(dom, '//div[contains(@class, "g ")]'): | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |         img_src = eval_xpath_getindex(result, './/img/@src', 0, None) | 
					
						
							|  |  |  |         if img_src is None: | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:57:22 +01:00
										 |  |  |         title = extract_text(eval_xpath_getindex(result, './/a/h3[1]', 0)) | 
					
						
							|  |  |  |         url = eval_xpath_getindex(result, './/a/h3[1]/../@href', 0) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 08:10:19 +02:00
										 |  |  |         c_node = eval_xpath_getindex(result, './/div[@class="ITZIwc"]', 0) | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |         content = extract_text(c_node) | 
					
						
							| 
									
										
										
										
											2023-08-22 08:10:19 +02:00
										 |  |  |         pub_info = extract_text(eval_xpath(result, './/div[@class="gqF9jc"]')) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'url': url, | 
					
						
							|  |  |  |                 'title': title, | 
					
						
							|  |  |  |                 'content': content, | 
					
						
							|  |  |  |                 'author': pub_info, | 
					
						
							|  |  |  |                 'thumbnail': img_src, | 
					
						
							|  |  |  |                 'template': 'videos.html', | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # parse suggestion | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     for suggestion in eval_xpath_list(dom, suggestion_xpath): | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |         # append suggestion | 
					
						
							|  |  |  |         results.append({'suggestion': extract_text(suggestion)}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  |     return results |