-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpage_restrictions_table.py
86 lines (73 loc) · 2.53 KB
/
page_restrictions_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
77
78
79
80
81
82
83
84
85
86
#!/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(ns=0):
#see full_gen() below
go_on = True
params = {'action': 'query',
'generator': 'allpages',
'gaplimit': 'max',
'gapprtype': 'edit|move',
'gapprlevel': 'sysop|autoconfirmed',
'gapnamespace': ns,
'prop': 'info',
'inprop': 'protection',
}
while go_on:
req = uncy.request(params)
print 'fetched'
# print type(req['query']['blocks'])
if 'query' in req:
yield req['query']['pages']
if 'query-continue' in req:
print req['query-continue']
for key in req['query-continue']['allpages'].keys():
params[key] = req['query-continue']['allpages'][key]
else:
go_on = False
def insert():
print 'Populating the page_restrictions 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 IGNORE INTO `page_restrictions` VALUES (?,?,?,?,?,?,?);', parse(full_gen()))
conn.close()
def parse(pts):
for group in pts:
for id in group.keys():
print group[id]
for prot in group[id]['protection']:
x=list()
x.append(int(id))
x.append(prot['type'])
x.append(prot['level'])
x.append(int(prot.has_key('cascade')))
x.append(None) # pr_user
if prot['expiry'] == 'infinity':
x.append(None)
else:
x.append(convert_ts(prot['expiry']))
x.append(None) # pr_id
yield tuple(x)
def convert_ts(i):
#2013-01-05T01:16:52Z
output = i.replace('-','').replace('T','').replace(':','').replace('Z','')
return output
def full_gen():
#wrapper for gen() above
#https://en.wikipedia.org/w/api.php?action=query&meta=siteinfo&siprop=namespaces&format=jsonfm
params = {'action':'query',
'meta':'siteinfo',
'siprop':'namespaces',
}
req = uncy.request(params)
for key in req['query']['namespaces'].keys():
if int(key) >= 0:
for item in gen(key):
yield item
if __name__ == "__main__":
insert()