Skip to content

Commit

Permalink
automatically update to new semester if found
Browse files Browse the repository at this point in the history
  • Loading branch information
Highfire1 committed Oct 16, 2024
1 parent 43129ff commit 3690ad4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
22 changes: 22 additions & 0 deletions Controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,29 @@ def updateLatestSemester(self, use_cache=False):
# now = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
# print(f"[{now}] Fetched new data from Langara. {len(changes)} changes found.")
return changes

def checkIfNextSemesterExistsAndUpdate(self):
latestSemester = Controller.getLatestSemester(self.engine)
year = latestSemester[0]
term = latestSemester[1]
year, term = self.incrementTerm(year, term)

termHTML = fetchTermFromWeb(year, term, use_cache=False)
if termHTML == None:
return False

print(f"New semester data for {year}{term} found!")

changes = self.updateSemester(year, term, use_cache=True)
self.genIndexesAndPreBuilts()

# now = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
# print(f"[{now}] Fetched new data from Langara. {len(changes)} changes found.")
return changes





# Build the entire database from scratch.
# Takes approximately 45 minutes from a live connection
Expand Down
26 changes: 22 additions & 4 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import gzip
import json
import os
from threading import Thread
import time

from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import FileResponse, HTMLResponse
Expand Down Expand Up @@ -45,7 +47,7 @@
from dotenv import load_dotenv
load_dotenv()

from schedule import every, repeat
from schedule import every, repeat, run_pending

# database controller
controller = Controller()
Expand All @@ -56,14 +58,30 @@ def get_session():

# === STARTUP STUFF ===

@repeat(every(60).minutes)
@repeat(every(20).seconds)
def hourly(use_cache: bool = False):
controller.updateLatestSemester(use_cache)
c = Controller()
c.updateLatestSemester(use_cache)


@repeat(every(24).hours)
def daily(use_cache: bool = False):
controller.buildDatabase(use_cache)
c = Controller()
c.buildDatabase(use_cache)

# check for next semester
c.checkIfNextSemesterExistsAndUpdate()

# have to run our scraping in a separate thread
def thread_task():
# hourly()
# daily()
while True:
run_pending()
time.sleep(1)

thread = Thread(target=thread_task)
thread.start()

# controller.create_db_and_tables()
# hourly(use_cache=False)
Expand Down

0 comments on commit 3690ad4

Please sign in to comment.