forked from wikimedia/mediawiki-tools-grabbers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks_table.py
76 lines (64 loc) · 2.33 KB
/
blocks_table.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
#!/usr/bin/env python
# Released into the public domain
# By Legoktm & Uncyclopedia development team, 2013
import oursql
import settings
import mw
uncy = mw.Wiki(settings.api_url)
def gen():
go_on = True
params = {'action':'query',
'list':'blocks',
'bklimit':'max',
'bkprop':'id|user|userid|by|byid|timestamp|expiry|reason|range|flags',
}
while go_on:
req=uncy.request(params)
print 'fetched'
yield req['query']['blocks']
if req.has_key('query-continue'):
params['bkstart'] = req['query-continue']['blocks']['bkstart']
print params['bkstart']
else:
go_on = False
def insert():
print 'Populating the ipblocks table....'
conn = oursql.connect(host=settings.db_host, user=settings.db_user, passwd=settings.db_pass,
db=settings.db_name)
cur = conn.cursor()
cur.executemany('INSERT INTO `ipblocks` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', parse(gen()))
conn.close()
def parse(blocks):
for blockset in blocks:
for block in blockset:
print block
x = list()
if block['reason'].startswith('Autoblocked because your IP address'):
continue
x.append(int(block['id']))
x.append(block['user'])
x.append(int(block['userid']))
x.append(int(block['byid']))
x.append(block['by'])
x.append(block['reason'])
x.append(convert_ts(block['timestamp']))
x.append(0) # ipb_auto
x.append(int(block.has_key('anononly')))
x.append(int(block.has_key('nocreate')))
x.append(int(block.has_key('autoblock')))
if block['expiry'] == 'infinity':
x.append('infinity')
else:
x.append(convert_ts(block['expiry']))
x.append(block['rangestart'])
x.append(block['rangeend'])
x.append(0) # ipb_deleted
x.append(int(block.has_key('noemail')))
x.append(int(not block.has_key('allowusertalk')))
x.append(None)
yield tuple(x)
def convert_ts(i):
output = i.replace('-','').replace('T','').replace(':','').replace('Z','')
return output
if __name__ == "__main__":
insert()