From b275cc7b9648e58f8f754a1632f6ec112545f839 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 29 Aug 2023 10:47:44 +0800 Subject: [PATCH] fix: bbs wiki --- func/fetch_monsters.py | 76 ------------------------------------------ main.py | 13 +------- 2 files changed, 1 insertion(+), 88 deletions(-) delete mode 100644 func/fetch_monsters.py diff --git a/func/fetch_monsters.py b/func/fetch_monsters.py deleted file mode 100644 index b74e3c9..0000000 --- a/func/fetch_monsters.py +++ /dev/null @@ -1,76 +0,0 @@ -import asyncio -import re -from pathlib import Path -from typing import List - -import aiofiles -import ujson -from bs4 import BeautifulSoup - -from func.client import client -from func.url import info_url -from models.enums import MonsterType, Area -from models.monster import Monster -from models.wiki import Children - -all_monsters: List[Monster] = [] - - -async def fetch_monsters(data: Children): - for content in data.list: - m_type = MonsterType(re.findall(r'类型/(.*?)\\', content.ext)[0]) - try: - m_area = Area(re.findall(r'区域/(.*?)\\', content.ext)[0]) - except IndexError: - m_area = Area.NULL - monster = Monster( - id=content.content_id, - name=content.title, - desc=content.summary, - icon=content.icon, - big_pic="", - type=m_type, - area=m_area, - resistance="", - find_area="", - ) - all_monsters.append(monster) - - -async def fetch_info(monster: Monster): - print(f"Fetch monster info: {monster.id}: {monster.name}") - params = { - 'app_sn': 'sr_wiki', - 'content_id': str(monster.id), - } - resp = await client.get(info_url, params=params) - data = resp.json()["data"]["content"]["contents"][0]["text"] - soup = BeautifulSoup(data, "lxml") - table = soup.find("table", {"class": "obc-tml-light-table--pc"}) - trs = table.find_all("tr") - monster.big_pic = trs[0].find("img").get("src") - monster.resistance = trs[2].find("p").get_text().strip() - monster.desc = trs[4].find_all("td")[-1].get_text().strip() - monster.find_area = trs[5].find_all("td")[-1].get_text().strip() - - -async def fetch_monsters_infos(): - tasks = [] - for monster in all_monsters: - tasks.append(fetch_info(monster)) - await asyncio.gather(*tasks) - - -async def dump_monsters(path: Path): - data = [monster.dict() for monster in all_monsters] - data.sort(key=lambda x: x["id"]) - async with aiofiles.open(path, "w", encoding="utf-8") as f: - await f.write(ujson.dumps(data, indent=4, ensure_ascii=False)) - - -async def read_monsters(path: Path): - async with aiofiles.open(path, "r", encoding="utf-8") as f: - data = ujson.loads(await f.read()) - for monster in data: - m = Monster(**monster) - all_monsters.append(m) diff --git a/main.py b/main.py index ec96afb..371fa8a 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,6 @@ from func.fetch_all import get_list from func.fetch_avatars import fetch_avatars, dump_avatars, read_avatars from func.fetch_light_cones import fetch_light_cones, fetch_light_cones_infos, dump_light_cones, read_light_cones from func.fetch_materials import fetch_materials, fetch_materials_infos, dump_materials, read_materials -from func.fetch_monsters import fetch_monsters, fetch_monsters_infos, dump_monsters, read_monsters from func.fetch_relics import fetch_relics, fetch_relics_infos, dump_relics, read_relics from func.fetch_src import move_files @@ -16,7 +15,6 @@ async def wiki( override_materials: bool = True, override_avatars: bool = True, override_light_cones: bool = True, - override_monsters: bool = True, override_relics: bool = True, ): main_data = await get_list() @@ -41,12 +39,6 @@ async def wiki( await dump_light_cones(data_path / "light_cones.json") else: await read_light_cones(data_path / "light_cones.json") - if override_monsters: - await fetch_monsters(main_data[3]) - await fetch_monsters_infos() - await dump_monsters(data_path / "monsters.json") - else: - await read_monsters(data_path / "monsters.json") if override_relics: await fetch_relics(main_data[2]) await fetch_relics_infos() @@ -63,11 +55,10 @@ async def main( override_materials: bool = True, override_avatars: bool = True, override_light_cones: bool = True, - override_monsters: bool = True, override_relics: bool = True, override_bbs_photos: bool = True, ): - await wiki(override_materials, override_avatars, override_light_cones, override_monsters, override_relics) + await wiki(override_materials, override_avatars, override_light_cones, override_relics) if override_bbs_photos: await bbs_photos() @@ -76,14 +67,12 @@ if __name__ == '__main__': override_material_ = True override_avatar_ = True override_light_cone_ = True - override_monster_ = True override_relic_ = True override_bbs_photo_ = True asyncio.run(main( override_material_, override_avatar_, override_light_cone_, - override_monster_, override_relic_, override_bbs_photo_, ))