This commit is contained in:
hibiki
2021-06-23 15:57:03 +08:00
parent 862776b011
commit f84896213a
17 changed files with 400 additions and 149 deletions
+82 -19
View File
@@ -16,10 +16,12 @@ headers = {'User-Agent': '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Te
prts_up_char = Path(DRAW_PATH + "/draw_card_up/prts_up_char.json")
genshin_up_char = Path(DRAW_PATH + "/draw_card_up/genshin_up_char.json")
pretty_up_char = Path(DRAW_PATH + "/draw_card_up/pretty_up_char.json")
guardian_up_char = Path(DRAW_PATH + "/draw_card_up/guardian_up_char.json")
prts_url = "https://wiki.biligame.com/arknights/%E6%96%B0%E9%97%BB%E5%85%AC%E5%91%8A"
genshin_url = "https://wiki.biligame.com/ys/%E7%A5%88%E6%84%BF"
pretty_url = "https://wiki.biligame.com/umamusume/%E5%85%AC%E5%91%8A"
guardian_url = "https://wiki.biligame.com/gt/%E9%A6%96%E9%A1%B5"
# 是否过时
@@ -36,10 +38,10 @@ def is_expired(data: dict):
# 检查写入
def check_write(data: dict, up_char_file, game_name: str = ''):
tmp = data
if game_name in ['genshin', 'pretty']:
if game_name in ['genshin', 'pretty', 'guardian']:
tmp = data['char']
if not is_expired(tmp):
if game_name in ['genshin']:
if game_name in ['genshin', 'guardian']:
data['char']['title'] = ''
data['arms']['title'] = ''
elif game_name in ['pretty']:
@@ -57,7 +59,7 @@ def check_write(data: dict, up_char_file, game_name: str = ''):
with open(up_char_file, 'r', encoding='utf8') as f:
old_data = json.load(f)
tmp = old_data
if game_name in ['genshin', 'pretty']:
if game_name in ['genshin', 'pretty', 'guardian']:
tmp = old_data['char']
if is_expired(tmp):
return old_data
@@ -256,28 +258,30 @@ class PrettyAnnouncement:
for msg in x:
if msg.find('★') != -1:
msg = msg.replace('<br/>', '')
msg = msg.split(' ')
if (star := len(msg[0].strip())) == 3:
data['char']['up_char']['3'][msg[1]] = '70'
char_name = msg[msg.find('['):].strip()
if (star := len(msg[:msg.find('[')].strip())) == 3:
data['char']['up_char']['3'][char_name] = '70'
elif star == 2:
data['char']['up_char']['2'][msg[1]] = '70'
data['char']['up_char']['2'][char_name] = '70'
elif star == 1:
data['char']['up_char']['1'][msg[1]] = '70'
data['char']['up_char']['1'][char_name] = '70'
if str(p).find('(当期UP对象)') != -1 and str(p).find('赛马娘') == -1:
data['card']['pool_img'] = p.find('img')['src']
r = re.search(r'■全?新?支援卡(当期UP对象)([\s\S]*)</p>', str(p))
if r:
rmsg = r.group(1)
rmsg = r.group(1).strip()
rmsg = rmsg.split('<br/>')
for x in rmsg[1:]:
rmsg = [x for x in rmsg if x]
for x in rmsg:
x = x.replace('\n', '').replace('・', '')
x = x.split(' ')
if x[0] == 'SSR':
data['card']['up_char']['3'][x[1]] = '70'
if x[0] == 'SR':
data['card']['up_char']['2'][x[1]] = '70'
if x[0] == 'R':
data['card']['up_char']['1'][x[1]] = '70'
star = x[:x.find('[')].strip()
char_name = x[x.find('['):].strip()
if star == 'SSR':
data['card']['up_char']['3'][char_name] = '70'
if star == 'SR':
data['card']['up_char']['2'][char_name] = '70'
if star == 'R':
data['card']['up_char']['1'][char_name] = '70'
# 日文->中文
with open(DRAW_PATH + 'pretty_card.json', 'r', encoding='utf8') as f:
all_data = json.load(f)
@@ -291,8 +295,67 @@ class PrettyAnnouncement:
data['card']['up_char'][star][all_data[x]['中文名']] = '70'
except TimeoutError:
logger.warning(f'更新赛马娘UP池信息超时...')
with open(pretty_up_char, 'r', encoding='utf8') as f:
data = json.load(f)
if pretty_up_char.exists():
with open(pretty_up_char, 'r', encoding='utf8') as f:
data = json.load(f)
except Exception as e:
logger.error(f'赛马娘up更新失败 {type(e)}:{e}')
return check_write(data, pretty_up_char, 'pretty')
class GuardianAnnouncement:
@staticmethod
async def get_announcement_text():
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(guardian_url, timeout=7) as res:
return await res.text()
@staticmethod
async def update_up_char():
data = {
'char': {'up_char': {'3': {}}, 'title': '', 'time': '', 'pool_img': ''},
'arms': {'up_char': {'5': {}}, 'title': '', 'time': '', 'pool_img': ''}
}
try:
text = await GuardianAnnouncement.get_announcement_text()
soup = BeautifulSoup(text, 'lxml')
context = soup.select('div.col-sm-3:nth-child(3) > div:nth-child(2) > div:nth-child(1) '
'> div:nth-child(2) > div:nth-child(3) > font:nth-child(1)')[0]
title = context.find('p').find('b').text
tmp = title.split(',')
time = ''
for msg in tmp:
r = re.search(r'[从|至](.*)(开始|结束)', msg)
if r:
time += r.group(1).strip() + ' - '
time = time[:-3]
title = time.split(' - ')[0] + 'UP卡池'
data['char']['title'] = title
data['arms']['title'] = title
data['char']['time'] = time
data['arms']['time'] = time
start_idx = -1
end_idx = -1
index = 0
divs = context.find_all('div')
for x in divs:
if x.text == '角色':
start_idx = index
if x.text == '武器':
end_idx = index
break
index += 1
for x in divs[start_idx+1: end_idx]:
name = x.find('p').find_all('a')[-1].text
data['char']['up_char']['3'][name] = '0'
for x in divs[end_idx+1:]:
name = x.find('p').find_all('a')[-1].text
data['arms']['up_char']['5'][name] = '0'
except TimeoutError:
print(f'更新坎公骑冠剑UP池信息超时...')
with open(pretty_up_char, 'r', encoding='utf8') as f:
data = json.load(f)
except Exception as e:
print(f'坎公骑冠剑up更新失败 {type(e)}:{e}')
return check_write(data, guardian_up_char, 'guardian')