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