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