Skip to content

Commit 334c462

Browse files
Store progress every Sunday
1 parent e76563f commit 334c462

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

generate.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,38 @@
3232
generation_time = datetime.now(timezone.utc)
3333

3434

35-
def get_previous_completion(language_code: str) -> float:
35+
def get_cached_data() -> dict:
3636
try:
3737
with open('index.json') as f:
38-
previous_data = json.load(f)
39-
for item in previous_data:
40-
if item['language']['code'] == language_code:
41-
return item.get('completion', 0.0)
38+
return json.load(f)
4239
except FileNotFoundError:
43-
pass
44-
return 0.0
40+
return {"previous_completion": {}, "last_sunday": None}
41+
42+
def get_last_sunday() -> str:
43+
today = datetime.date.today()
44+
offset = today.weekday() + 1
45+
last_sunday = today - datetime.timedelta(days=offset)
46+
return last_sunday.isoformat()
47+
48+
def update_previous_completion(cached_data: dict, completion_progress: list):
49+
current_sunday = get_last_sunday()
50+
if cached_data.get('last_sunday') != current_sunday:
51+
cached_data['previous_completion'] = {
52+
item['language']['code']: item['completion'] for item in completion_progress
53+
}
54+
cached_data['last_sunday'] = current_sunday
55+
56+
def save_index_json(cached_data: dict, completion_progress: list):
57+
with open('index.json', 'w') as f:
58+
json.dump(
59+
{
60+
"last_sunday": cached_data['last_sunday'],
61+
"previous_completion": cached_data['previous_completion'],
62+
"languages": [asdict(item) for item in completion_progress],
63+
},
64+
f,
65+
indent=2,
66+
)
4567

4668

4769
def get_completion_progress() -> Iterator['LanguageProjectData']:
@@ -125,15 +147,19 @@ class LanguageProjectData:
125147
if __name__ == '__main__':
126148
logging.basicConfig(level=logging.INFO)
127149
logging.info(f'starting at {generation_time}')
150+
151+
cached_data = get_cached_data()
152+
completion_progress = list(get_completion_progress())
153+
154+
update_previous_completion(cached_data, completion_progress)
155+
save_index_json(cached_data, completion_progress)
156+
128157
template = Template(Path('template.html.jinja').read_text())
129158

130159
output = template.render(
131-
completion_progress=(completion_progress := list(get_completion_progress())),
160+
completion_progress=completion_progress,
132161
generation_time=generation_time,
133162
duration=(datetime.now(timezone.utc) - generation_time).seconds,
134163
)
135164

136165
Path('index.html').write_text(output)
137-
Path('index.json').write_text(
138-
json.dumps(completion_progress, indent=2, default=asdict)
139-
)

0 commit comments

Comments
 (0)