-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_progress.py
More file actions
99 lines (81 loc) · 2.8 KB
/
update_progress.py
File metadata and controls
99 lines (81 loc) · 2.8 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "elf",
# "tabulate"
# ]
# ///
from datetime import datetime
from pathlib import Path
from elf import OutputFormat, get_user_status
from elf.exceptions import StatusFetchError
from tabulate import tabulate
current_year = datetime.now().year
years = list(range(2015, current_year + 1))
# Fetch progress via elf
progress_by_year: dict[int, dict[int, int]] = {}
for year in years:
try:
status = get_user_status(year, fmt=OutputFormat.MODEL)
except StatusFetchError as e:
if "Event page not found for year" in str(e):
continue
else:
raise
progress_by_year[year] = {day.day: day.stars for day in status.days}
# Create table columns
headers = ["**Year**", "🌟"] + [f"**{d}**" for d in range(1, 26)]
table_data = []
# Add table rows
total_stars_available = 0
total_stars_obtained = 0
for year in sorted(progress_by_year.keys()):
year_progress = progress_by_year.get(year, {})
year_stars_available = len(year_progress) * 2
total_stars_available += year_stars_available
year_stars_obtained = sum(year_progress.values())
total_stars_obtained += year_stars_obtained
row = [f"**{year}**"]
if year_stars_obtained == year_stars_available:
row.append(f"**{year_stars_obtained}/{year_stars_available}** 🌟")
else:
row.append(f"{year_stars_obtained}/{year_stars_available}")
for day in range(1, 26):
stars = year_progress.get(day, None)
if stars is None:
icon = "⬛"
elif stars == 0:
icon = "⬜"
elif stars == 1:
icon = "🟨"
elif stars == 2:
icon = "🟩"
else:
assert False, "unexpected number of stars"
row.append(icon)
table_data.append(row)
timestamp = datetime.now().strftime("%Y-%m-%d")
# Generate markdown table
markdown_table = tabulate(table_data, headers=headers, tablefmt="github")
# Update README.md
readme_path = Path(__file__).parent / "README.md"
with open(readme_path, "r") as f:
readme_content = f.read()
# Find markers and replace content between them
start_marker = "<!-- PROGRESS_START -->"
end_marker = "<!-- PROGRESS_END -->"
if start_marker in readme_content and end_marker in readme_content:
before_start = readme_content.split(start_marker)[0]
after_end = readme_content.split(end_marker)[1]
new_progress = f"""\
{start_marker}
Updated {timestamp}.
{markdown_table}
Total stars: {total_stars_obtained}/{total_stars_available}
{end_marker}"""
new_content = f"{before_start}{new_progress}{after_end}"
with open(readme_path, "w") as f:
f.write(new_content)
print(f"README.md updated with progress as of {timestamp}.")
else:
print("Warning: Could not find progress markers in README.md")