diff --git a/src/main.py b/src/main.py index 6f575b7..7394670 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,5 @@ -from env import CSV_FILE_PATH, DRAW_PER_PERSON, EMAIL_SUBJECT, EMAIL_BODY -from file_io import open_csv, save_csv +from env import DRAW_PER_PERSON, HISTORY_YEARS, EMAIL_SUBJECT, EMAIL_BODY +from file_io import load_history, save_csv, load_participants from draw import draw_names from emailer import send_email from utils import get_current_time @@ -11,29 +11,29 @@ def send_all_emails(new_draw): for participant in new_draw: name = participant[0] receiver_email = participant[1] - draws = ", ".join(participant[2:]) # List of recipients + draws = ", ".join(participant[2:]) message = EMAIL_BODY.format(name=name, draws=draws) subject = EMAIL_SUBJECT.format(year=current_year) - send_email(receiver_email, subject, message) # Send the email + send_email(receiver_email, subject, message) print(f"Email sent to {name} ({receiver_email})") if __name__ == "__main__": try: - # Load previous draw data - old_draw = open_csv(CSV_FILE_PATH) + # Load data from historical CSVs and this year's participants + history_data = load_history(HISTORY_YEARS) - # Perform new draw - new_draw = draw_names(old_draw, DRAW_PER_PERSON) + current_year = date.today().year + current_participants = load_participants() # Load participants data + # Perform the draw + new_draw = draw_names(current_participants, history_data, DRAW_PER_PERSON) # Send emails to participants send_all_emails(new_draw) # Save new draw results - save_csv(new_draw, CSV_FILE_PATH) + save_csv(new_draw, current_year) - # Output completion time print(f"Process completed at {get_current_time()[1]} on {get_current_time()[0]}") except Exception as e: print(f"Error occurred: {e}") - # Retry or handle errors