.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
bing_videos.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=invalid-name
3"""Bing-Videos: description see :py:obj:`searx.engines.bing`.
4"""
5
6from typing import TYPE_CHECKING
7import json
8from urllib.parse import urlencode
9
10from lxml import html
11
12from searx.enginelib.traits import EngineTraits
13from searx.engines.bing import set_bing_cookies
14from searx.engines.bing import fetch_traits # pylint: disable=unused-import
15from searx.engines.bing_images import time_map
16
17if TYPE_CHECKING:
18 import logging
19
20 logger: logging.Logger
21
22traits: EngineTraits
23
24
25about = {
26 "website": 'https://www.bing.com/videos',
27 "wikidata_id": 'Q4914152',
28 "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-video-search-api',
29 "use_official_api": False,
30 "require_api_key": False,
31 "results": 'HTML',
32}
33
34# engine dependent config
35categories = ['videos', 'web']
36paging = True
37safesearch = True
38time_range_support = True
39
40base_url = 'https://www.bing.com/videos/asyncv2'
41"""Bing (Videos) async search URL."""
42
43
44def request(query, params):
45 """Assemble a Bing-Video request."""
46
47 engine_region = traits.get_region(params['searxng_locale'], traits.all_locale) # type: ignore
48 engine_language = traits.get_language(params['searxng_locale'], 'en') # type: ignore
49 set_bing_cookies(params, engine_language, engine_region)
50
51 # build URL query
52 #
53 # example: https://www.bing.com/videos/asyncv2?q=foo&async=content&first=1&count=35
54
55 query_params = {
56 'q': query,
57 'async': 'content',
58 # to simplify the page count lets use the default of 35 images per page
59 'first': (int(params.get('pageno', 1)) - 1) * 35 + 1,
60 'count': 35,
61 }
62
63 # time range
64 #
65 # example: one week (10080 minutes) '&qft= filterui:videoage-lt10080' '&form=VRFLTR'
66
67 if params['time_range']:
68 query_params['form'] = 'VRFLTR'
69 query_params['qft'] = ' filterui:videoage-lt%s' % time_map[params['time_range']]
70
71 params['url'] = base_url + '?' + urlencode(query_params)
72
73 return params
74
75
76def response(resp):
77 """Get response from Bing-Video"""
78 results = []
79
80 dom = html.fromstring(resp.text)
81
82 for result in dom.xpath('//div[@class="dg_u"]//div[contains(@id, "mc_vtvc_video")]'):
83 metadata = json.loads(result.xpath('.//div[@class="vrhdata"]/@vrhm')[0])
84 info = ' - '.join(result.xpath('.//div[@class="mc_vtvc_meta_block"]//span/text()')).strip()
85 content = '{0} - {1}'.format(metadata['du'], info)
86 thumbnail = result.xpath('.//div[contains(@class, "mc_vtvc_th")]//img/@src')[0]
87
88 results.append(
89 {
90 'url': metadata['murl'],
91 'thumbnail': thumbnail,
92 'title': metadata.get('vt', ''),
93 'content': content,
94 'template': 'videos.html',
95 }
96 )
97
98 return results