Browse Source

Merge pull request #1273 from MarcAbonce/google-hotfix

[fix] Force English results in Google when using en-US
Adam Tauber 6 years ago
parent
commit
e6aaf20898
No account linked to committer's email
2 changed files with 7 additions and 7 deletions
  1. 3
    6
      searx/engines/google.py
  2. 4
    1
      tests/unit/engines/test_google.py

+ 3
- 6
searx/engines/google.py View File

91
 search_path = '/search'
91
 search_path = '/search'
92
 search_url = ('https://{hostname}' +
92
 search_url = ('https://{hostname}' +
93
               search_path +
93
               search_path +
94
-              '?{query}&start={offset}&gws_rd=cr&gbv=1&lr={lang}&ei=x')
94
+              '?{query}&start={offset}&gws_rd=cr&gbv=1&lr={lang}&hl={lang_short}&ei=x')
95
 
95
 
96
 time_range_search = "&tbs=qdr:{range}"
96
 time_range_search = "&tbs=qdr:{range}"
97
 time_range_dict = {'day': 'd',
97
 time_range_dict = {'day': 'd',
175
     else:
175
     else:
176
         country = 'US'
176
         country = 'US'
177
 
177
 
178
-    # temporary fix until a way of supporting en-US is found
179
-    if language == 'en-US':
180
-        country = 'GB'
181
-
182
     url_lang = 'lang_' + language
178
     url_lang = 'lang_' + language
183
 
179
 
184
     if use_locale_domain:
180
     if use_locale_domain:
191
     params['url'] = search_url.format(offset=offset,
187
     params['url'] = search_url.format(offset=offset,
192
                                       query=urlencode({'q': query}),
188
                                       query=urlencode({'q': query}),
193
                                       hostname=google_hostname,
189
                                       hostname=google_hostname,
194
-                                      lang=url_lang)
190
+                                      lang=url_lang,
191
+                                      lang_short=language)
195
     if params['time_range'] in time_range_dict:
192
     if params['time_range'] in time_range_dict:
196
         params['url'] += time_range_search.format(range=time_range_dict[params['time_range']])
193
         params['url'] += time_range_search.format(range=time_range_dict[params['time_range']])
197
 
194
 

+ 4
- 1
tests/unit/engines/test_google.py View File

26
         self.assertIn('url', params)
26
         self.assertIn('url', params)
27
         self.assertIn(query, params['url'])
27
         self.assertIn(query, params['url'])
28
         self.assertIn('google.fr', params['url'])
28
         self.assertIn('google.fr', params['url'])
29
+        self.assertIn('fr', params['url'])
29
         self.assertIn('fr', params['headers']['Accept-Language'])
30
         self.assertIn('fr', params['headers']['Accept-Language'])
30
 
31
 
31
         dicto['language'] = 'en-US'
32
         dicto['language'] = 'en-US'
32
         params = google.request(query, dicto)
33
         params = google.request(query, dicto)
33
-        self.assertIn('google.co', params['url'])
34
+        self.assertIn('google.com', params['url'])
35
+        self.assertIn('en', params['url'])
34
         self.assertIn('en', params['headers']['Accept-Language'])
36
         self.assertIn('en', params['headers']['Accept-Language'])
35
 
37
 
36
         dicto['language'] = 'zh'
38
         dicto['language'] = 'zh'
37
         params = google.request(query, dicto)
39
         params = google.request(query, dicto)
38
         self.assertIn('google.com', params['url'])
40
         self.assertIn('google.com', params['url'])
41
+        self.assertIn('zh-CN', params['url'])
39
         self.assertIn('zh-CN', params['headers']['Accept-Language'])
42
         self.assertIn('zh-CN', params['headers']['Accept-Language'])
40
 
43
 
41
     def test_response(self):
44
     def test_response(self):