-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
445 lines (417 loc) · 14.6 KB
/
api.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# encoding: utf-8
import urllib2, urllib
from xml.dom.minidom import parseString
import gzip,StringIO
import httplib,socket
class Api(object):
"""The API implemetation for the MediaWiki API. Should work with
MediaWiki 1.18+"""
server = "example.org"
url = "http://example.org/api.php"
indexurl = "http://example.org/index.php"
default_data = {"format" : "xml"}
user_data = {}
logged_in = False
send_cookies = False
debugmode = False
additional_cookies = {}
httpsocket = None
"""Use a general socket, so we don't need to initiate for each request."""
def init_socket(self):
self.httpsocket = httplib.HTTPConnection(self.server, 80)
"""General response obtain (for both post and get methods)."""
def getresponse(self, method, url, query, headers, limit=1):
if limit > 5:
return None
try:
if method == 'POST':
self.httpsocket.request(method, url, query, headers)
else:
self.httpsocket.request(method, url+'?'+query, '', headers)
response = self.httpsocket.getresponse()
except httplib.BadStatusLine:
return None
except socket.error:
self.httpsocket.close()
self.httpsocket.connect()
response = self.getresponse(method, url, query, headers, limit+1)
except httplib.CannotSendRequest:
self.httpsocket.close()
self.httpsocket.connect()
response = self.getresponse(method, url, query, headers, limit+1)
return response
"""Post data."""
def post(self, data, tries=1):
"""Only try posting the data five times."""
if tries > 5:
return None
orgdata = data
data.update(self.default_data)
query = []
for d in data:
query.append('='.join((d, urllib.quote(unicode(data[d]).encode('utf-8')))))
query = '&'.join(query)
if self.debugmode:
print query
headers = {"Accept-encoding" : "gzip", 'Content-type': 'application/x-www-form-urlencoded'}
if self.send_cookies:
headers.update({"Cookie" : self.make_cookie()})
if data['action']=='edit':
headers.update({"Content-Type" : "application/x-www-form-urlencoded"})
if self.httpsocket is None:
self.init_socket()
try:
self.httpsocket.connect()
except socket.error:
return self.post(orgdata, tries+1)
response = self.getresponse("POST", "/api.php", query, headers)
if response is None:
return None
setcookie = response.getheader('Set-Cookie')
if not setcookie is None:
cookie = setcookie.split(";")[0].split("=")
self.additional_cookies.update({cookie[0] : cookie[1]})
compressedstream = StringIO.StringIO(response.read())
gzipper = gzip.GzipFile(fileobj=compressedstream)
self.httpsocket.close()
return gzipper.read()
"""Obtain the raw data of a document."""
def index_request(self, title, action, query={}):
data = {"title" : title, "action" : action}
data.update(query)
query = []
for d in data:
query.append('='.join((d, urllib.quote(data[d]))))
query = '&'.join(query)
headers = {"Accept-encoding" : "gzip"}
try:
if self.httpsocket is None:
self.init_socket()
if self.debugmode:
print query
response = self.getresponse("GET", "/index.php", query, headers)
if response is None:
return None
raw = response.read()
compressedstream = StringIO.StringIO(raw)
gzipper = gzip.GzipFile(fileobj=compressedstream)
return gzipper.read().decode("utf8")
except IOError:
return raw
"""Create cookie for the API requests."""
def make_cookie(self):
p = self.user_data["cookieprefix"]
if self.logged_in:
cookie = {p + "UserName" : self.user_data["username"],
p + "UserID" : self.user_data["userid"],
p + "Token" : self.user_data["token"],
p + "_session" : self.user_data["sessionid"]}
else:
cookie= {p + "_session" : self.user_data["sessionid"]}
cookie.update(self.additional_cookies)
line = ""
for c in cookie:
if line!="":
line += "; "
line += c + "=" + cookie[c]
return line
def create_page(self, title, content, summary="Bot edit"):
query = {"action" : "query", "prop" : "info|revisions", "intoken" : "edit", "titles" : title}
xml = parseString(self.post(query))
try:
t = xml.getElementsByTagName("revisions")[0]
return False
except IndexError:
pass
token = xml.getElementsByTagName("page")[0].attributes["edittoken"].value
query = {"action" : "edit", "title" : title, "text" : content, "bot" : "true",
"createonly" : "true", "summary" : summary, "token" : token}
if self.debugmode:
return False
t = self.post(query)
if t == None:
return False
xml = parseString(t)
try:
edit = xml.getElementsByTagName("edit")[0]
if edit.attributes["result"].value=="Success":
return True
except KeyError:
error = xml.getElementsByTagName("error")[0]
if error.attributes["code"].value == "articleexists":
return False
return False
def edit_page(self, title, content, summary="Bot edit", section=None):
query = {"action" : "query", "prop" : "info|revisions", "intoken" : "edit", "titles" : title}
t = self.post(query)
if t == None:
return False
xml = parseString(t)
token = xml.getElementsByTagName("page")[0].attributes["edittoken"].value
query = {"action" : "edit", "title" : title, "text" : content, "bot" : "true", "summary" : summary, "token" : token}
if section!=None:
query.update({"section" : str(section)})
if self.debugmode:
return False
t = self.post(query)
if t == None:
return False
xml = parseString(t)
return True
def delete_page(self, title, reason="Bot deletion"):
query = {"action" : "query", "prop" : "info|revisions", "intoken" : "delete", "titles" : title}
xml = parseString(self.post(query))
token = xml.getElementsByTagName("page")[0].attributes["deletetoken"].value
query = {"action" : "delete", "title" : title, "reason" : reason, "token" : token}
if self.debugmode:
return False
t = self.post(query)
if t == None:
return False
xml = parseString(t)
return True
def block_user(self, user, expiry, reason="Bot blockage"):
query = {"action" : "block", "gettoken" : "yes", "user" : user}
xml = parseString(self.post(query))
token = xml.getElementsByTagName("block")[0].attributes["blocktoken"].value
query = {"action" : "block", "user" : user, "expiry" : expiry, "reason" : reason, "token" : token}
if self.debugmode:
return False
t = self.post(query)
if t == None:
return False
return True
def get_token(self, title, tokentype='edit'):
query = {'action' : 'query', 'prop' : 'info|revisions', 'intoken' : tokentype, 'titles' : title}
xml = parseString(self.post(query))
token = xml.getElementsByTagName('page')[0].attributes["%stoken"%tokentype].value
return token
def move(self, old, new, summary="Bot edit"):
token = self.get_token(old, 'move')
query = {'action' : 'move', 'from' : old, 'to' : new,
'token' : token, 'movetalk' : 'true', 'reason' : summary}
if self.debugmode:
return False
t = self.post(query)
if t == None:
return False
xml = parseString(t)
return True
def login(self, username, password):
query = {"action" : "login", "lgname" : username, "lgpassword" : password}
t = self.post(query)
xml = parseString(t)
login = xml.getElementsByTagName("login")[0]
a = login.attributes
if a['result'].value == 'NeedToken':
cookieprefix = a["cookieprefix"].value
sessionid = a["sessionid"].value
self.user_data.update({"cookieprefix" : cookieprefix,
"sessionid" : sessionid})
self.send_cookies = True
token = a['token'].value
query = {"action" : "login", "lgname" : username,
"lgpassword" : password, "lgtoken" : token}
t = self.post(query)
if t == None:
return False
xml = parseString(t)
login = xml.getElementsByTagName("login")[0]
a = login.attributes
i = 1
while a['result'].value == 'NeedToken':
# keep trying
sessionid = a["sessionid"].value
self.user_data.update({ "sessionid" : sessionid})
token = a['token'].value
query = {"action" : "login", "lgname" : username,
"lgpassword" : password, "lgtoken" : token}
t = self.post(query)
print t
if t == None:
return False
xml = parseString(t)
login = xml.getElementsByTagName("login")[0]
a = login.attributes
i += 1
if i > 9:
return False
try:
login = xml.getElementsByTagName("login")[0]
except IndexError:
return False
a = login.attributes
if a["result"].value == "Success":
userid = a["lguserid"].value
username = a["lgusername"].value
token = a["lgtoken"].value
self.user_data.update({"userid" : userid, "username" : username,
"token" : token})
self.logged_in = True
return True
return False
def get_list(self, amount, namespace="0"):
query = {"action" : "query", "list" : "random", "rnnamespace" : namespace, "rnlimit" : str(amount), "prop" : "info"}
try:
xml = parseString(self.post(query))
except TypeError:
return []
pages = []
for page in xml.getElementsByTagName("page"):
pages.append({"title" : page.attributes["title"].value, "id" : page.attributes["id"].value})
return pages
def get_image(self, title):
query = {"action" : "query", "prop" : "imageinfo", "titles" : title,
"iiprop" : "timestamp|user|url|size" }
try:
xml = parseString(self.post(query))
except TypeError:
return None
try:
image = xml.getElementsByTagName("ii")[0]
except IndexError:
return None
return { "timestamp" : image.attributes["timestamp"].value,
"user" : image.attributes["user"].value,
"url" : image.attributes["url"].value,
"descriptionurl" : image.attributes["descriptionurl"].value,
"size" : int(image.attributes["size"].value),
"width" : int(image.attributes["width"].value),
"height" : int(image.attributes["height"].value) }
def get_includelist(self, title, amount):
query = {"action" : "query", "list" : "embeddedin", "eititle" : title, "rnnamespace" : "0", "eilimit" : "500", "prop" : "info", "eifilterredir" : "nonredirects"}
xml = parseString(self.post(query))
pages = []
i = 0
for page in xml.getElementsByTagName("ei"):
if page.attributes["ns"].value=="0":
pages.append({"title" : page.attributes["title"].value, "id" : page.attributes["pageid"].value})
i += 1
if i > amount:
break
return pages
def get_content(self, title, section=None):
query = {}
if section!=None:
query = {"section" : str(section)}
c = self.index_request(title.encode("utf-8"), "raw", query)
if c == None:
return None
if "Invalid file extension found in PATH_INFO or QUERY_STRING. Raw pages must be accessed through the primary script entry point." in c:
return None
return c
def get_title_search(self, search, amount, namespace="0|4|14|100", offset=0):
query = {'action' : 'query', 'list' : 'search', 'srsearch' : search,
'srnamespace' : str(namespace), 'srwhat' : 'title',
'srlimit' : str(amount), 'sroffset' : str(offset)}
xml = parseString(self.post(query))
pages = []
for page in xml.getElementsByTagName('p'):
if page.attributes['ns'].value==str(namespace):
pages.append({"title" : page.attributes["title"].value})
return pages
def get_text_search(self, search, amount, namespace="0|4|14|100", offset=0):
query = {'action' : 'query', 'list' : 'search', 'srsearch' : search,
'srnamespace' : str(namespace), 'srwhat' : 'text',
'srlimit' : str(amount), 'sroffset' : str(offset)}
xml = parseString(self.post(query))
pages = []
for page in xml.getElementsByTagName('p'):
if page.attributes['ns'].value==str(namespace):
pages.append({"title" : page.attributes["title"].value})
return pages
def get_category_pages(self, category, amount, namespace="0|4|14|100", offset=0):
query = {'action' : 'query', 'list' : 'categorymembers',
'cmtitle' : category, 'cmprop' : 'ids|title',
'cmnamespace' : str(namespace), 'cmlimit' : str(amount) }
xml = parseString(self.post(query))
pages = []
i = 0
namespaces = str(namespace).split('|')
for page in xml.getElementsByTagName("cm"):
if page.attributes["ns"].value in namespaces:
pages.append({"title" : page.attributes["title"].value, "id" : page.attributes["pageid"].value})
i += 1
if i > amount:
break
return pages
def get_recentchanges(self, amount, start=None, namespace="0|4|14|100", botedits=False):
if start==None:
start = ''
query = {'action' : 'query', 'list' : 'recentchanges',
'rclimit' : str(amount), 'rcnamespace' : str(namespace),
'rcprop' : 'ids|title'}
if start!=None and start!='':
query.update({'rcend' : start})
if botedits:
query.update({'rcshow' : '!redirect'})
else:
query.update({'rcshow' : '!bot|!redirect'})
try:
xml = parseString(self.post(query))
except TypeError:
return []
pages = []
i = 0
namespaces = str(namespace).split('|')
for page in xml.getElementsByTagName("rc"):
if page.attributes["ns"].value in namespaces:
pages.append({"title" : page.attributes["title"].value,
"id" : page.attributes["pageid"].value,
"type" : page.attributes["type"].value,
"rcid" : page.attributes["rcid"].value,
"revid" : page.attributes["revid"].value,
"old_revid" : page.attributes["old_revid"].value})
i += 1
if i > amount:
break
return pages
def get_user(self, user):
query = {'action' : 'query', 'list' : 'users',
'ususers' : user, 'usprop' : 'blockinfo|groups|editcount|registration|emailable|gender'}
xml = parseString(self.post(query))
tmp = None
for user in xml.getElementsByTagName('user'):
groups = []
for group in user.getElementsByTagName('g'):
groups.append(group.firstChild.data)
tmp = {'groups' : groups,
'editcount' : user.attributes['editcount'].value,
'registration' : user.attributes['registration'].value,
'emailable' : user.hasAttribute('emailable'),
'gender' : user.attributes['gender'].value}
return tmp
def get_edit(self, revid, old_revid):
if old_revid == None:
revids = revid
else:
revids = "%s|%s" % (revid, old_revid)
query = {'action' : 'query',
'revids' : revids, 'prop' : 'revisions',
'rvprop' : 'ids|flags|user|userid|size|comment|content'}
xml = parseString(self.post(query))
after = None
before = None
for page in xml.getElementsByTagName("rev"):
try:
c = page.firstChild.data
except AttributeError:
c = None
tmp = {'user' : page.attributes["user"].value,
'userid' : page.attributes["userid"].value,
'revid' : page.attributes["revid"].value,
'size' : page.attributes["size"].value,
'comment' : page.attributes["comment"].value,
'content' : c,
'anon' : page.hasAttribute('anon'),
'minor' : page.hasAttribute('minor')}
if page.attributes['revid'].value == revid:
after = tmp
else:
before = tmp
return (before, after)
def __init__(self, server, debug=False):
self.server = server
self.url = "http://%s/api.php" % server
self.indexurl = "http://%s/index.php" % server
self.debugmode = debug