添加获取角色信息的服务

This commit is contained in:
洛水居室 2022-06-15 20:34:14 +08:00
parent 7df91ff471
commit 18bdcdc5c6
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -3,6 +3,7 @@ import asyncio
import ujson import ujson
from logger import Log from logger import Log
from model.wiki.characters import Characters
from model.wiki.weapons import Weapons from model.wiki.weapons import Weapons
from utils.redisdb import RedisDB from utils.redisdb import RedisDB
@ -17,17 +18,14 @@ class Wiki:
""" """
self.redis_client = redis.client self.redis_client = redis.client
self.weapons = Weapons() self.weapons = Weapons()
self.characters = Characters()
self._characters_list = []
self._characters_name_list = []
self._weapons_name_list = [] self._weapons_name_list = []
self._weapons_list = [] self._weapons_list = []
self.first_run = True self.first_run = True
async def refresh_wiki(self): async def refresh_weapon(self):
"""
用于把Redis的缓存全部加载进Python
:return:
"""
Log.info("正在重新获取Wiki")
Log.info("正在重新获取武器信息")
weapon_url_list = await self.weapons.get_all_weapon_url() weapon_url_list = await self.weapons.get_all_weapon_url()
Log.info(f"一共找到 {len(weapon_url_list)} 把武器信息") Log.info(f"一共找到 {len(weapon_url_list)} 把武器信息")
weapons_list = [] weapons_list = []
@ -41,7 +39,7 @@ class Wiki:
if isinstance(result, dict): if isinstance(result, dict):
weapons_list.append(result) weapons_list.append(result)
task_list.clear() task_list.clear()
if index % 10 == 0: if index % 10 == 0 and index != 0:
Log.info(f"现在已经获取到 {index} 把武器信息") Log.info(f"现在已经获取到 {index} 把武器信息")
result_list = await asyncio.gather(*task_list) result_list = await asyncio.gather(*task_list)
for result in result_list: for result in result_list:
@ -51,6 +49,41 @@ class Wiki:
self._weapons_list = weapons_list self._weapons_list = weapons_list
await self._del_one("weapon") await self._del_one("weapon")
await self._refresh_info_cache("weapon", weapons_list) await self._refresh_info_cache("weapon", weapons_list)
async def refresh_characters(self):
characters_url_list = await self.characters.get_all_characters_url()
Log.info(f"一共找到 {len(characters_url_list)} 个角色信息")
characters_list = []
task_list = []
for index, characters_url in enumerate(characters_url_list):
task_list.append(self.characters.get_characters(characters_url))
if index % 5 == 0:
result_list = await asyncio.gather(*task_list)
for result in result_list:
if isinstance(result, dict):
characters_list.append(result)
task_list.clear()
if index % 10 == 0 and index != 0:
Log.info(f"现在已经获取到 {index} 个角色信息")
result_list = await asyncio.gather(*task_list)
for result in result_list:
if isinstance(result, dict):
characters_list.append(result)
Log.info("写入角色信息到Redis")
self._characters_list = characters_list
await self._del_one("characters")
await self._refresh_info_cache("characters", characters_list)
async def refresh_wiki(self):
"""
用于把Redis的缓存全部加载进Python
:return:
"""
Log.info("正在重新获取Wiki")
Log.info("正在重新获取武器信息")
await self.refresh_weapon()
Log.info("正在重新获取角色信息")
await self.refresh_characters()
Log.info("刷新成功") Log.info("刷新成功")
async def init(self): async def init(self):
@ -63,6 +96,10 @@ class Wiki:
self._weapons_list = ujson.loads(weapon_dict) self._weapons_list = ujson.loads(weapon_dict)
for weapon in self._weapons_list: for weapon in self._weapons_list:
self._weapons_name_list.append(weapon["name"]) self._weapons_name_list.append(weapon["name"])
characters_dict = await self._get_one("characters")
self._characters_list = ujson.loads(characters_dict)
for characters in self._characters_list:
self._characters_name_list.append(characters["name"])
self.first_run = False self.first_run = False
async def get_weapons(self, name: str): async def get_weapons(self, name: str):
@ -82,6 +119,14 @@ class Wiki:
await self.init() await self.init()
return self._weapons_list return self._weapons_list
async def get_characters_list(self) -> list:
await self.init()
return self._characters_list
async def get_characters_name_list(self) -> list:
await self.init()
return self._characters_name_list
""" """
Redis 相关函数 Redis 相关函数
""" """