Skip to content

Commit b298afc

Browse files
committed
Upload file: collecting_hero.py
1 parent 54f33ce commit b298afc

File tree

1 file changed

+130
-130
lines changed

1 file changed

+130
-130
lines changed

crawling_data/collecting_hero.py

Lines changed: 130 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
1-
# -*- coding:utf-8 -*-
2-
# @Project :LPL
3-
# @FileName :collecting_hero.py
4-
# @Time :2025/3/5 00:12
5-
# @Software :PyCharm
6-
# @Author :Viper373
7-
# @Index :https://viper3.top
8-
# @Blog :https://blog.viper3.top
9-
10-
import os
11-
import sys
12-
import requests
13-
import json
14-
import logging # 日志
15-
from rich.logging import RichHandler
16-
17-
# 获取当前脚本的目录
18-
script_dir = os.path.dirname(os.path.abspath(__file__))
19-
# 获取项目的根目录(假设你的项目根目录在上两级)
20-
project_root = os.path.abspath(os.path.join(script_dir, os.pardir))
21-
if project_root not in sys.path:
22-
sys.path.append(project_root)
23-
24-
# rich日志处理器配置
25-
logging.basicConfig(
26-
level="INFO",
27-
format="%(asctime)s - [%(levelname)s] - %(message)s",
28-
datefmt="[%Y-%m-%d %H:%M:%S]",
29-
handlers=[
30-
# 日志控制台处理器
31-
RichHandler(rich_tracebacks=True),
32-
# 日志文件处理器
33-
logging.FileHandler(os.path.join(project_root, 'logs', 'lol.logs'), mode='a', encoding='utf-8'),
34-
],
35-
)
36-
37-
log = logging.getLogger("rich") # 日志对象
38-
39-
40-
class HeroData:
41-
42-
def __init__(self):
43-
self.hero_list_url = "https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js"
44-
self.logo_url_template = "https://game.gtimg.cn/images/lol/act/img/champion/{alias}.png" # 官方Logo URL模板
45-
46-
def get_hero_list(self):
47-
"""
48-
获取英雄列表并保存为格式化的JSON文件
49-
:return: None
50-
"""
51-
try:
52-
response = requests.get(self.hero_list_url)
53-
54-
if response.status_code != 200:
55-
log.error(f"请求失败,状态码: {response.status_code}")
56-
return
57-
58-
try:
59-
# 直接解析 JSON 数据(自动处理 Unicode 转义)
60-
hero_data = response.json()
61-
except json.JSONDecodeError:
62-
log.error("响应内容不是有效的 JSON 格式")
63-
return
64-
65-
# 保存格式化的 JSON 文件
66-
save_path = os.path.join(project_root, 'data', 'json', 'hero_list.json')
67-
with open(save_path, 'w', encoding='utf-8') as f:
68-
json.dump(hero_data, f, ensure_ascii=False, indent=4)
69-
log.info(f"{hero_data.get('fileName')}保存完成 丨 version:{hero_data.get('version')}{hero_data.get('fileTime')}")
70-
except Exception as _error:
71-
log.error(f"获取英雄列表失败: {_error}")
72-
73-
def generate_logo_data(self):
74-
"""
75-
根据hero_list.json生成logo数据
76-
:return: None
77-
"""
78-
hero_list_json = os.path.join(project_root, 'data', 'json', 'hero_list.json')
79-
save_path = os.path.join(project_root, 'data', 'json', 'hero_logo.json')
80-
try:
81-
# 读取英雄列表
82-
with open(hero_list_json, 'r', encoding='utf-8') as f:
83-
hero_list = json.load(f)
84-
85-
# 生成Logo数据
86-
logo_data = []
87-
for hero in hero_list.get('hero'):
88-
try:
89-
# 关键字段校验
90-
hero_id = str(hero['heroId'])
91-
name = hero['name']
92-
alias = hero['alias']
93-
94-
# 生成官方Logo URL
95-
logo_url = self.logo_url_template.format(alias=alias)
96-
97-
logo_data.append({
98-
"heroId": hero_id,
99-
"name": name,
100-
"heroLogo": logo_url
101-
})
102-
except KeyError as e:
103-
log.warning(f"字段缺失: {e} | 英雄数据: {hero}")
104-
except Exception as e:
105-
log.error(f"处理英雄异常: {str(e)} | 数据: {hero}")
106-
107-
# 保存Logo数据
108-
with open(save_path, 'w', encoding='utf-8') as f:
109-
json.dump(logo_data, f, ensure_ascii=False, indent=4)
110-
111-
log.info(f"成功生成 {len(logo_data)} 条Logo数据")
112-
return True
113-
114-
except FileNotFoundError:
115-
log.error("hero_list.json文件不存在")
116-
except json.JSONDecodeError:
117-
log.error("hero_list.json文件格式错误")
118-
except Exception as e:
119-
log.error(f"生成Logo数据失败: {str(e)}")
120-
return False
121-
122-
123-
def main():
124-
hero_data = HeroData()
125-
# hero_data.get_hero_list()
126-
hero_data.generate_logo_data()
127-
128-
129-
if __name__ == '__main__':
130-
main()
1+
# -*- coding:utf-8 -*-
2+
# @Project :LPL
3+
# @FileName :collecting_hero.py
4+
# @Time :2025/3/5 00:12
5+
# @Software :PyCharm
6+
# @Author :Viper373
7+
# @Index :https://viper3.top
8+
# @Blog :https://blog.viper3.top
9+
10+
import os
11+
import sys
12+
import requests
13+
import json
14+
import logging # 日志
15+
from rich.logging import RichHandler
16+
17+
# 获取当前脚本的目录
18+
script_dir = os.path.dirname(os.path.abspath(__file__))
19+
# 获取项目的根目录(假设你的项目根目录在上两级)
20+
project_root = os.path.abspath(os.path.join(script_dir, os.pardir))
21+
if project_root not in sys.path:
22+
sys.path.append(project_root)
23+
24+
# rich日志处理器配置
25+
logging.basicConfig(
26+
level="INFO",
27+
format="%(asctime)s - [%(levelname)s] - %(message)s",
28+
datefmt="[%Y-%m-%d %H:%M:%S]",
29+
handlers=[
30+
# 日志控制台处理器
31+
RichHandler(rich_tracebacks=True),
32+
# 日志文件处理器
33+
logging.FileHandler(os.path.join(project_root, 'logs', 'lol.logs'), mode='a', encoding='utf-8'),
34+
],
35+
)
36+
37+
log = logging.getLogger("rich") # 日志对象
38+
39+
40+
class HeroData:
41+
42+
def __init__(self):
43+
self.hero_list_url = "https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js"
44+
self.logo_url_template = "https://game.gtimg.cn/images/lol/act/img/champion/{alias}.png" # 官方Logo URL模板
45+
46+
def get_hero_list(self):
47+
"""
48+
获取英雄列表并保存为格式化的JSON文件
49+
:return: None
50+
"""
51+
try:
52+
response = requests.get(self.hero_list_url)
53+
54+
if response.status_code != 200:
55+
log.error(f"请求失败,状态码: {response.status_code}")
56+
return
57+
58+
try:
59+
# 直接解析 JSON 数据(自动处理 Unicode 转义)
60+
hero_data = response.json()
61+
except json.JSONDecodeError:
62+
log.error("响应内容不是有效的 JSON 格式")
63+
return
64+
65+
# 保存格式化的 JSON 文件
66+
save_path = os.path.join(project_root, 'data', 'json', 'hero_list.json')
67+
with open(save_path, 'w', encoding='utf-8') as f:
68+
json.dump(hero_data, f, ensure_ascii=False, indent=4)
69+
log.info(f"{hero_data.get('fileName')}保存完成 丨 version:{hero_data.get('version')}{hero_data.get('fileTime')}")
70+
except Exception as _error:
71+
log.error(f"获取英雄列表失败: {_error}")
72+
73+
def generate_logo_data(self):
74+
"""
75+
根据hero_list.json生成logo数据
76+
:return: None
77+
"""
78+
hero_list_json = os.path.join(project_root, 'data', 'json', 'hero_list.json')
79+
save_path = os.path.join(project_root, 'data', 'json', 'hero_logo.json')
80+
try:
81+
# 读取英雄列表
82+
with open(hero_list_json, 'r', encoding='utf-8') as f:
83+
hero_list = json.load(f)
84+
85+
# 生成Logo数据
86+
logo_data = []
87+
for hero in hero_list.get('hero'):
88+
try:
89+
# 关键字段校验
90+
hero_id = str(hero['heroId'])
91+
name = hero['name']
92+
alias = hero['alias']
93+
94+
# 生成官方Logo URL
95+
logo_url = self.logo_url_template.format(alias=alias)
96+
97+
logo_data.append({
98+
"heroId": hero_id,
99+
"name": name,
100+
"heroLogo": logo_url
101+
})
102+
except KeyError as e:
103+
log.warning(f"字段缺失: {e} | 英雄数据: {hero}")
104+
except Exception as e:
105+
log.error(f"处理英雄异常: {str(e)} | 数据: {hero}")
106+
107+
# 保存Logo数据
108+
with open(save_path, 'w', encoding='utf-8') as f:
109+
json.dump(logo_data, f, ensure_ascii=False, indent=4)
110+
111+
log.info(f"成功生成 {len(logo_data)} 条Logo数据")
112+
return True
113+
114+
except FileNotFoundError:
115+
log.error("hero_list.json文件不存在")
116+
except json.JSONDecodeError:
117+
log.error("hero_list.json文件格式错误")
118+
except Exception as e:
119+
log.error(f"生成Logo数据失败: {str(e)}")
120+
return False
121+
122+
123+
def main():
124+
hero_data = HeroData()
125+
# hero_data.get_hero_list()
126+
hero_data.generate_logo_data()
127+
128+
129+
if __name__ == '__main__':
130+
main()

0 commit comments

Comments
 (0)