Skip to content

Commit 3c52d0a

Browse files
authored
Merge pull request #177 from dipamsen/cache-calendar-2
Cache Academic Calendar
2 parents a6e8af0 + badc345 commit 3c52d0a

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ venv
1111
.env
1212
.python-version
1313

14-
ACADEMIC_CALENDAR_*.pdf
14+
*.pdf
1515
Academic_Cal-j/**
1616
final.json
1717

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ RUN chmod +x ./postinstall.sh
2020

2121
COPY . .
2222

23+
RUN python download-calendar.py
24+
2325
CMD [ "./postinstall.sh", "gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app" ]

app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def download_ics():
213213

214214
courses = get_courses(session, sso_token, roll_number)
215215

216-
ics_content = generate_ics(courses, "")
216+
ics_content = generate_ics(courses, "", is_web=True)
217217

218218
# Create an in-memory file-like object for the ics content
219219
ics_file = io.BytesIO()
@@ -256,7 +256,7 @@ def image_parser():
256256

257257
courses = build_courses_from_image(data)
258258

259-
ics_content = generate_ics(courses, "")
259+
ics_content = generate_ics(courses, "", is_web=True)
260260

261261
# Create an in-memory file-like object for the ics content
262262
ics_file = io.BytesIO()

download-calendar.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from utils.academic_calander_handler import get_latest_calendar
2+
import time
3+
4+
MAX_RETRIES = 10
5+
WAIT_TIME = 2
6+
7+
def main():
8+
for attempt in range(1, MAX_RETRIES + 1):
9+
print(f"Attempt {attempt} to download academic calendar...")
10+
success = get_latest_calendar()
11+
if success:
12+
print("Academic calendar downloaded successfully.")
13+
return
14+
else:
15+
print(f"Download failed. Retrying in {WAIT_TIME} seconds...")
16+
time.sleep(WAIT_TIME)
17+
print("Failed to download academic calendar after multiple attempts.")
18+
19+
if __name__ == "__main__":
20+
main()
21+

timetable/generate_ics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
WORKING_DAYS = dates.get_dates()
99

1010

11-
def generate_ics(courses: list[Course], output_filename):
11+
def generate_ics(courses: list[Course], output_filename, is_web=False):
1212
"""
1313
Creates an ICS file `timetable.ics` with the timetable data present inside the 'timetable' parameter.
1414
"""
@@ -52,7 +52,7 @@ def generate_ics(courses: list[Course], output_filename):
5252
event.add("dtend", holiday[1] + timedelta(days=1))
5353
cal.add_component(event)
5454

55-
for entry in academic_calander_handler.get_academic_calendar():
55+
for entry in academic_calander_handler.get_academic_calendar(is_web):
5656
event = Event()
5757
event.add("summary", entry.event)
5858
event.add("dtstart",entry.start_date)

utils/academic_calander_handler.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,27 @@ def delete_file(file):
6464

6565

6666
# fetch the latest academic calendar from the iitkgp website
67-
def get_latest_calendar():
67+
def get_latest_calendar(is_web=False):
6868
filename = get_latest_calendar_name()
6969
url = 'https://www.iitkgp.ac.in/assets/pdf/' + filename
7070

71-
## delete any old academic calander pdf if exists
72-
if (is_file_present(filename)):
71+
if is_web and is_file_present(filename):
72+
return True
73+
74+
# delete any old academic calendar pdf if exists
75+
if is_file_present(filename):
7376
delete_file(filename)
7477

75-
with open(filename, "wb") as file:
76-
response = requests.get(url)
77-
file.write(response.content)
78+
try:
79+
with open(filename, "wb") as file:
80+
response = requests.get(url)
81+
response.raise_for_status()
82+
file.write(response.content)
83+
except Exception as e:
84+
print(f"Error downloading calendar: {e}")
85+
return False
7886

79-
if (is_file_present(filename)):
87+
if is_file_present(filename):
8088
return True
8189
return False
8290

@@ -153,8 +161,8 @@ def clean_temp_files():
153161
continue
154162

155163

156-
def get_academic_calendar() -> list[DataEntry]:
157-
get_latest_calendar()
164+
def get_academic_calendar(is_web = False) -> list[DataEntry]:
165+
get_latest_calendar(is_web)
158166
export_json()
159167

160168
all_dates = merge_json()

0 commit comments

Comments
 (0)