-
Notifications
You must be signed in to change notification settings - Fork 33
/
wikipedia.py
117 lines (85 loc) · 2.98 KB
/
wikipedia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# This file is part of Wike (com.github.hugolabe.Wike)
# SPDX-FileCopyrightText: 2021-24 Hugo Olabera <hugolabe@gmail.com>
# SPDX-License-Identifier: GPL-3.0-or-later
import json, urllib.parse
from gi.repository import Soup
# Create a Soup session and set user agent
session = Soup.Session.new()
session.set_user_agent('Wike/3.1.0 (https://github.com/hugolabe)')
# Get Wikipedia random page
def get_random(lang, callback):
endpoint = 'https://' + lang + '.wikipedia.org/w/api.php'
params = { 'action': 'query',
'generator': 'random',
'grnlimit': 1,
'grnnamespace': 0,
'prop': 'info',
'inprop': 'url',
'format': 'json' }
_request(endpoint, params, callback, None)
# Get random result from response data
def random_result(async_result):
response = session.send_and_read_finish(async_result)
data = response.get_data()
result = json.loads(data)
pages = result['query']['pages']
page_props = list(pages.values())[0]
uri = page_props['fullurl']
return uri
# Search Wikipedia with a limit of responses
def search(text, lang, limit, callback):
endpoint = 'https://' + lang + '.wikipedia.org/w/api.php'
params = { 'action': 'opensearch',
'search': text,
'limit': limit,
'namespace': 0,
'redirects': 'resolve',
'format': 'json' }
if callback:
_request(endpoint, params, callback, None)
return
data = _request(endpoint, params, None, None)
if data:
result = json.loads(data)
if len(result[1]) > 0:
return result[1], result[3]
return None
# Get search results from response data
def search_result(async_result):
response = session.send_and_read_finish(async_result)
data = response.get_data()
result = json.loads(data)
if len(result[1]) > 0:
return result[1], result[3]
else:
return None
# Get various properties for Wikipedia page
def get_properties(page, lang, callback, user_data):
endpoint = 'https://' + lang + '.wikipedia.org/w/api.php'
params = { 'action': 'parse',
'prop': 'sections|langlinks',
'redirects': 1,
'page': page,
'format': 'json' }
_request(endpoint, params, callback, user_data)
# Get properties result from response data
def properties_result(async_result):
response = session.send_and_read_finish(async_result)
data = response.get_data()
result = json.loads(data)
return result['parse']
# Perform query to Wikipedia API with given parameters
def _request(endpoint, params, callback, user_data):
global session
params_encoded = urllib.parse.urlencode(params, safe='%=&|')
message = Soup.Message.new_from_encoded_form('GET', endpoint, params_encoded)
if callback:
session.send_and_read_async(message, 0, None, callback, user_data)
return
else:
response = session.send_and_read(message, None)
if message.get_status() == Soup.Status.OK:
data = response.get_data()
return data
else:
return None