summaryrefslogtreecommitdiff
path: root/searx/engines/mediawiki.py
blob: bc4aab6df117897c9a7bf39d03b4f46e823eb5d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from json import loads
from urllib import urlencode, quote

url = 'https://en.wikipedia.org/'

search_url = url + 'w/api.php?action=query&list=search&{query}&srprop=timestamp&format=json'  # noqa

number_of_results = 10


def request(query, params):
    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', [])
    return [{'url': url + 'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8')),  # noqa
        'title': result['title']} for result in res[:int(number_of_results)]]