.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
bing_images.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Bing-Images: description see :py:obj:`searx.engines.bing`.
3"""
4# pylint: disable=invalid-name
5import json
6from urllib.parse import urlencode
7
8from lxml import html
9
10from searx.engines.bing import set_bing_cookies
11from searx.engines.bing import fetch_traits # pylint: disable=unused-import
12
13# about
14about = {
15 "website": 'https://www.bing.com/images',
16 "wikidata_id": 'Q182496',
17 "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-image-search-api',
18 "use_official_api": False,
19 "require_api_key": False,
20 "results": 'HTML',
21}
22
23# engine dependent config
24categories = ['images', 'web']
25paging = True
26safesearch = True
27time_range_support = True
28
29base_url = 'https://www.bing.com/images/async'
30"""Bing (Images) search URL"""
31
32time_map = {
33 'day': 60 * 24,
34 'week': 60 * 24 * 7,
35 'month': 60 * 24 * 31,
36 'year': 60 * 24 * 365,
37}
38
39
40def request(query, params):
41 """Assemble a Bing-Image request."""
42
43 engine_region = traits.get_region(params['searxng_locale'], traits.all_locale) # type: ignore
44 engine_language = traits.get_language(params['searxng_locale'], 'en') # type: ignore
45 set_bing_cookies(params, engine_language, engine_region)
46
47 # build URL query
48 # - example: https://www.bing.com/images/async?q=foo&async=content&first=1&count=35
49 query_params = {
50 'q': query,
51 'async': '1',
52 # to simplify the page count lets use the default of 35 images per page
53 'first': (int(params.get('pageno', 1)) - 1) * 35 + 1,
54 'count': 35,
55 }
56
57 # time range
58 # - example: one year (525600 minutes) 'qft=+filterui:age-lt525600'
59
60 if params['time_range']:
61 query_params['qft'] = 'filterui:age-lt%s' % time_map[params['time_range']]
62
63 params['url'] = base_url + '?' + urlencode(query_params)
64
65 return params
66
67
68def response(resp):
69 """Get response from Bing-Images"""
70
71 results = []
72 dom = html.fromstring(resp.text)
73
74 for result in dom.xpath('//ul[contains(@class, "dgControl_list")]/li'):
75
76 metadata = result.xpath('.//a[@class="iusc"]/@m')
77 if not metadata:
78 continue
79
80 metadata = json.loads(result.xpath('.//a[@class="iusc"]/@m')[0])
81 title = ' '.join(result.xpath('.//div[@class="infnmpt"]//a/text()')).strip()
82 img_format = ' '.join(result.xpath('.//div[@class="imgpt"]/div/span/text()')).strip().split(" ยท ")
83 source = ' '.join(result.xpath('.//div[@class="imgpt"]//div[@class="lnkw"]//a/text()')).strip()
84 results.append(
85 {
86 'template': 'images.html',
87 'url': metadata['purl'],
88 'thumbnail_src': metadata['turl'],
89 'img_src': metadata['murl'],
90 'content': metadata.get('desc'),
91 'title': title,
92 'source': source,
93 'resolution': img_format[0],
94 'img_format': img_format[1] if len(img_format) >= 2 else None,
95 }
96 )
97 return results