fix: bbs wiki

This commit is contained in:
xtaodada 2023-08-29 10:47:44 +08:00
parent aebc0ae8a0
commit b275cc7b96
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 1 additions and 88 deletions

View File

@ -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)

13
main.py
View File

@ -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_,
))