-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathformat_fans.py
52 lines (42 loc) · 1.38 KB
/
format_fans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import re
with open("./index.html") as f:
lines = f.readlines()
fans_regex = re.compile(r'href="([^"]+)">@([^<]+)<')
fans_set = set()
start_ind = -1
last_ind = -1
for ind, line in enumerate(lines):
if start_ind != -1:
if "</p>" in line:
last_ind = ind
break
match = fans_regex.search(line.strip())
if match:
fans_set.add((match.group(1), match.group(2)))
if "Fans (in alphabetical order):" in line:
start_ind = ind
if start_ind == -1:
raise ValueError("'Fans (in alphabetical order):' text not found in the index.html")
if last_ind == -1:
raise ValueError("Closing '</p>' tag for closing fans list not found.")
def sort_key(fan):
"""
Sorting key for fans:
1. Sort by the first letter of the nickname (case-insensitive).
2. If the first letter is the same, sort by whether it’s uppercase.
3. Finally, sort alphabetically by the full nickname.
"""
_nickname = fan[1]
return (
_nickname[0].lower(),
_nickname[0].isupper(),
_nickname.lower(),
)
fans_list = sorted(fans_set, key=sort_key)
output = [
f' <a href="{_url}">@{_nickname}</a>{"," if len(fans_list) - 1 != ind else "."}\n'
for ind, (_url, _nickname) in enumerate(fans_list)
]
lines[start_ind + 1 : last_ind] = output
with open("./index.html", "w+") as f:
f.writelines(lines)