47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
import re
|
|
|
|
def normalize_file(path):
|
|
with open(path, 'rb') as f:
|
|
raw = f.read()
|
|
text = raw.decode('utf-8', errors='ignore')
|
|
while text.startswith('\ufeff'):
|
|
text = text[1:]
|
|
text = text.replace('\r\n', '\n').replace('\r', '\n')
|
|
lines = [l.rstrip() for l in text.split('\n')]
|
|
clean_lines = []
|
|
prev_blank = False
|
|
for l in lines:
|
|
if l == '':
|
|
if not prev_blank:
|
|
clean_lines.append(l)
|
|
prev_blank = True
|
|
else:
|
|
clean_lines.append(l)
|
|
prev_blank = False
|
|
while clean_lines and clean_lines[-1] == '':
|
|
clean_lines.pop()
|
|
final_text = '\r\n'.join(clean_lines) + '\r\n'
|
|
with open(path, 'wb') as f:
|
|
f.write(b'\xef\xbb\xbf' + final_text.encode('utf-8'))
|
|
print(f"Normalized {path}")
|
|
|
|
def update_trial_entry_contains(keyword, url):
|
|
path_idx = 'trials/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')
|
|
pattern = rf'(<td><strong>.*?{re.escape(keyword)}.*?</strong></td>.*?<td>)準備中(</td>)'
|
|
c = re.sub(pattern, rf'\g<1><a href="{url}">攻略を見る ➜</a>\2', c, count=1, flags=re.DOTALL)
|
|
with open(path_idx, 'wb') as f:
|
|
f.write(c.encode('utf-8'))
|
|
normalize_file(path_idx)
|
|
print(f"Updated {keyword} in {path_idx}")
|
|
else:
|
|
print(f"Already updated {keyword}")
|
|
|
|
if __name__ == '__main__':
|
|
normalize_file('trials/extreme_ifrit.html')
|
|
update_trial_entry_contains('極イフリート', 'extreme_ifrit.html')
|