2013-10-23 21:53:27 +00:00
|
|
|
from json import loads
|
|
|
|
from urllib import urlencode, quote
|
|
|
|
|
|
|
|
url = 'https://en.wikipedia.org/'
|
|
|
|
|
2014-01-30 00:44:12 +00:00
|
|
|
search_url = url + 'w/api.php?action=query&list=search&{query}&srprop=timestamp&format=json&sroffset={offset}' # noqa
|
2014-01-20 01:31:20 +00:00
|
|
|
|
2013-10-23 21:53:27 +00:00
|
|
|
number_of_results = 10
|
|
|
|
|
2014-01-20 01:31:20 +00:00
|
|
|
|
2013-10-23 21:53:27 +00:00
|
|
|
def request(query, params):
|
2014-01-30 00:44:12 +00:00
|
|
|
offset = (params['pageno'] - 1) * 10
|
|
|
|
params['url'] = search_url.format(query=urlencode({'srsearch': query}),
|
|
|
|
offset=offset)
|
2013-10-23 21:53:27 +00:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
|
|
|
search_results = loads(resp.text)
|
|
|
|
res = search_results.get('query', {}).get('search', [])
|
2014-01-20 01:31:20 +00:00
|
|
|
return [{'url': url + 'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8')), # noqa
|
2014-01-14 09:34:35 +00:00
|
|
|
'title': result['title']} for result in res[:int(number_of_results)]]
|