🐛 Fix post code regex

This commit is contained in:
xtaodada 2024-03-16 18:45:57 +08:00
parent 8c8fa8ea99
commit 62891670c0
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 12 additions and 5 deletions

View File

@ -174,11 +174,14 @@ class Hyperion:
} }
codes = [] codes = []
response = await self.client.get(url=self.LIVE_CODE_HOYO_URL, headers=headers, params=params) response = await self.client.get(url=self.LIVE_CODE_HOYO_URL, headers=headers, params=params)
guess_offline_at = LiveCodeHoYo.guess_offline_at()
for module in response.get("modules", []): for module in response.get("modules", []):
if exchange_group := module.get("exchange_group"): if exchange_group := module.get("exchange_group"):
for code_data in exchange_group.get("bonuses", []): for code_data in exchange_group.get("bonuses", []):
codes.append(LiveCodeHoYo(**code_data)) codes.append(LiveCodeHoYo(**code_data))
break break
for _ in range(len(codes), 3):
codes.append(LiveCodeHoYo(exchange_code="", offline_at=guess_offline_at))
return codes return codes
async def close(self): async def close(self):

View File

@ -1,4 +1,4 @@
from datetime import datetime from datetime import datetime, timedelta
from io import BytesIO from io import BytesIO
from typing import Any, List, Optional from typing import Any, List, Optional
@ -121,3 +121,7 @@ class LiveCodeHoYo(BaseModel):
@property @property
def text(self) -> str: def text(self) -> str:
return self.exchange_code if self.exchange_code else "XXXXXXXXXXXX" return self.exchange_code if self.exchange_code else "XXXXXXXXXXXX"
@staticmethod
def guess_offline_at() -> datetime:
return datetime.now().replace(hour=12, minute=0, second=0, microsecond=0) + timedelta(days=1)

View File

@ -141,9 +141,9 @@ class PostCode(Plugin.Conversation):
continue continue
if not (subject := post.get("subject")): if not (subject := post.get("subject")):
continue continue
if not (match := self.SUBJECT_RE.match(subject)): if not (match := self.SUBJECT_RE.findall(subject)):
continue continue
return match.group(1), post return match[0], post
return None, None return None, None
def init_act_id(self, post: Dict) -> Optional[str]: def init_act_id(self, post: Dict) -> Optional[str]:
@ -179,7 +179,7 @@ class PostCode(Plugin.Conversation):
post_code_handler_data.ver_code = ver_code post_code_handler_data.ver_code = ver_code
post_code_handler_data.mys_code = await client.get_live_code(act_id, ver_code) post_code_handler_data.mys_code = await client.get_live_code(act_id, ver_code)
post_code_handler_data.hoyo_code = await client.get_live_code_hoyo(self.gids) post_code_handler_data.hoyo_code = await client.get_live_code_hoyo(self.gids)
if len(post_code_handler_data.mys_code) != 3 or len(post_code_handler_data.hoyo_code) != 3: if len(post_code_handler_data.mys_code) != 3:
raise ValueError("获取兑换码数据成功,但是数量不对") raise ValueError("获取兑换码数据成功,但是数量不对")
return True return True
finally: finally:
@ -312,7 +312,7 @@ class PostCode(Plugin.Conversation):
try: try:
mys_code = await client.get_live_code(act_id, ver_code) mys_code = await client.get_live_code(act_id, ver_code)
hoyo_code = await client.get_live_code_hoyo(self.gids) hoyo_code = await client.get_live_code_hoyo(self.gids)
if len(post_code_handler_data.mys_code) != 3 or len(post_code_handler_data.hoyo_code) != 3: if len(post_code_handler_data.mys_code) != 3:
raise ValueError("获取兑换码数据成功,但是数量不对") raise ValueError("获取兑换码数据成功,但是数量不对")
if post_code_handler_data.have_changes(mys_code, hoyo_code): if post_code_handler_data.have_changes(mys_code, hoyo_code):
post_code_handler_data.mys_code = mys_code post_code_handler_data.mys_code = mys_code