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

This commit is contained in:
Adam Tauber 2014-09-04 21:19:11 +02:00
parent c38a743c6f
commit cb8acbd2c3

View file

@ -1,16 +1,17 @@
## general mediawiki-engine (Web) ## general mediawiki-engine (Web)
# #
# @website websites built on mediawiki (https://www.mediawiki.org) # @website websites built on mediawiki (https://www.mediawiki.org)
# @provide-api yes (http://www.mediawiki.org/wiki/API:Search) # @provide-api yes (http://www.mediawiki.org/wiki/API:Search)
# #
# @using-api yes # @using-api yes
# @results JSON # @results JSON
# @stable yes # @stable yes
# @parse url, title # @parse url, title
# #
# @todo content # @todo content
from json import loads from json import loads
from string import Formatter
from urllib import urlencode, quote from urllib import urlencode, quote
# engine dependent config # engine dependent config
@ -18,28 +19,38 @@ categories = ['general']
language_support = True language_support = True
paging = True paging = True
number_of_results = 1 number_of_results = 1
# search-url # search-url
base_url = 'https://{language}.wikipedia.org/' base_url = 'https://{language}.wikipedia.org/'
search_url = base_url + 'w/api.php?action=query&list=search&{query}&srprop=timestamp&format=json&sroffset={offset}&srlimit={limit}' # noqa search_url = base_url + 'w/api.php?action=query\
&list=search\
&{query}\
&srprop=timestamp\
&format=json\
&sroffset={offset}\
&srlimit={limit}'
# do search-request # do search-request
def request(query, params): def request(query, params):
offset = (params['pageno'] - 1) * number_of_results offset = (params['pageno'] - 1) * number_of_results
string_args = dict(query=urlencode({'srsearch': query}),
offset=offset,
limit=number_of_results)
format_strings = Formatter().parse(base_url)
if params['language'] == 'all': if params['language'] == 'all':
language = 'en' language = 'en'
else: else:
language = params['language'].split('_')[0] language = params['language'].split('_')[0]
if len(format_strings) > 1:
string_args['language'] = language
# write search-language back to params, required in response # write search-language back to params, required in response
params['language'] = language params['language'] = language
params['url'] = search_url.format(query=urlencode({'srsearch': query}), params['url'] = search_url.format(**string_args)
offset=offset,
limit=number_of_results,
language=language)
return params return params
@ -57,7 +68,7 @@ def response(resp):
# parse results # parse results
for result in search_results['query']['search']: for result in search_results['query']['search']:
url = base_url.format(language=resp.search_params['language']) + 'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8')) url = base_url.format(language=resp.search_params['language']) + 'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8'))
# append result # append result
results.append({'url': url, results.append({'url': url,
'title': result['title'], 'title': result['title'],