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