| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-06-21 18:15:40 +02: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
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 13:26:59 +02:00
										 |  |  | # pylint: disable=invalid-name | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 15:48:22 +01:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from searx.engines.google import ( | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     get_lang_info, | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |     time_range_dict, | 
					
						
							|  |  |  |     filter_mapping, | 
					
						
							|  |  |  |     g_section_with_header, | 
					
						
							|  |  |  |     title_xpath, | 
					
						
							|  |  |  |     suggestion_xpath, | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     detect_google_sorry, | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # pylint: disable=unused-import | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | from searx.engines.google import supported_languages_url, _fetch_supported_languages | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | # pylint: enable=unused-import | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | categories = ['videos'] | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | paging = False | 
					
						
							|  |  |  | language_support = True | 
					
						
							|  |  |  | use_locale_domain = 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
										 |  |  | RE_CACHE = {} | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | def _re(regexpr): | 
					
						
							|  |  |  |     """returns compiled regular expression""" | 
					
						
							|  |  |  |     RE_CACHE[regexpr] = RE_CACHE.get(regexpr, re.compile(regexpr)) | 
					
						
							|  |  |  |     return RE_CACHE[regexpr] | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def scrap_out_thumbs_src(dom): | 
					
						
							|  |  |  |     ret_val = {} | 
					
						
							|  |  |  |     thumb_name = 'dimg_' | 
					
						
							|  |  |  |     for script in eval_xpath_list(dom, '//script[contains(., "google.ldi={")]'): | 
					
						
							|  |  |  |         _script = script.text | 
					
						
							|  |  |  |         # "dimg_35":"https://i.ytimg.c....", | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         _dimurl = _re("s='([^']*)").findall(_script) | 
					
						
							|  |  |  |         for k, v in _re('(' + thumb_name + '[0-9]*)":"(http[^"]*)').findall(_script): | 
					
						
							|  |  |  |             v = v.replace(r'\u003d', '=') | 
					
						
							|  |  |  |             v = v.replace(r'\u0026', '&') | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |             ret_val[k] = v | 
					
						
							|  |  |  |     logger.debug("found %s imgdata for: %s", thumb_name, ret_val.keys()) | 
					
						
							|  |  |  |     return ret_val | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | def scrap_out_thumbs(dom): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     """Scrap out thumbnail data from <script> tags.""" | 
					
						
							| 
									
										
										
										
											2021-08-31 10:40:29 +02:00
										 |  |  |     ret_val = {} | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |     thumb_name = 'dimg_' | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |     for script in eval_xpath_list(dom, '//script[contains(., "_setImagesSrc")]'): | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |         _script = script.text | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # var s='data:image/jpeg;base64, ...' | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         _imgdata = _re("s='([^']*)").findall(_script) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |         if not _imgdata: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |         # var ii=['dimg_17'] | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |         for _vidthumb in _re(r"(%s\d+)" % thumb_name).findall(_script): | 
					
						
							|  |  |  |             # At least the equal sign in the URL needs to be decoded | 
					
						
							|  |  |  |             ret_val[_vidthumb] = _imgdata[0].replace(r"\x3d", "=") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     logger.debug("found %s imgdata for: %s", thumb_name, ret_val.keys()) | 
					
						
							|  |  |  |     return ret_val | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | def request(query, params): | 
					
						
							|  |  |  |     """Google-Video search request""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     lang_info = get_lang_info(params, supported_languages, language_aliases, False) | 
					
						
							|  |  |  |     logger.debug("HTTP header Accept-Language --> %s", lang_info['headers']['Accept-Language']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     query_url = ( | 
					
						
							|  |  |  |         'https://' | 
					
						
							|  |  |  |         + lang_info['subdomain'] | 
					
						
							|  |  |  |         + '/search' | 
					
						
							|  |  |  |         + "?" | 
					
						
							|  |  |  |         + urlencode( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'q': query, | 
					
						
							|  |  |  |                 'tbm': "vid", | 
					
						
							|  |  |  |                 **lang_info['params'], | 
					
						
							|  |  |  |                 'ie': "utf8", | 
					
						
							|  |  |  |                 'oe': "utf8", | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-06 08:18:07 +02:00
										 |  |  |     params['headers'].update(lang_info['headers']) | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     params['headers']['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |     vidthumb_imgdata = scrap_out_thumbs(dom) | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |     thumbs_src = scrap_out_thumbs_src(dom) | 
					
						
							|  |  |  |     logger.debug(str(thumbs_src)) | 
					
						
							| 
									
										
										
										
											2017-07-22 05:17:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 17:11:20 +01:00
										 |  |  |         # ignore google *sections* | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |         if extract_text(eval_xpath(result, g_section_with_header)): | 
					
						
							|  |  |  |             logger.debug("ingoring <g-section-with-header>") | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 17:11:20 +01:00
										 |  |  |         # ingnore articles without an image id / e.g. news articles | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |         img_id = eval_xpath_getindex(result, './/g-img/img/@id', 0, default=None) | 
					
						
							| 
									
										
										
										
											2021-01-26 11:49:27 +01:00
										 |  |  |         if img_id is None: | 
					
						
							| 
									
										
										
										
											2021-11-26 17:11:20 +01:00
										 |  |  |             logger.error("no img_id found in item %s (news article?)", len(results) + 1) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  |         img_src = vidthumb_imgdata.get(img_id, None) | 
					
						
							|  |  |  |         if not img_src: | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |             img_src = thumbs_src.get(img_id, "") | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-26 17:11:20 +01:00
										 |  |  |         title = extract_text(eval_xpath_getindex(result, title_xpath, 0)) | 
					
						
							|  |  |  |         url = eval_xpath_getindex(result, './/div[@class="dXiKIc"]//a/@href', 0) | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         length = extract_text(eval_xpath(result, './/div[contains(@class, "P7xzyf")]/span/span')) | 
					
						
							| 
									
										
										
										
											2021-11-26 01:14:17 +01:00
										 |  |  |         c_node = eval_xpath_getindex(result, './/div[@class="Uroaid"]', 0) | 
					
						
							|  |  |  |         content = extract_text(c_node) | 
					
						
							|  |  |  |         pub_info = extract_text(eval_xpath(result, './/div[@class="Zg1NU"]')) | 
					
						
							| 
									
										
										
										
											2021-01-22 17:16:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'url': url, | 
					
						
							|  |  |  |                 'title': title, | 
					
						
							|  |  |  |                 'content': content, | 
					
						
							|  |  |  |                 'length': length, | 
					
						
							|  |  |  |                 '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 |