Repository navigation
Expand file tree
/
Copy pathdata.py
More file actions
74 lines (53 loc) · 2.03 KB
/
Copy pathdata.py
File metadata and controls
74 lines (53 loc) · 2.03 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
import datetime
import json
import os
STALE_DATA_DAYS = 7
STALE_NOTIFICATION_FILE = "data/.last_stale_notification"
def get_stale_data_warning(today=None):
if today is None:
today = datetime.date.today()
today_file = f"data/{today.strftime('%Y%m%d')}.json"
try:
last_updated_timestamp = os.path.getmtime(today_file)
except OSError:
return "⚠️ Cached availability data has not been updated."
age = datetime.datetime.now().timestamp() - last_updated_timestamp
if age <= datetime.timedelta(days=STALE_DATA_DAYS).total_seconds():
return None
last_updated = datetime.datetime.fromtimestamp(last_updated_timestamp)
return (
f"⚠️ Cached availability data is over {STALE_DATA_DAYS} days old "
f"(last update: {last_updated.strftime('%d-%m-%Y')})."
)
def stale_notification_was_sent_today(today=None):
if today is None:
today = datetime.date.today()
try:
with open(STALE_NOTIFICATION_FILE, "r") as file:
return file.read().strip() == today.isoformat()
except OSError:
return False
def mark_stale_notification_sent(today=None):
if today is None:
today = datetime.date.today()
os.makedirs(os.path.dirname(STALE_NOTIFICATION_FILE), exist_ok=True)
with open(STALE_NOTIFICATION_FILE, "w") as file:
file.write(today.isoformat())
def get_schedule(hall, date):
file_name = f"data/{date.strftime('%Y%m%d')}.json"
if not os.path.exists(file_name):
return None
with open(file_name, "r") as file:
data = json.load(file)
return data.get(hall, None)
def update_schedule(hall, date, times):
file_name = f"data/{date.strftime('%Y%m%d')}.json"
if os.path.exists(file_name):
with open(file_name, "r") as file:
data = json.load(file)
else:
data = {}
data[hall] = times
with open(file_name, "w") as file:
json.dump(data, file, indent=4)
print(f"Schedule updated for {hall} on {date.strftime('%Y-%m-%d')}.", flush=True)