-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathduplicate_page.py
106 lines (85 loc) · 2.42 KB
/
duplicate_page.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
duplicate_page.py
MediaWiki API
"""
import requests
import time
import io
S = requests.Session()
URL = "https://dragalialost.gamepedia.com/api.php"
# Retrieve login token first
PARAMS_0 = {
'action': "query",
'meta': "tokens",
'type': "login",
'format': "json"
}
R = S.get(url=URL, params=PARAMS_0)
DATA = R.json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
print(LOGIN_TOKEN)
PARAMS_1 = {
'action': "login",
'lgname': "BOT NAME",
'lgpassword': "BOT PASSWORD",
'lgtoken': LOGIN_TOKEN,
'format': "json"
}
R = S.post(URL, data=PARAMS_1)
DATA = R.json()
print(DATA)
PARAMS_2 = {
"action": "query",
"meta": "tokens",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_2)
DATA = R.json()
CSRF_TOKEN = DATA['query']['tokens']['csrftoken']
queryPARAMS = {
'action': "query",
'prop': "revisions",
'rvslots': "*",
'rvprop': "content",
'formatversion': "2",
'format': "json"
}
editPARAMS = {
'action': "edit",
'summary': "Creating archived page",
'token': CSRF_TOKEN,
'bot': "1",
'format': "json",
'createonly': "1"
}
def archive_group(filepath, wikipath):
with io.open(filepath, 'rt', encoding='utf8') as file:
for line in file:
old_title = line.replace("\n", "")
queryPARAMS['titles'] = old_title
print("Fetching ", old_title)
json = S.get(url=URL, params=queryPARAMS)
archive_data = json.json()
pages = archive_data['query']["pages"]
content = pages[0]['revisions'][0]['slots']['main']['content']
new_title = wikipath.format(old_title)
editPARAMS['title'] = new_title
editPARAMS['text'] = content
print("Posting ", new_title)
json = S.post(URL, data=editPARAMS)
archive_data = json.json()
print(archive_data)
time.sleep(5)
archive_group("C:/Users/Canim/Documents/Weapon List.txt", "Weapons/Archive/Version 1.23.1/{}")
archive_group("C:/Users/Canim/Documents/Wyrmprint List.txt", "Wyrmprints/Archive/Version 1.23.1/{}")
# Step 4: Send a POST request to logout
PARAMS_3 = {
"action": "logout",
"token": CSRF_TOKEN,
"format": "json"
}
R = S.post(URL, data=PARAMS_3)
DATA = R.json()
print(DATA)