-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (53 loc) · 2.27 KB
/
main.py
File metadata and controls
69 lines (53 loc) · 2.27 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
import logging
from database_helper import DBHelper
from cron_helper import CronHelper
import os
# Constants for paths
# Get the realpath of the current file
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
# Constants for paths
VENV_ACTIVATE_PATH = os.path.join(CURRENT_DIR, "venv/bin/activate")
WRAPPER_SCRIPT_PATH = os.path.join(CURRENT_DIR, "wrapper_script.sh")
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def setup_contests(db_helper, cron_helper):
"""
Fetch contests and set up cron jobs.
"""
contests = db_helper.fetch_unnotified_contests()
for contest in contests:
contest_name = contest["contest_name"]
contest_url = contest["url"]
try:
# Check if the contest is already present in crontab
if cron_helper.is_contest_in_crontab(contest_url):
continue
logging.info(f"Setting up Contest: {contest_name} (URL: {contest_url})")
# Create cron jobs 24 hours and 1 hour before the contest
cron_helper.create_cron_job(contest, 24)
cron_helper.create_cron_job(contest, 1)
except Exception as e:
logging.error(f"Failed to set up cron job for contest: {contest_url} - Error: {e}")
def main():
logging.info("Starting contest fetcher...")
try:
# Initialize helpers
db_helper = DBHelper()
cron_helper = CronHelper(VENV_ACTIVATE_PATH, WRAPPER_SCRIPT_PATH)
# Fetch contests from external sources
logging.info("Fetching contests from external sources...")
db_helper.fetch_and_insert_codeforces_contests()
db_helper.fetch_and_insert_leetcode_contests()
db_helper.fetch_and_insert_atcoder_contests()
logging.info("External contests fetched and inserted into the database.")
# Setup cron jobs for contests
setup_contests(db_helper, cron_helper)
# Write and list cron jobs
cron_helper.write_cron_jobs()
# cron_helper.list_cron_jobs() # Uncomment to list cron jobs
except Exception as e:
logging.error(f"An error occurred while running the contest fetcher: {e}")
finally:
logging.info("Contest fetcher completed.")
if __name__ == "__main__":
main()