-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFriendlyRecordsBot.py
84 lines (73 loc) · 2.3 KB
/
FriendlyRecordsBot.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
import os
import requests
import json
from FriendlyRecords import *
from KcwikiClient import KcwikiClient
def downVoiceFromFriendlyRecords(path):
rebuildDataJson()
f = open('friendlyvoicelist.json', 'r')
js = json.load(f)
urls = []
fns = []
genmp3Urls_filenames(js, urls, fns)
downloadmp3(urls, fns, path)
def loginWiki(mysession):
print("Login start, getting login token...")
kcwikiAPIUrl = 'https://zh.kcwiki.org/api.php'
with open('config.json', 'r') as f:
cfg = json.loads(f.read())
user_name = cfg['login_config']['user_name']
user_pswd = cfg['login_config']['password']
f.close()
print("Login token get, ready to login...")
rdata = {
'action': 'query',
'meta': 'tokens',
'type': 'login',
'format': 'json'
}
resp = mysession.post(kcwikiAPIUrl, data=rdata)
resp_json = resp.json()
rdata = {
'action': 'login',
'format': 'json',
'lgtoken': resp_json['query']['tokens']['logintoken'],
'lgname': user_name,
'lgpassword': user_pswd
}
resp = mysession.post(kcwikiAPIUrl, data=rdata)
resp_json = resp.json()
rdata = {
'action': 'query',
'meta': 'tokens',
'format': 'json'
}
print("Getting edit token...")
resp = mysession.post(kcwikiAPIUrl, data=rdata)
resp_json = resp.json()
print("login successfully!")
print("Edit token is " + resp_json['query']['tokens']['csrftoken'])
return resp_json['query']['tokens']['csrftoken']
def uploadVoice(path, mysession, editToken):
kcwikiAPIUrl = 'https://zh.kcwiki.org/api.php'
filelist = os.listdir(path)
i = 0
for fff in filelist:
rdata = {
'action': 'upload',
'token': editToken,
'format': 'json',
'filename': fff,
}
resp = mysession.post(kcwikiAPIUrl, data=rdata, files=[('file', open(os.path.join(path, fff), 'rb'))])
print(resp.content)
print('Uploading ' + fff)
i += 1
print("Upload Finished!")
print("Uploaded {} files.".format(i))
if __name__ == "__main__":
path = ''
downVoiceFromFriendlyRecords(path)
mySess = requests.session()
editToken = loginWiki(mySess)
uploadVoice(path, mySess, editToken)