[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
1 changed files with 22 additions and 11 deletions

View File

@ -11,6 +11,7 @@
# @todo content
from json import loads
from string import Formatter
from urllib import urlencode, quote
# engine dependent config
@ -21,25 +22,35 @@ number_of_results = 1
# search-url
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
def request(query, params):
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':
language = 'en'
else:
language = params['language'].split('_')[0]
if len(format_strings) > 1:
string_args['language'] = language
# write search-language back to params, required in response
params['language'] = language
params['url'] = search_url.format(query=urlencode({'srsearch': query}),
offset=offset,
limit=number_of_results,
language=language)
params['url'] = search_url.format(**string_args)
return params