-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmissingRedirect.py
More file actions
93 lines (86 loc) · 2.79 KB
/
missingRedirect.py
File metadata and controls
93 lines (86 loc) · 2.79 KB
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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0
import pywikibot
from pywikibot.data import api
from datetime import datetime, timedelta
import re
import sys
site = pywikibot.Site('wikidata', 'wikidata')
site.login()
repo = site.data_repository()
cat = pywikibot.Category(site, 'Category:Notability policy exemptions')
liste = list(cat.articles(recurse=5))
def merge(fromId,toId):
fromItem = pywikibot.ItemPage(repo,fromId)
toItem = pywikibot.ItemPage(repo,toId)
fromItem.mergeInto(toItem, ignore_conflicts='description')
clearItem(fromId)
fromItem.set_redirect_target(toItem, force=True, save=True)
def clearItem(fromId):
#get token
params = {
'action': 'query',
'meta': 'tokens'
}
req = api.Request(site=site, **params)
data = req.submit()
#clear item
params2 = {
'action': 'wbeditentity',
'id': fromId,
'clear': 1,
'data':'{}',
'bot': 1,
'summary': 'clearing item to prepare for redirect',
'token': data['query']['tokens']['csrftoken']
}
req2 = api.Request(site=site,**params2)
data2 = req2.submit()
def main():
time_file = 'missingRedirect_time.dat'
f1 = open(time_file,'r')
old_time_str = str(int(f1.read())+1)
f1.close()
now = datetime.now()
new_time = now - timedelta(minutes=10)
new_time_str = new_time.strftime("%Y%m%d%H%M%S")
rccontinue = old_time_str+'|0'
while True:
params = {
'action': 'query',
'list': 'recentchanges',
'rcprop': 'title|comment|timestamp',
'rcstart': old_time_str,
'rcend': new_time_str,
'rcdir': 'newer',
'rclimit' : 500,
'rctype': 'edit',
'rcnamespace':0,
'rctoponly':1,
'rccontinue':rccontinue
}
req = api.Request(site=site, **params)
data = req.submit()
for m in data['query']['recentchanges']:
timestamp = m['timestamp']
if 'comment' in m:
res = re.search('\/\* wbmergeitems-to:0\|\|(Q[0-9]+) \*\/', m['comment'])
if res:
#ignore items in Category:Notability policy exemptions
for ll in liste:
if m['title'] == ll.title()[5:]:
continue
try:
merge(m['title'],res.group(1))
except:
pass
if 'query-continue' in data:
rccontinue = data['query-continue']['recentchanges']['rccontinue']
else:
break
f1 = open(time_file, 'w')
f1.write(re.sub(r'\:|\-|Z|T', '', timestamp))
f1.close()
if __name__ == "__main__":
main()