Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done auto save #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

from src.JobTracker.utils import EmailMessage
from src.JobTracker.chatbot import ChatGPT
from src.JobTracker.config import AUTO_SAVE_EMAIL

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')

def process_email(email_path):
def process_email(email_path, output_csv):
'''
Process the emails at the given path and return the results.

Expand All @@ -29,25 +30,38 @@ def process_email(email_path):
elif k.lower() == "n":
logging.info("---------Stop processing emails---------")
return
email_counter = 0
Garenbbbb marked this conversation as resolved.
Show resolved Hide resolved
for mail in mail_info:
state, data = chatbot.get_content(mail)
if state == 'Succeed':
res.append(data)
return res
email_counter += 1
if email_counter % AUTO_SAVE_EMAIL == 0:
export_to_csv(res, output_csv)
res = []
return res, email_counter

def export_to_csv(data, filename):
with open(filename, mode='w', newline='', encoding='utf-8') as csvfile:
file_exists = False
try:
with open(filename, 'r', newline='', encoding='utf-8') as csvfile:
file_exists = True
except FileNotFoundError:
pass
Garenbbbb marked this conversation as resolved.
Show resolved Hide resolved
mode = 'a' if file_exists else 'w'
Garenbbbb marked this conversation as resolved.
Show resolved Hide resolved
with open(filename, mode=mode, newline='', encoding='utf-8') as csvfile:
fieldnames = data[0].keys()
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
if not file_exists:
writer.writeheader()
for row in data:
writer.writerow(row)

def main(email_path, output_csv):
result = process_email(email_path)
if result:
result, count = process_email(email_path, output_csv)
Garenbbbb marked this conversation as resolved.
Show resolved Hide resolved
if count > 0:
export_to_csv(result, output_csv)
logging.info(f"Processed emails successfully and exported to CSV at {output_csv}.")
logging.info(f"Processed {str(count)} emails successfully and exported to CSV at {output_csv}.")
Garenbbbb marked this conversation as resolved.
Show resolved Hide resolved
else:
logging.info("No emails processed.")

Expand Down
2 changes: 1 addition & 1 deletion src/JobTracker/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os


AUTO_SAVE_EMAIL = 5
Garenbbbb marked this conversation as resolved.
Show resolved Hide resolved
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
MODEL = 'gpt-4-1106-preview'
PRICE = {
Expand Down