-
Notifications
You must be signed in to change notification settings - Fork 6
/
protected_titles_table.py
68 lines (57 loc) · 1.9 KB
/
protected_titles_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
#!/usr/bin/env python
# Released into the public domain
# By Legoktm & Uncyclopedia development team, 2013
import oursql
import mw
import settings
uncy = mw.Wiki(settings.api_url)
def gen():
go_on = True
params = {'action': 'query',
'list': 'protectedtitles',
'ptlimit': 'max',
'ptprop': 'timestamp|user|comment|expiry|level',
}
while go_on:
req = uncy.request(params)
print 'fetched'
# print type(req['query']['blocks'])
yield req['query']['protectedtitles']
if 'query-continue' in req:
params['ptstart'] = req['query-continue']['protectedtitles']['ptstart']
print params['ptstart']
else:
go_on = False
def insert():
print 'Populating the protected_titles 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 `protected_titles` VALUES (?,?,?,?,?,?,?);', parse(gen()))
conn.close()
def parse(pts):
for ptset in pts:
for pt in ptset:
print pt
x=list()
x.append(int(pt['ns']))
#sanitize the title
if not int(pt['ns']):
x.append(pt['title'])
else:
x.append(pt['title'].split(':',1)[1])
x.append(int(pt['userid']))
x.append(pt['comment'])
x.append(convert_ts(pt['timestamp']))
if pt['expiry'] == 'infinity':
x.append('infinity')
else:
x.append(convert_ts(pt['expiry']))
x.append(pt['level'])
yield tuple(x)
def convert_ts(i):
#2013-01-05T01:16:52Z
output = i.replace('-','').replace('T','').replace(':','').replace('Z','')
return output
if __name__ == "__main__":
insert()