Skip to content

Commit 0236210

Browse files
committed
牌堆市场支持资源包下载与多种牌堆
1 parent 94e6a7e commit 0236210

File tree

1 file changed

+103
-24
lines changed

1 file changed

+103
-24
lines changed

OlivaDiceOdyssey/webTool.py

+103-24
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,41 @@
2525
import threading
2626
import hashlib
2727
import os
28+
import zipfile
2829

2930
gExtiverseDeck = {}
3031

32+
def releaseDir(dir_path):
33+
if not os.path.exists(dir_path):
34+
os.makedirs(dir_path)
35+
36+
def releaseToDirForFile(dir_path):
37+
tmp_path_list = dir_path.rstrip('/').split('/')
38+
if len(tmp_path_list) > 0:
39+
tmp_path_list = tmp_path_list[:-1]
40+
for tmp_path_list_index in range(len(tmp_path_list)):
41+
if tmp_path_list[tmp_path_list_index] != '':
42+
releaseDir('/'.join(tmp_path_list[:tmp_path_list_index + 1]))
43+
44+
def GETHttpFile(url, path):
45+
res = False
46+
send_url = url
47+
headers = {
48+
'User-Agent': 'OlivaDiceOdyssey/%s' % OlivaDiceOdyssey.data.OlivaDiceOdyssey_ver_short
49+
}
50+
try:
51+
msg_res = req.request("GET", send_url, headers = headers, proxies = OlivaDiceCore.webTool.get_system_proxy())
52+
releaseToDirForFile(path)
53+
with open(path, 'wb+') as tmp:
54+
tmp.write(msg_res.content)
55+
if msg_res.status_code in [200, 300]:
56+
res = True
57+
else:
58+
res = False
59+
except:
60+
res = False
61+
return res
62+
3163
def getExtiverseDeckRemote():
3264
global gExtiverseDeck
3365
res = None
@@ -51,36 +83,83 @@ def downloadExtiverseDeckRemote(name, botHash = 'unity'):
5183
res = False
5284
flag_hit = False
5385
res_text = None
86+
res_resource_list = None
5487
deck_type = 'deckclassic'
55-
if type(gExtiverseDeck) is dict \
56-
and 'classic' in gExtiverseDeck \
57-
and type(gExtiverseDeck['classic']) is list:
58-
for item in gExtiverseDeck['classic']:
59-
if type(item) is dict \
60-
and 'name' in item \
61-
and 'download_link' in item \
62-
and type(item['download_link']) is list \
63-
and item['name'] == name:
64-
for send_url in item['download_link']:
65-
headers = {
66-
'User-Agent': OlivaDiceCore.data.bot_version_short_header
67-
}
68-
try:
69-
msg_res = req.request("GET", send_url, headers = headers, proxies = OlivaDiceCore.webTool.get_system_proxy())
70-
res_text = str(msg_res.text)
71-
flag_hit = True
72-
except:
73-
pass
74-
if flag_hit:
75-
res = True
76-
deck_type = 'deckclassic'
77-
break
88+
for deck_meta_type in ['classic', 'yaml', 'excel']:
89+
if type(gExtiverseDeck) is dict \
90+
and deck_meta_type in gExtiverseDeck \
91+
and type(gExtiverseDeck[deck_meta_type]) is list:
92+
for item in gExtiverseDeck[deck_meta_type]:
93+
if type(item) is dict \
94+
and 'name' in item \
95+
and 'download_link' in item \
96+
and type(item['download_link']) is list \
97+
and item['name'] == name:
98+
if 'resource_link' in item:
99+
res_resource_list = item['resource_link']
100+
for send_url in item['download_link']:
101+
headers = {
102+
'User-Agent': OlivaDiceCore.data.bot_version_short_header
103+
}
104+
try:
105+
msg_res = req.request("GET", send_url, headers = headers, proxies = OlivaDiceCore.webTool.get_system_proxy())
106+
res_text = str(msg_res.text)
107+
flag_hit = True
108+
except:
109+
pass
110+
if flag_hit:
111+
res = True
112+
deck_type = {
113+
'classic': 'deckclassic',
114+
'yaml': 'deckyaml',
115+
'excel': 'deckexcel'
116+
}[deck_meta_type]
117+
break
78118
# 这里要写入文件
79119
if flag_hit:
80-
with open(os.path.join('plugin', 'data', 'OlivaDice', botHash, 'extend', deck_type, name + '.json'), 'w', encoding = 'utf-8') as f:
120+
checkDict = {
121+
'deckclassic': '.json',
122+
'deckyaml': '.yaml',
123+
'deckexcel': '.xlsx'
124+
}
125+
dfix = checkDict[deck_type]
126+
with open(os.path.join('plugin', 'data', 'OlivaDice', botHash, 'extend', deck_type, name + dfix), 'w', encoding = 'utf-8') as f:
81127
f.write(res_text)
128+
# 这里下载并解压资源文件
129+
if res_resource_list is not None \
130+
and type(res_resource_list) is list:
131+
for resource_url_this in res_resource_list:
132+
if type(resource_url_this) is str:
133+
GETHttpFile(resource_url_this, 'plugin/data/OlivaDice/unity/update/tmp_deck_resource.zip')
134+
with support_gbk(
135+
zipfile.ZipFile(
136+
'plugin/data/OlivaDice/unity/update/tmp_deck_resource.zip',
137+
'r',
138+
zipfile.ZIP_DEFLATED
139+
)
140+
) as resourceFile:
141+
resourceFile_list = resourceFile.namelist()
142+
for resourceFile_list_this in resourceFile_list:
143+
try:
144+
resourceFile.extract(resourceFile_list_this, 'data')
145+
except:
146+
pass
82147
return res
83148

149+
def support_gbk(zip_file: zipfile.ZipFile):
150+
name_to_info = zip_file.NameToInfo
151+
# copy map first
152+
for name, info in name_to_info.copy().items():
153+
try:
154+
real_name = name.encode('cp437').decode('gbk')
155+
except:
156+
real_name = name
157+
if real_name != name:
158+
info.filename = real_name
159+
del name_to_info[name]
160+
name_to_info[real_name] = info
161+
return zip_file
162+
84163
def getCnmodsReq(title = None, page = None):
85164
res = None
86165
tmp_res = None

0 commit comments

Comments
 (0)