-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
169 lines (124 loc) · 4.03 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import requests
import os
from urllib.parse import quote
S = requests.Session()
URL = "https://thwiki.cc/api.php"
ALLOW_LIST = ("png", "gif", "jpg", "jpeg",
"pdf", "mid", "midi", "ogg", "mp3", "svg")
def read_bot_token():
"""
using local token file, you need to make a BotToken.txt file
the token file should like
"""
"""
botusername=XXXXX
botpassword=XXXXX
"""
f = open(r"BotToken.txt", "r", encoding="utf-8")
bot_token = {}
for line in f.readlines():
name, value = line.strip("\n").split("=", 1)
bot_token[name] = value
return bot_token
def fetch_login_token():
""" Fetch login token via `tokens` module """
response = S.get(url=URL, params={
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
})
data = response.json()
return data["query"]["tokens"]["logintoken"]
def start_client_login(username, password):
"""
Send a post request along with login token, user information
and return URL to the API to log in on a wiki
"""
login_token = fetch_login_token()
response = S.post(url=URL, data={
"action": "clientlogin",
"username": username,
"password": password,
"loginreturnurl": "http://127.0.0.1:5000/",
"logintoken": login_token,
"format": "json"
})
data = response.json()
if data["clientlogin"]["status"] == "PASS":
print("Clientlogin Success")
else:
print("Clientlogin Failed")
def start_bot_login(botusername, botpassword):
"""
using BotPasswords to login with API access
"""
login_token = fetch_login_token()
response = S.post(url=URL, data={
"action": "login",
"lgname": botusername,
"lgpassword": botpassword,
"lgtoken": login_token,
"format": "json"
})
data = response.json()
if data["login"]["result"] == "Success":
print("Login Success")
else:
print("Login Failed")
return
def retrieve_csrf_token():
"""retrieve Csrf token after login"""
response = S.get(url=URL, params={
"action": "query",
"meta": "tokens",
"format": "json"
})
data = response.json()
return data["query"]["tokens"]["csrftoken"]
def upload_file(filepath, filename, csrftoken, comment=""):
"""FILE DIC
("filename", "fileobject", "content-type", "headers")
"""
encoded_filename = quote(filename)
# FILE = {"file":("红黑月历.png",open(filepath,"rb"),"multipart/form-data")}
file = {"file": (encoded_filename, open(
filepath, "rb"), "multipart/form-data")}
response = S.post(url=URL, files=file, data={
"action": "upload",
"filename": filename,
"format": "json",
"token": csrftoken,
"comment": comment,
"ignorewarnings": 1
})
data = response.json()
try:
upload_status = data["upload"]["result"]
if upload_status == "Success":
print("Upload {name} Success".format(name=filename))
else:
print("Upload {name} failed".format(name=filename))
except KeyError:
err = data.get('error')
err_code = err.get('code')
err_info = err.get('info')
if(err and err_code and err_info):
print(err_code)
print(f" {err_info}")
print("Upload {name} failed".format(name=filename))
return
def multi_upload(path, csrftoken, comment=""):
os.chdir(path)
files = [fn for fn in os.listdir(path) if fn.endswith(ALLOW_LIST)]
for f in files:
upload_file(f, f, csrftoken, comment)
return
BOT_TOKEN = read_bot_token()
start_bot_login(BOT_TOKEN["botusername"], BOT_TOKEN["botpassword"])
CSRF_TOKEN = retrieve_csrf_token() # CSRF_TOKEN can use for many times
# upload_file(FILE_PATH,"2020-03-27-3.png",CSRF_TOKEN)
PATH = input("Please input directory path:\n")
COMMENT = input(
"Please input upload comment(support wikitext) within single line:\n")
multi_upload(PATH, CSRF_TOKEN, COMMENT)