Files
searxng/searx/engines/google_videos.py
T

140 lines
3.6 KiB
Python
Raw Normal View History

2021-01-13 11:31:25 +01:00
# SPDX-License-Identifier: AGPL-3.0-or-later
"""This is the implementation of the Google Videos engine.
2021-01-22 17:16:46 +01: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
``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-21 22:17:28 -05:00
"""
from typing import TYPE_CHECKING
2021-01-22 17:16:46 +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,
eval_xpath_getindex,
2021-01-22 17:16:46 +01:00
extract_text,
)
from searx.engines.google import fetch_traits # pylint: disable=unused-import
2021-01-22 17:16:46 +01:00
from searx.engines.google import (
get_google_info,
2021-01-22 17:16:46 +01:00
time_range_dict,
filter_mapping,
suggestion_xpath,
detect_google_sorry,
2021-01-22 17:16:46 +01:00
)
from searx.enginelib.traits import EngineTraits
2024-09-14 16:28:35 -06:00
from searx.utils import get_embeded_stream_url
2021-01-22 17:16:46 +01:00
if TYPE_CHECKING:
import logging
logger: logging.Logger
traits: EngineTraits
2017-07-21 22:17:28 -05:00
2021-01-13 11:31:25 +01:00
# about
about = {
"website": 'https://www.google.com',
"wikidata_id": 'Q219885',
"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-21 22:17:28 -05:00
# engine dependent config
2021-01-22 17:16:46 +01:00
2021-12-22 16:58:52 +01:00
categories = ['videos', 'web']
paging = True
2023-11-13 19:12:50 +01:00
max_page = 50
2021-01-22 17:16:46 +01:00
language_support = True
2017-07-21 22:17:28 -05:00
time_range_support = True
2021-01-22 17:16:46 +01:00
safesearch = True
2017-07-21 22:17:28 -05:00
def request(query, params):
2021-01-22 17:16:46 +01:00
"""Google-Video search request"""
google_info = get_google_info(params, traits)
2021-01-22 17:16:46 +01:00
query_url = (
'https://'
+ google_info['subdomain']
+ '/search'
+ "?"
+ urlencode(
{
'q': query,
'tbm': "vid",
'start': 10 * params['pageno'],
**google_info['params'],
'asearch': 'arc',
'async': 'use_ac:true,_fmt:html',
}
)
)
2017-07-21 22:17:28 -05:00
if params['time_range'] in time_range_dict:
2021-01-22 17:16:46 +01:00
query_url += '&' + urlencode({'tbs': 'qdr:' + time_range_dict[params['time_range']]})
if 'safesearch' in params:
2021-01-22 17:16:46 +01:00
query_url += '&' + urlencode({'safe': filter_mapping[params['safesearch']]})
params['url'] = query_url
2017-07-21 22:17:28 -05:00
params['cookies'] = google_info['cookies']
params['headers'].update(google_info['headers'])
2017-07-21 22:17:28 -05:00
return params
def response(resp):
2021-01-22 17:16:46 +01:00
"""Get response from google's search request"""
2017-07-21 22:17:28 -05:00
results = []
detect_google_sorry(resp)
2021-01-22 17:16:46 +01:00
# convert the text to dom
2017-07-21 22:17:28 -05:00
dom = html.fromstring(resp.text)
# parse results
for result in eval_xpath_list(dom, '//div[contains(@class, "g ")]'):
2017-07-21 22:17:28 -05:00
thumbnail = eval_xpath_getindex(result, './/img/@src', 0, None)
if thumbnail is None:
2021-01-22 17:16:46 +01:00
continue
2017-07-21 22:17:28 -05:00
title = extract_text(eval_xpath_getindex(result, './/a/h3[1]', 0))
url = eval_xpath_getindex(result, './/a/h3[1]/../@href', 0)
c_node = eval_xpath_getindex(result, './/div[contains(@class, "ITZIwc")]', 0)
content = extract_text(c_node)
pub_info = extract_text(eval_xpath(result, './/div[contains(@class, "gqF9jc")]'))
2021-01-22 17:16:46 +01:00
results.append(
{
'url': url,
'title': title,
'content': content,
'author': pub_info,
'thumbnail': thumbnail,
2024-09-14 16:28:35 -06:00
'iframe_src': get_embeded_stream_url(url),
'template': 'videos.html',
}
)
2021-01-22 17:16:46 +01:00
# parse suggestion
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-21 22:17:28 -05:00
return results