Browse Source

[enh][fix] support base_urls without language code ++ extra whitespaces removed

Adam Tauber 10 years ago
parent
commit
cb8acbd2c3
1 changed files with 22 additions and 11 deletions
  1. 22
    11
      searx/engines/mediawiki.py

+ 22
- 11
searx/engines/mediawiki.py View File

1
 ## general mediawiki-engine (Web)
1
 ## general mediawiki-engine (Web)
2
-# 
2
+#
3
 # @website     websites built on mediawiki (https://www.mediawiki.org)
3
 # @website     websites built on mediawiki (https://www.mediawiki.org)
4
 # @provide-api yes (http://www.mediawiki.org/wiki/API:Search)
4
 # @provide-api yes (http://www.mediawiki.org/wiki/API:Search)
5
-# 
5
+#
6
 # @using-api   yes
6
 # @using-api   yes
7
 # @results     JSON
7
 # @results     JSON
8
 # @stable      yes
8
 # @stable      yes
9
-# @parse       url, title 
9
+# @parse       url, title
10
 #
10
 #
11
 # @todo        content
11
 # @todo        content
12
 
12
 
13
 from json import loads
13
 from json import loads
14
+from string import Formatter
14
 from urllib import urlencode, quote
15
 from urllib import urlencode, quote
15
 
16
 
16
 # engine dependent config
17
 # engine dependent config
18
 language_support = True
19
 language_support = True
19
 paging = True
20
 paging = True
20
 number_of_results = 1
21
 number_of_results = 1
21
-    
22
+
22
 # search-url
23
 # search-url
23
 base_url = 'https://{language}.wikipedia.org/'
24
 base_url = 'https://{language}.wikipedia.org/'
24
-search_url = base_url + 'w/api.php?action=query&list=search&{query}&srprop=timestamp&format=json&sroffset={offset}&srlimit={limit}'  # noqa
25
+search_url = base_url + 'w/api.php?action=query\
26
+                                  &list=search\
27
+                                  &{query}\
28
+                                  &srprop=timestamp\
29
+                                  &format=json\
30
+                                  &sroffset={offset}\
31
+                                  &srlimit={limit}'
25
 
32
 
26
 
33
 
27
 # do search-request
34
 # do search-request
28
 def request(query, params):
35
 def request(query, params):
29
     offset = (params['pageno'] - 1) * number_of_results
36
     offset = (params['pageno'] - 1) * number_of_results
37
+    string_args = dict(query=urlencode({'srsearch': query}),
38
+                        offset=offset,
39
+                        limit=number_of_results)
40
+    format_strings = Formatter().parse(base_url)
30
 
41
 
31
     if params['language'] == 'all':
42
     if params['language'] == 'all':
32
         language = 'en'
43
         language = 'en'
33
     else:
44
     else:
34
         language = params['language'].split('_')[0]
45
         language = params['language'].split('_')[0]
35
-    
46
+
47
+    if len(format_strings) > 1:
48
+        string_args['language'] = language
49
+
36
     # write search-language back to params, required in response
50
     # write search-language back to params, required in response
37
     params['language'] = language
51
     params['language'] = language
38
 
52
 
39
-    params['url'] = search_url.format(query=urlencode({'srsearch': query}),
40
-                                      offset=offset,
41
-                                      limit=number_of_results,
42
-                                      language=language)
53
+    params['url'] = search_url.format(**string_args)
43
 
54
 
44
     return params
55
     return params
45
 
56
 
57
     # parse results
68
     # parse results
58
     for result in search_results['query']['search']:
69
     for result in search_results['query']['search']:
59
         url = base_url.format(language=resp.search_params['language']) + 'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8'))
70
         url = base_url.format(language=resp.search_params['language']) + 'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8'))
60
-        
71
+
61
         # append result
72
         # append result
62
         results.append({'url': url,
73
         results.append({'url': url,
63
                         'title': result['title'],
74
                         'title': result['title'],