45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
import os
|
|
import re
|
|
|
|
def clean_and_save(path, content):
|
|
content = content.replace('\r\n', '\n').replace('\r', '\n')
|
|
lines = [line.rstrip() for line in content.split('\n')]
|
|
result_lines = []
|
|
prev_empty = False
|
|
for line in lines:
|
|
if line == '':
|
|
if not prev_empty:
|
|
result_lines.append(line)
|
|
prev_empty = True
|
|
else:
|
|
result_lines.append(line)
|
|
prev_empty = False
|
|
while result_lines and result_lines[-1] == '':
|
|
result_lines.pop()
|
|
text = '\r\n'.join(result_lines) + '\r\n'
|
|
text = text.lstrip('\ufeff')
|
|
with open(path, 'wb') as f:
|
|
f.write(b'\xef\xbb\xbf' + text.encode('utf-8'))
|
|
|
|
def update_dungeon_entry(name_keyword, url):
|
|
path_idx = 'dungeons/index.html'
|
|
with open(path_idx, 'rb') as f:
|
|
c = f.read().decode('utf-8', errors='ignore')
|
|
if f'href="{url}"' not in c:
|
|
c = c.replace('\r\n', '\n')
|
|
c = re.sub(rf'({re.escape(name_keyword)}.*?<td>)準備中(</td>)', rf'\g<1><a href="{url}">攻略を見る ➜</a>\2', c, flags=re.DOTALL)
|
|
clean_and_save(path_idx, c)
|
|
print(f"Updated {name_keyword} in {path_idx}")
|
|
else:
|
|
print(f"Already updated {name_keyword}")
|
|
|
|
if __name__ == '__main__':
|
|
# 1. yuweyawata.html を正規化保存
|
|
raw_content = open('dungeons/yuweyawata.html', 'rb').read().decode('utf-8', errors='ignore')
|
|
clean_and_save('dungeons/yuweyawata.html', raw_content)
|
|
print("Cleaned and saved dungeons/yuweyawata.html")
|
|
|
|
# 2. dungeons/index.html を更新
|
|
update_dungeon_entry('ユウェヤーワータ', 'yuweyawata.html')
|