-
Notifications
You must be signed in to change notification settings - Fork 0
/
skins.py
177 lines (130 loc) · 6.07 KB
/
skins.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
# -*- coding: utf-8 -*-
# BY: TONG
"""
The script is used to download lol skins from
https://lol.qq.com/data/info-heros.shtml,
https://101.qq.com/#/hero.
"""
import re
import os
import json
import requests
class Logger(object):
def __init__(self, log_file):
if os.path.isfile(log_file):
os.remove(log_file)
self.log_f = open(log_file, 'a', encoding='utf-8')
def log(self, msg):
print(msg)
self.log_f.write(str(msg)+"\n")
self.log_f.flush()
def close(self):
self.log_f.close()
class LoLSkins(object):
def __init__(self, ROOT_DIR='lolhero'):
self.n_heroes = 0
self.n_skins = 0
self.n_notfound = 0
self.notfound_urls = []
self.dirs = {
'js': os.path.join(ROOT_DIR, 'js'),
'hero': {
'audio': os.path.join(ROOT_DIR, 'hero'),
'main': os.path.join(ROOT_DIR, 'hero'),
'loading': os.path.join(ROOT_DIR, 'hero'),
# 'icon': os.path.join(ROOT_DIR, 'hero'),
# 'video': os.path.join(ROOT_DIR, 'hero'),
# 'source': os.path.join(ROOT_DIR, 'hero')
}
}
# javascript url
self.heroes_url = r'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
self.hero_url = r'https://game.gtimg.cn/images/lol/act/img/js/hero/{:s}.js'
# resources
self.audio_url = r'https://game.gtimg.cn/images/lol/act/img/vo/{:s}/{:s}.ogg'
self.main_url = r'https://game.gtimg.cn/images/lol/act/img/skin/big_{:s}.jpg'
self.loading_url = r'https://game.gtimg.cn/images/lol/act/img/skinloading/{:s}.jpg'
self.icon_url = r'https://game.gtimg.cn/images/lol/act/img/skin/small_{:s}.jpg'
self.video_url = r'https://game.gtimg.cn/images/lol/act/img/skinvideo/sp_{:s}.jpg'
self.source_url = r'https://game.gtimg.cn/images/lol/act/img/guidetop/guide_{:s}.jpg'
if not os.path.isdir(self.dirs['js']):
os.makedirs(self.dirs['js'])
self.log_file = os.path.join(ROOT_DIR, 'log.txt')
self.logger = Logger(self.log_file)
def _download(self, url, filename=''):
if filename == '':
filename = url.split('/')[-1]
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'}
r = requests.get(url=url, headers=headers)
if r.status_code == 200:
with open(filename, 'wb') as f:
f.write(r.content)
return 1
elif r.status_code == 404:
self.n_notfound += 1
self.notfound_urls.append({os.path.basename(filename):url})
else:
r.raise_for_status()
return 0
def parse_heroes(self):
js_filename = os.path.join(self.dirs['js'], self.heroes_url.split('/')[-1])
self._download(self.heroes_url, js_filename)
with open(js_filename, 'r') as f:
heroes_data = json.load(f)
version = heroes_data['version']
filetime = heroes_data['fileTime']
heroes_list = heroes_data['hero']
self.logger.log("Version {:s}".format(version))
self.logger.log("{:d} heroes detected".format(len(heroes_list)))
self.logger.log("Filetime {:s}".format(filetime))
for i, h in enumerate(heroes_list):
self.n_heroes += 1
self.download_hero(i, h)
self.logger.log("\n{:d} skins for {:d} heroes downloaded.".format(self.n_skins, self.n_heroes))
if self.n_notfound:
self.logger.log("{:d} files not found.".format(self.n_notfound))
self.logger.log(self.notfound_urls)
self.logger.close()
def download_hero(self, i, hero):
js_filename = os.path.join(self.dirs['js'], '{:s}_{:s}.js'.format(hero['heroId'], hero['alias']))
self._download(self.hero_url.format(hero['heroId']), js_filename)
with open(js_filename, 'r') as f:
hero_data = json.load(f)
version = hero_data['version']
filetime = hero_data['fileTime']
info = hero_data['hero']
skins = hero_data['skins']
hero_id = info['heroId']
name = info['name']
alias = info['alias']
title = info['title']
self.logger.log("[{:d}] hero_id: {:s}, name: {:s}, title: {:s}, alias: {:s}".format(i+1, hero_id, name, title, alias))
# make dirs
dirs = {}
for d in self.dirs['hero']:
dirs[d] = os.path.join(self.dirs['hero'][d], name+' '+title, d)
if not os.path.isdir(dirs[d]):
os.makedirs(dirs[d])
n_skins = 0
for j in range(len(skins)):
if skins[j]['chromas'] == '1':
continue
skin_id = skins[j]['skinId']
skin_name = skins[j]['name']
self.logger.log(" skin_id: {:s}, skin_name: {:s}".format(skin_id, skin_name))
# for filename rules on e.g., Windows
skin_name = re.sub(r'[\\/:*?"<>|]+', '', skin_name)
status_code = self._download(skins[j]['mainImg'], os.path.join(dirs['main'], skin_name+'.'+skins[j]['mainImg'].split('.')[-1]))
if status_code:
n_skins += 1
self._download(skins[j]['loadingImg'], os.path.join(dirs['loading'], skin_name+'.'+skins[j]['loadingImg'].split('.')[-1]))
# self._download(skins[j]['sourceImg'], os.path.join(dirs['source'], skin_name+'.'+skins[j]['sourceImg'].split('.')[-1]))
self.n_skins += n_skins
self.logger.log(" {:d} skins downloaded".format(n_skins))
self._download(info['selectAudio'], os.path.join(dirs['audio'], 'pick_'+info['selectAudio'].split('/')[-1]))
self._download(info['banAudio'], os.path.join(dirs['audio'], 'ban_'+info['selectAudio'].split('/')[-1]))
self.logger.log(" pick/ban audio downloaded")
if __name__ == '__main__':
ROOT_DIR = 'lolhero'
parser = LoLSkins(ROOT_DIR)
parser.parse_heroes()