-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
217 lines (166 loc) · 7.93 KB
/
utils.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import json
import requests
from io import BytesIO
from PIL import Image
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
def link_genshin_art(name):
return f'https://enka.network/ui/UI_Gacha_AvatarImg_{name}.png'
def link_genshin_icon(name):
return f'https://enka.network/ui/UI_AvatarIcon_{name}.png'
def link_starrail_art(id):
return f'https://enka.network/ui/hsr/SpriteOutput/AvatarDrawCard/{id}.png'
def link_starrail_icon(id):
return f'https://raw.githubusercontent.com/FortOfFans/HSR/main/spriteoutput/avatarshopicon/{id}.png'
# -------------------------------------------------------------------------------------------------
# Version Check
# -------------------------------------------------------------------------------------------------
def version_check():
return_data = []
url = 'https://sg-hyp-api.hoyoverse.com/hyp/hyp-connect/api/getGamePackages?launcher_id=VYTpXlbWo8'
response = requests.get(url)
data = response.json().get('data').get('game_packages')
for games in data:
game = games.get('game')
game_name = game.get('biz')
main = games.get('main').get('major')
game_version = main.get('version')
if game_name != 'hk4e_global' and game_name != 'hkrpg_global':
continue
else:
return_data.append({ game_name: game_version })
return return_data
def update():
latest_versions = version_check()
try:
with open('version.json', 'r') as file:
current_versions = json.load(file)
except FileNotFoundError:
current_versions = {}
except json.JSONDecodeError:
print("Error decoding JSON file.")
current_versions = {}
latest_versions_dict = {list(version.keys())[0]: list(version.values())[0] for version in latest_versions}
for game, new_version in latest_versions_dict.items():
current_version = current_versions.get(game)
if current_version != new_version:
if game == 'hk4e_global':
update_genshin()
elif game == 'hkrpg_global':
update_starrail()
current_versions[game] = new_version
with open('version.json', 'w') as file:
json.dump(current_versions, file, indent=4)
# -------------------------------------------------------------------------------------------------
# GENSHIN IMPACT
# -------------------------------------------------------------------------------------------------
def update_genshin():
with open('gi_characters.json', 'r') as file:
data = json.load(file)
progress_bar = tqdm(total=len(data), desc="Updating Genshin Impact characters", unit="character")
current_dir = os.path.dirname(os.path.abspath(__file__))
def process_character(id_str, name):
genshin_icon_art(current_dir, name, id_str)
genshin_splash_art(current_dir, name, id_str)
genshin_wish_art(current_dir, name, id_str)
return id_str
with ThreadPoolExecutor(max_workers=None) as executor:
futures = [executor.submit(process_character, id_str, name) for id_str, name in data.items()]
for future in as_completed(futures):
future.result()
progress_bar.update(1)
progress_bar.close()
print(f"Finished updating {len(data)} Genshin Impact characters")
def genshin_icon_art(current_dir, character, id):
try:
response = requests.get(link_genshin_icon(character))
character_icon = Image.open(BytesIO(response.content))
text_files_dir = os.path.join(current_dir, "genshin", "icon")
os.makedirs(text_files_dir, exist_ok=True)
output_path = os.path.join(text_files_dir, f'{id}.png')
character_icon.save(output_path)
except Exception as e:
print(f"Failed to create icon art for {character}: {str(e)}")
def genshin_splash_art(current_dir, character, id):
try:
response = requests.get(link_genshin_art(character))
character_art = Image.open(BytesIO(response.content))
text_files_dir = os.path.join(current_dir, "genshin", "splash")
os.makedirs(text_files_dir, exist_ok=True)
output_path = os.path.join(text_files_dir, f'{id}.png')
character_art.save(output_path)
except Exception as e:
print(f"Failed to create splash art for {character}: {str(e)}")
def genshin_wish_art(current_dir, character, id):
try:
response = requests.get(link_genshin_art(character))
character_image = Image.open(BytesIO(response.content))
new_width = character_image.height * 0.3125 # 5:16 ratio
left = (character_image.width - new_width) // 2
top = 0
right = left + new_width
bottom = character_image.height
cropped_image = character_image.crop((left, top, right, bottom))
text_files_dir = os.path.join(current_dir, "genshin", "wish")
os.makedirs(text_files_dir, exist_ok=True)
output_path = os.path.join(text_files_dir, f'{id}.png')
cropped_image.save(output_path)
except Exception as e:
print(f"Failed to create wish art for {character}: {str(e)}")
# -------------------------------------------------------------------------------------------------
# HONKAI: STAR RAIL
# -------------------------------------------------------------------------------------------------
def update_starrail():
with open('hsr_characters.json', 'r') as file:
data = json.load(file)
progress_bar = tqdm(total=len(data), desc="Updating Star Rail characters", unit="character")
current_dir = os.path.dirname(os.path.abspath(__file__))
def process_character(id_str, name):
starrail_icon_art(current_dir, name, id_str)
starrail_splash_art(current_dir, name, id_str)
starrail_wish_art(current_dir, name, id_str)
return id_str
with ThreadPoolExecutor(max_workers=None) as executor:
futures = [executor.submit(process_character, id_str, name) for id_str, name in data.items()]
for future in as_completed(futures):
future.result()
progress_bar.update(1)
progress_bar.close()
print(f"Finished updating {len(data)} Star Rail characters")
def starrail_icon_art(current_dir, character, id):
try:
response = requests.get(link_starrail_icon(id))
character_icon = Image.open(BytesIO(response.content))
text_files_dir = os.path.join(current_dir, "starrail", "icon")
os.makedirs(text_files_dir, exist_ok=True)
output_path = os.path.join(text_files_dir, f'{id}.png')
character_icon.save(output_path)
except Exception as e:
print(f"Failed to create icon art for {character}: {str(e)}")
def starrail_splash_art(current_dir, character, id):
try:
response = requests.get(link_starrail_art(id))
character_art = Image.open(BytesIO(response.content))
text_files_dir = os.path.join(current_dir, "starrail", "splash")
os.makedirs(text_files_dir, exist_ok=True)
output_path = os.path.join(text_files_dir, f'{id}.png')
character_art.save(output_path)
except Exception as e:
print(f"Failed to create splash art for {character}: {str(e)}")
def starrail_wish_art(current_dir, character, id):
try:
response = requests.get(link_starrail_art(id))
character_image = Image.open(BytesIO(response.content))
new_width = character_image.height * 0.3125 # 5:16 ratio
left = (character_image.width - new_width) // 2
top = 0
right = left + new_width
bottom = character_image.height
cropped_image = character_image.crop((left, top, right, bottom))
text_files_dir = os.path.join(current_dir, "starrail", "wish")
os.makedirs(text_files_dir, exist_ok=True)
output_path = os.path.join(text_files_dir, f'{id}.png')
cropped_image.save(output_path)
except Exception as e:
print(f"Failed to create wish art for {character}: {str(e)}")