-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdoubleRedirect.py
53 lines (46 loc) · 1.31 KB
/
doubleRedirect.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0
import pywikibot
from pywikibot.data import api
import re
site = pywikibot.Site('wikidata', 'wikidata')
site.login()
repo = site.data_repository()
def redirect(fromId, toId):
# get token
params = {
'action': 'query',
'meta': 'tokens'
}
req = api.Request(site=site, **params)
data = req.submit()
# create redirect
params3 = {
'action': 'wbcreateredirect',
'from': fromId,
'to': toId,
'bot': 1,
'token': data['query']['tokens']['csrftoken']
}
req3 = api.Request(site=site, **params3)
data3 = req3.submit()
def main():
params = {
'action': 'query',
'list': 'querypage',
'qppage': 'DoubleRedirects',
'qplimit': 5000
}
req = api.Request(site=site, **params)
data = req.submit()
for m in data['query']['querypage']['results']:
try:
if m['ns'] == 0:
item1 = pywikibot.ItemPage(repo, m['title'])
item2 = item1.getRedirectTarget().getRedirectTarget().getID()
redirect(m['title'], item2)
except:
pass
if __name__ == "__main__":
main()