-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (63 loc) · 2.26 KB
/
main.py
File metadata and controls
94 lines (63 loc) · 2.26 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
import sys
import time
import os
from dotenv import load_dotenv
from schedule import every, repeat, run_pending
import logging
import logging
logger = logging.getLogger("LangaraCourseWatcherScraper")
logger.setLevel(logging.INFO)
screen_handler = logging.StreamHandler()
formatter = logging.Formatter(fmt='[%(asctime)s] : [%(levelname)-8s] : %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
screen_handler.setFormatter(formatter)
logger.addHandler(screen_handler)
from Controller import Controller
load_dotenv()
DB_LOCATION="database/database.db"
CACHE_DB_LOCATION="database/cache/cache.db"
PREBUILTS_DIRECTORY="database/prebuilts/"
ARCHIVES_DIRECTORY="database/archives/"
@repeat(every(60).minutes)
def hourly(use_cache: bool = False):
c = Controller()
c.updateLatestSemester(use_cache)
c.setMetadata("last_updated")
@repeat(every(24).hours)
def daily(use_cache: bool = False):
c = Controller()
# check for next semester
c.checkIfNextSemesterExistsAndUpdate()
c.buildDatabase(use_cache)
c.setMetadata("last_updated")
if __name__ == "__main__":
logger.info("Launching Langara Course Watcher")
if not os.path.exists("database/"):
os.mkdir("database")
if not os.path.exists("database/cache"):
os.mkdir("database/cache")
if not os.path.exists(PREBUILTS_DIRECTORY):
os.mkdir(PREBUILTS_DIRECTORY)
if not os.path.exists(ARCHIVES_DIRECTORY):
os.mkdir(ARCHIVES_DIRECTORY)
if (os.path.exists(DB_LOCATION)):
logger.info("Database found.")
controller = Controller()
controller.create_db_and_tables()
# controller.checkIfNextSemesterExistsAndUpdate()
hourly(use_cache=True)
daily(use_cache=True)
controller.setMetadata("last_updated")
else:
logger.info("Database not found. Building database from scratch.")
# save results to cache if cache doesn't exist
controller = Controller()
controller.create_db_and_tables()
controller.buildDatabase(use_cache=False)
controller.setMetadata("last_updated")
logger.info("Finished intialization.")
# hourly()
# daily()
while True:
run_pending()
time.sleep(1)