-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikibase.py
executable file
·363 lines (327 loc) · 11.5 KB
/
wikibase.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
'''
Allows to conveniently interface with the WikiBase API.
see:
* https://www.mediawiki.org/wiki/Wikibase/DataModel/JSON
* https://wikibase.oho.wiki/index.php?title=Special:NewItem
* https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity
* https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities
* https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities
* https://wikibase.oho.wiki/api.php?action=help&modules=wbeditentity
* https://wikibase.oho.wiki/api.php?action=help&modules=wbgetentities
* https://wikibase.oho.wiki/api.php?action=help&modules=wbsearchentities
'''
import json
import re
import logging
import requests
try: # for Python 3
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection
API_URL_MEDIA_WIKI = 'https://www.wikidata.org/w/api.php'
API_URL_OHO = 'https://wikibase.oho.wiki/api.php'
def enable_debug():
'''
Enabling debugging at http.client level (requests->urllib3->http.client)
you will see the REQUEST, including HEADERS and DATA,
and RESPONSE with HEADERS but without DATA.
the only thing missing will be the response.body which is not logged.
'''
HTTPConnection.debuglevel = 1
# you need to initialize logging,
# otherwise you will not see anything from requests
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
class WBSession:
'''
Represents a session of HTTP communication with a Wiki-Base instance,
through its "api.php".
'''
def __init__(self, api_url):
self.http_sess = requests.Session()
self.api_url = api_url
def call_api(self, params=None, data=None, method='POST'):
'''
Calls the MediaWiki API (api.php) with the given parameters.
'''
req = self.http_sess.request(method=method, url=self.api_url, params=params, data=data)
return req
def close(self):
'''
Closes this session.
After calling this, further use of other methods will fail.
'''
self.http_sess.close()
def bot_login(self, bot_user, bot_passwd):
'''
see: https://www.mediawiki.org/wiki/API:Login#Method_1._login
'''
# Retrieve login token first
params_login_token = {
'action':"query",
'meta':"tokens",
'type':"login",
'format':"json"
}
req = self.call_api(params=params_login_token)
ans = req.json()
login_token = ans['query']['tokens']['logintoken']
#print(login_token)
# Send a post request to login. Using the main account for login is not
# supported. Obtain credentials via Special:BotPasswords
# (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
params_login = {
'action':"login",
'lgname':bot_user,
'lgpassword':bot_passwd,
'lgtoken':login_token,
'format':"json"
}
req = self.call_api(data=params_login)
ans = req.json()
#print(ans)
def fetch_login_token(self) -> str:
""" Fetch login token via `tokens` module """
res = self.call_api(
params={
'action': "query",
'meta': "tokens",
'type': "login",
'format': "json"})
data = res.json()
return data['query']['tokens']['logintoken']
def login(self, username, password):
"""
Send a post request along with login token, user information
and return URL to the API to log in on a wiki
https://www.mediawiki.org/wiki/API:Login#Method_2._clientlogin
"""
login_token = self.fetch_login_token()
response = self.call_api(
data={
'action': "clientlogin",
'username': username,
'password': password,
'loginreturnurl': 'http://127.0.0.1:5000/',
'logintoken': login_token,
'format': "json"
})
data = response.json()
login_success = data['clientlogin']['status'] == 'PASS'
if login_success:
print('Login success! Welcome, ' + data['clientlogin']['username'] + '!')
else:
raise RuntimeError('Failed to log into WikiBase at "%s", error: %d' %
(self.api_url, data['clientlogin']['messagecode']))
def request_token(self) -> str:
'''
Requests a standard token, required to do almost any interaction
with the API.
'''
res = self.call_api(params={'action':'query', 'meta':'tokens', 'format':'json'})
if res.status_code != 200:
raise RuntimeError('Failed to get token; HTTP error: {}' % res.status_code)
res_data = json.loads(res.content)
return res_data['query']['tokens']['csrftoken']
def clear_thing(self, part_id):
'''
Clears everything from an Item or Property.
'''
print('- Clear Item/Property ...')
res = self.call_api(
method='POST',
params = {
'action': 'wbeditentity',
'id': part_id,
'clear': 'true',
'format':'json',
'data': '{}'
},
data = {'token': self.request_token()}
)
ans = res.json()
if 'error' in ans:
raise RuntimeError('Failed creating item, reason: %s - %s'
% (ans['error']['code'], ans['error']['info']))
def add_wb_thing_claims(self, wb_id, claims={}):
'''
Adds claims to an item or property.
'''
data = { 'claims': claims }
return self.create_wb_thing_raw(None, data, wb_id)
def create_wb_thing_raw(self, item=True, data={}, wb_id=None) -> str:
'''
Creates a new WikiBase item.
'''
print('- Create Item/Property ...')
print(json.dumps(data))
params = {
'action': 'wbeditentity',
#'site': site,
#'title': title,
'format':'json',
'data': json.dumps(data)
}
if wb_id is None:
params['new'] = 'item' if item else 'property'
params['clear'] = 'true'
else:
params['id'] = wb_id
res = self.call_api(
method = 'POST',
params = params,
data = {'token': self.request_token()}
)
ans = res.json()
if 'error' in ans:
#print(ans)
if ' already has ' in ans['error']['info']:
# Item/Property already exists.
# -> Delete it and create it from anew.
# api.php?action=wbeditentity&clear=true&id=Q42&data={} [open in sandbox]
item_pat = re.compile('\[\[Item:(Q[0-9]+)')
prop_pat = re.compile('\[\[Property:(P[0-9]+)')
pat = item_pat if item else prop_pat
match = pat.search(ans['error']['info'])
wb_id = match.group(1)
self.clear_thing(wb_id)
return self.create_wb_thing_raw(item, data, wb_id)
raise RuntimeError('Failed creating item, reason: %s - %s'
% (ans['error']['code'], ans['error']['info']))
print(ans)
return ans['entity']['id']
def create_wb_thing(self, item=True, labels={}, descriptions={}, claims={}, property_type='string') -> str:
'''
Creates a WikiBase item or property,
and returns its id (eg. "Q123456" or "P12345") if successful.
@param property_type see the list at: https://wikibase.oho.wiki/index.php?title=Special:NewProperty
'''
data = {
'labels': {},
'descriptions': {},
}
if not item:
data['datatype'] = property_type # see the following list (extracted from: https://wikibase.oho.wiki/index.php?title=Special:NewProperty )
for label_lang in labels.keys():
if isinstance(labels[label_lang], list):
for i in range(0, len(labels[label_lang])):
data['labels'][label_lang] = {
'language': label_lang,
'value': labels[label_lang][i]
}
else:
data['labels'][label_lang] = {
'language': label_lang,
'value': labels[label_lang]
}
for desc_lang in descriptions.keys():
if isinstance(labels[desc_lang], list):
for i in range(0, len(descriptions[desc_lang])):
data['descriptions'][desc_lang] = {
'language': desc_lang,
'value': descriptions[desc_lang][i]
}
else:
desc = descriptions[desc_lang]
desc = (desc[:247] + '...') if len(desc) > 250 else desc
data['descriptions'][desc_lang] = {
'language': desc_lang,
'value': desc
}
return self.create_wb_thing_raw(item, data)
if __name__ == "__main__":
# Run as a CLI script
SAMPLE_DATA = '''
{
"labels": {
"en": {
"language": "en",
"value": "New York City"
},
"ar": {
"language": "ar",
"value": "\u0645\u062f\u064a\u0646\u0629 \u0646\u064a\u0648 \u064a\u0648\u0631\u0643"
}
},
"descriptions": {
"en": {
"language": "en",
"value": "largest city in New York and the United States of America"
},
"it": {
"language": "it",
"value": "citt\u00e0 degli Stati Uniti d'America"
}
},
"aliases": {
"en": [
{
"language": "en",
"value": "NYC"
},
{
"language": "en",
"value": "New York"
},
],
"fr": [
{
"language": "fr",
"value": "New York City"
},
{
"language": "fr",
"value": "NYC"
},
{
"language": "fr",
"value": "The City"
},
{
"language": "fr",
"value": "City of New York"
},
{
"language": "fr",
"value": "La grosse pomme"
}
]
}
"claims": {
"P17": [
{
"id": "q60$5083E43C-228B-4E3E-B82A-4CB20A22A3FB",
"mainsnak": {},
"type": "statement",
"rank": "normal",
"qualifiers": {
"P580": [],
"P5436": []
}
"references": [
{
"hash": "d103e3541cc531fa54adcaffebde6bef28d87d32",
"snaks": []
}
]
}
]
}
}
'''
'''
SAMPLE_DATA = '{}'
#ITEM_TITLE = 'TEST_EMPTY'
#SITE = 'oho'
#enable_debug()
wbs = WBSession(API_URL_OHO)
#wbs.bot_login(bot_user, bot_passwd)
wbs.login(USER, PASSWD)
#item_id = wbs.create_item(SITE, ITEM_TITLE, SAMPLE_DATA)
item_id = wbs.create_item(SAMPLE_DATA)
print(item_id)
'''