-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathmergehistory.py
78 lines (61 loc) · 1.72 KB
/
mergehistory.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
#!/usr/bin/python3
"""
mergehistory.py
MediaWiki API Demos
Demo of `mergehistory` module: Merge the page revisions of Oldpage
dating up to 2015-12-31T04:37:41Z into Newpage
MIT license
"""
import requests
S = requests.Session()
URL = "https://test.wikipedia.org/w/api.php"
# Step 1: Retrieve a login token
PARAMS_1 = {
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_1)
DATA = R.json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
# Step 2: Send a post request to log in using the clientlogin method.
# import rights can't be granted using Special:BotPasswords
# hence using bot passwords may not work.
# See https://www.mediawiki.org/wiki/API:Login for more
# information on log in methods.
PARAMS_2 = {
"action": "clientlogin",
"username": "username",
"password": "password",
"format": "json",
"loginreturnurl": "http://127.0.0.1:5000/",
"logintoken": LOGIN_TOKEN
}
R = S.post(URL, data=PARAMS_2)
DATA = R.json()
# Step 3: While logged in, retrieve a CSRF token
PARAMS_3 = {
"action": "query",
"meta": "tokens",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_3)
DATA = R.json()
CSRF_TOKEN = DATA["query"]["tokens"]["csrftoken"]
# Step 4: Send a POST request to merge the page revisions
# of Oldpage dating up to 2015-12-31T04:37:41Z into Newpage
PARAMS_4 = {
"action":"mergehistory",
"from":"Oldpage",
"to":"Newpage",
"format":"json",
"timestamp":"2015-12-31T04:37:41Z",
"reason":"Reason",
"token" : CSRF_TOKEN
}
R = S.post(URL, data=PARAMS_4)
DATA = R.text
print(DATA)
# To merge entire history of Oldpage to Newpage,
# remove the "timestamp" parameter in step 4