add lock
This commit is contained in:
parent
a2c1797d6d
commit
fc253c8422
@ -36,29 +36,50 @@ class Player:
|
||||
data = json.load(fp)
|
||||
self.name = data.get("playerInfo", {}).get("nickname", "")
|
||||
|
||||
@staticmethod
|
||||
def can_refresh():
|
||||
return sqlite.get("can_refresh", True)
|
||||
|
||||
@staticmethod
|
||||
def lock():
|
||||
sqlite["can_refresh"] = False
|
||||
|
||||
@staticmethod
|
||||
def unlock():
|
||||
sqlite["can_refresh"] = True
|
||||
|
||||
async def update_char(self):
|
||||
all_char_ = await getRawData(self.uid)
|
||||
all_char_ = all_char_.get("list", [])
|
||||
for i in all_char_:
|
||||
for f in self.all_char:
|
||||
if f["name"] == i:
|
||||
self.all_char.remove(f)
|
||||
break
|
||||
try:
|
||||
all_char = await getPanelMsg(self.uid, i)
|
||||
img = all_char.get("pic", None)
|
||||
error = all_char.get("error", None)
|
||||
if img:
|
||||
msg = await app.send_photo(channel_id, img)
|
||||
self.all_char.append(gen_char_dict(i, msg.photo.file_id))
|
||||
elif error:
|
||||
return error
|
||||
else:
|
||||
print(all_char)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
continue
|
||||
return f"成功缓存了 UID{self.uid} 的{'、'.join(all_char_)}等 {len(all_char_)} 位角色数据!"
|
||||
if not self.can_refresh():
|
||||
return "有刷新任务正在运行,请稍等一会儿再试"
|
||||
self.lock()
|
||||
try:
|
||||
all_char_ = await getRawData(self.uid)
|
||||
all_char_ = all_char_.get("list", [])
|
||||
for i in all_char_:
|
||||
for f in self.all_char:
|
||||
if f["name"] == i:
|
||||
self.all_char.remove(f)
|
||||
break
|
||||
try:
|
||||
all_char = await getPanelMsg(self.uid, i)
|
||||
img = all_char.get("pic", None)
|
||||
error = all_char.get("error", None)
|
||||
if img:
|
||||
msg = await app.send_photo(channel_id, img)
|
||||
self.all_char.append(gen_char_dict(i, msg.photo.file_id))
|
||||
elif error:
|
||||
return error
|
||||
else:
|
||||
print(all_char)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
continue
|
||||
return f"成功缓存了 UID{self.uid} 的{'、'.join(all_char_)}等 {len(all_char_)} 位角色数据!"
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return "数据刷新失败,请重试"
|
||||
finally:
|
||||
self.unlock()
|
||||
|
||||
def export(self):
|
||||
return {"name": self.name, "uid": self.uid, "time": int(time.time()), "all_char": self.all_char}
|
||||
|
Loading…
Reference in New Issue
Block a user