|
@@ -18,7 +18,6 @@
|
18
|
18
|
from lxml import html
|
19
|
19
|
from json import loads
|
20
|
20
|
import re
|
21
|
|
-from searx.engines.bing import _fetch_supported_languages, supported_languages_url
|
22
|
21
|
from searx.url_utils import urlencode
|
23
|
22
|
|
24
|
23
|
# engine dependent config
|
|
@@ -26,6 +25,8 @@ categories = ['images']
|
26
|
25
|
paging = True
|
27
|
26
|
safesearch = True
|
28
|
27
|
time_range_support = True
|
|
28
|
+language_support = True
|
|
29
|
+supported_languages_url = 'https://www.bing.com/account/general'
|
29
|
30
|
|
30
|
31
|
# search-url
|
31
|
32
|
base_url = 'https://www.bing.com/'
|
|
@@ -45,23 +46,41 @@ safesearch_types = {2: 'STRICT',
|
45
|
46
|
_quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
|
46
|
47
|
|
47
|
48
|
|
|
49
|
+# get supported region code
|
|
50
|
+def get_region_code(lang, lang_list=None):
|
|
51
|
+ region = None
|
|
52
|
+ if lang in (lang_list or supported_languages):
|
|
53
|
+ region = lang
|
|
54
|
+ elif lang.startswith('no'):
|
|
55
|
+ region = 'nb-NO'
|
|
56
|
+ else:
|
|
57
|
+ # try to get a supported country code with language
|
|
58
|
+ lang = lang.split('-')[0]
|
|
59
|
+ for lc in (lang_list or supported_languages):
|
|
60
|
+ if lang == lc.split('-')[0]:
|
|
61
|
+ region = lc
|
|
62
|
+ break
|
|
63
|
+ if region:
|
|
64
|
+ return region.lower()
|
|
65
|
+ else:
|
|
66
|
+ return 'en-us'
|
|
67
|
+
|
|
68
|
+
|
48
|
69
|
# do search-request
|
49
|
70
|
def request(query, params):
|
50
|
71
|
offset = (params['pageno'] - 1) * 10 + 1
|
51
|
72
|
|
52
|
|
- # required for cookie
|
53
|
|
- if params['language'] == 'all':
|
54
|
|
- language = 'en-US'
|
55
|
|
- else:
|
56
|
|
- language = params['language']
|
57
|
|
-
|
58
|
73
|
search_path = search_string.format(
|
59
|
74
|
query=urlencode({'q': query}),
|
60
|
75
|
offset=offset)
|
61
|
76
|
|
|
77
|
+ language = get_region_code(params['language'])
|
|
78
|
+
|
62
|
79
|
params['cookies']['SRCHHPGUSR'] = \
|
63
|
|
- 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0] +\
|
64
|
|
- '&ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
|
|
80
|
+ 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
|
|
81
|
+
|
|
82
|
+ params['cookies']['_EDGE_S'] = 'mkt=' + language +\
|
|
83
|
+ '&ui=' + language + '&F=1'
|
65
|
84
|
|
66
|
85
|
params['url'] = base_url + search_path
|
67
|
86
|
if params['time_range'] in time_range_dict:
|
|
@@ -106,3 +125,22 @@ def response(resp):
|
106
|
125
|
|
107
|
126
|
# return results
|
108
|
127
|
return results
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+# get supported languages from their site
|
|
131
|
+def _fetch_supported_languages(resp):
|
|
132
|
+ supported_languages = []
|
|
133
|
+ dom = html.fromstring(resp.text)
|
|
134
|
+
|
|
135
|
+ regions_xpath = '//div[@id="region-section-content"]' \
|
|
136
|
+ + '//ul[@class="b_vList"]/li/a/@href'
|
|
137
|
+
|
|
138
|
+ regions = dom.xpath(regions_xpath)
|
|
139
|
+ for region in regions:
|
|
140
|
+ code = re.search('setmkt=[^\&]+', region).group()[7:]
|
|
141
|
+ if code == 'nb-NO':
|
|
142
|
+ code = 'no-NO'
|
|
143
|
+
|
|
144
|
+ supported_languages.append(code)
|
|
145
|
+
|
|
146
|
+ return supported_languages
|