fixed some issues
This commit is contained in:
parent
9ef928569b
commit
6540bbbb40
9 changed files with 127 additions and 77 deletions
79
main.py
79
main.py
|
|
@ -7,9 +7,9 @@ import click
|
|||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.service import Service as ChromeService
|
||||
from webdriver_manager.chrome import ChromeDriverManager
|
||||
from selenium.common.exceptions import WebDriverException, TimeoutException
|
||||
from lib_resume_builder_AIHawk import Resume,StyleManager,FacadeManager,ResumeGenerator
|
||||
from src.utils import chromeBrowserOptions
|
||||
from selenium.common.exceptions import WebDriverException
|
||||
from lib_resume_builder_AIHawk import Resume, StyleManager, FacadeManager, ResumeGenerator
|
||||
from src.utils import chrome_browser_options
|
||||
from src.gpt import GPTAnswerer
|
||||
from src.linkedIn_authenticator import LinkedInAuthenticator
|
||||
from src.linkedIn_bot_facade import LinkedInBotFacade
|
||||
|
|
@ -19,14 +19,16 @@ from src.job_application_profile import JobApplicationProfile
|
|||
# Suppress stderr
|
||||
sys.stderr = open(os.devnull, 'w')
|
||||
|
||||
|
||||
class ConfigError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ConfigValidator:
|
||||
@staticmethod
|
||||
def validate_email(email: str) -> bool:
|
||||
return re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email) is not None
|
||||
|
||||
|
||||
@staticmethod
|
||||
def validate_yaml_file(yaml_path: Path) -> dict:
|
||||
try:
|
||||
|
|
@ -36,37 +38,37 @@ class ConfigValidator:
|
|||
raise ConfigError(f"Error reading file {yaml_path}: {exc}")
|
||||
except FileNotFoundError:
|
||||
raise ConfigError(f"File not found: {yaml_path}")
|
||||
|
||||
|
||||
|
||||
def validate_config(config_yaml_path: Path) -> dict:
|
||||
parameters = ConfigValidator.validate_yaml_file(config_yaml_path)
|
||||
required_keys = {
|
||||
'remote': bool,
|
||||
'experienceLevel': dict,
|
||||
'experience_level': dict,
|
||||
'jobTypes': dict,
|
||||
'date': dict,
|
||||
'positions': list,
|
||||
'locations': list,
|
||||
'distance': int,
|
||||
'companyBlacklist': list,
|
||||
'titleBlacklist': list
|
||||
'company_blacklist': list,
|
||||
'title_blacklist': list
|
||||
}
|
||||
|
||||
for key, expected_type in required_keys.items():
|
||||
if key not in parameters:
|
||||
if key in ['companyBlacklist', 'titleBlacklist']:
|
||||
if key in ['company_blacklist', 'title_blacklist']:
|
||||
parameters[key] = []
|
||||
else:
|
||||
raise ConfigError(f"Missing or invalid key '{key}' in config file {config_yaml_path}")
|
||||
elif not isinstance(parameters[key], expected_type):
|
||||
if key in ['companyBlacklist', 'titleBlacklist'] and parameters[key] is None:
|
||||
if key in ['company_blacklist', 'title_blacklist'] and parameters[key] is None:
|
||||
parameters[key] = []
|
||||
else:
|
||||
raise ConfigError(f"Invalid type for key '{key}' in config file {config_yaml_path}. Expected {expected_type}.")
|
||||
raise ConfigError(
|
||||
f"Invalid type for key '{key}' in config file {config_yaml_path}. Expected {expected_type}.")
|
||||
|
||||
experience_levels = ['internship', 'entry', 'associate', 'mid-senior level', 'director', 'executive']
|
||||
for level in experience_levels:
|
||||
if not isinstance(parameters['experienceLevel'].get(level), bool):
|
||||
if not isinstance(parameters['experience_level'].get(level), bool):
|
||||
raise ConfigError(f"Experience level '{level}' must be a boolean in config file {config_yaml_path}")
|
||||
|
||||
job_types = ['full-time', 'contract', 'part-time', 'temporary', 'internship', 'other', 'volunteer']
|
||||
|
|
@ -86,9 +88,10 @@ class ConfigValidator:
|
|||
|
||||
approved_distances = {0, 5, 10, 25, 50, 100}
|
||||
if parameters['distance'] not in approved_distances:
|
||||
raise ConfigError(f"Invalid distance value in config file {config_yaml_path}. Must be one of: {approved_distances}")
|
||||
raise ConfigError(
|
||||
f"Invalid distance value in config file {config_yaml_path}. Must be one of: {approved_distances}")
|
||||
|
||||
for blacklist in ['companyBlacklist', 'titleBlacklist']:
|
||||
for blacklist in ['company_blacklist', 'title_blacklist']:
|
||||
if not isinstance(parameters.get(blacklist), list):
|
||||
raise ConfigError(f"'{blacklist}' must be a list in config file {config_yaml_path}")
|
||||
if parameters[blacklist] is None:
|
||||
|
|
@ -96,8 +99,6 @@ class ConfigValidator:
|
|||
|
||||
return parameters
|
||||
|
||||
|
||||
|
||||
@staticmethod
|
||||
def validate_secrets(secrets_yaml_path: Path) -> tuple:
|
||||
secrets = ConfigValidator.validate_yaml_file(secrets_yaml_path)
|
||||
|
|
@ -113,10 +114,13 @@ class ConfigValidator:
|
|||
raise ConfigError(f"Password cannot be empty in secrets file {secrets_yaml_path}.")
|
||||
return secrets['email'], str(secrets['password']), secrets['llm_api_key']
|
||||
|
||||
|
||||
class FileManager:
|
||||
@staticmethod
|
||||
def find_file(name_containing: str, with_extension: str, at_path: Path) -> Path:
|
||||
return next((file for file in at_path.iterdir() if name_containing.lower() in file.name.lower() and file.suffix.lower() == with_extension.lower()), None)
|
||||
return next((file for file in at_path.iterdir() if
|
||||
name_containing.lower() in file.name.lower() and file.suffix.lower() == with_extension.lower()),
|
||||
None)
|
||||
|
||||
@staticmethod
|
||||
def validate_data_folder(app_data_folder: Path) -> tuple:
|
||||
|
|
@ -125,13 +129,15 @@ class FileManager:
|
|||
|
||||
required_files = ['secrets.yaml', 'config.yaml', 'plain_text_resume.yaml']
|
||||
missing_files = [file for file in required_files if not (app_data_folder / file).exists()]
|
||||
|
||||
|
||||
if missing_files:
|
||||
raise FileNotFoundError(f"Missing files in the data folder: {', '.join(missing_files)}")
|
||||
|
||||
output_folder = app_data_folder / 'output'
|
||||
output_folder.mkdir(exist_ok=True)
|
||||
return (app_data_folder / 'secrets.yaml', app_data_folder / 'config.yaml', app_data_folder / 'plain_text_resume.yaml', output_folder)
|
||||
return (
|
||||
app_data_folder / 'secrets.yaml', app_data_folder / 'config.yaml', app_data_folder / 'plain_text_resume.yaml',
|
||||
output_folder)
|
||||
|
||||
@staticmethod
|
||||
def file_paths_to_dict(resume_file: Path | None, plain_text_resume_file: Path) -> dict:
|
||||
|
|
@ -147,14 +153,16 @@ class FileManager:
|
|||
|
||||
return result
|
||||
|
||||
|
||||
def init_browser() -> webdriver.Chrome:
|
||||
try:
|
||||
options = chromeBrowserOptions()
|
||||
options = chrome_browser_options()
|
||||
service = ChromeService(ChromeDriverManager().install())
|
||||
return webdriver.Chrome(service=service, options=options)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to initialize browser: {str(e)}")
|
||||
|
||||
|
||||
def create_and_run_bot(email, password, parameters, llm_api_key):
|
||||
try:
|
||||
style_manager = StyleManager()
|
||||
|
|
@ -162,13 +170,14 @@ def create_and_run_bot(email, password, parameters, llm_api_key):
|
|||
with open(parameters['uploads']['plainTextResume'], "r", encoding='utf-8') as file:
|
||||
plain_text_resume = file.read()
|
||||
resume_object = Resume(plain_text_resume)
|
||||
resume_generator_manager = FacadeManager(llm_api_key, style_manager, resume_generator, resume_object, Path("data_folder/output"))
|
||||
resume_generator_manager = FacadeManager(llm_api_key, style_manager, resume_generator, resume_object,
|
||||
Path("data_folder/output"))
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
resume_generator_manager.choose_style()
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
|
||||
|
||||
job_application_profile_object = JobApplicationProfile(plain_text_resume)
|
||||
|
||||
|
||||
browser = init_browser()
|
||||
login_component = LinkedInAuthenticator(browser)
|
||||
apply_component = LinkedInJobManager(browser)
|
||||
|
|
@ -187,34 +196,40 @@ def create_and_run_bot(email, password, parameters, llm_api_key):
|
|||
|
||||
|
||||
@click.command()
|
||||
@click.option('--resume', type=click.Path(exists=True, file_okay=True, dir_okay=False, path_type=Path), help="Path to the resume PDF file")
|
||||
@click.option('--resume', type=click.Path(exists=True, file_okay=True, dir_okay=False, path_type=Path),
|
||||
help="Path to the resume PDF file")
|
||||
def main(resume: Path = None):
|
||||
try:
|
||||
data_folder = Path("data_folder")
|
||||
secrets_file, config_file, plain_text_resume_file, output_folder = FileManager.validate_data_folder(data_folder)
|
||||
|
||||
|
||||
parameters = ConfigValidator.validate_config(config_file)
|
||||
email, password, llm_api_key = ConfigValidator.validate_secrets(secrets_file)
|
||||
|
||||
|
||||
parameters['uploads'] = FileManager.file_paths_to_dict(resume, plain_text_resume_file)
|
||||
parameters['outputFileDirectory'] = output_folder
|
||||
|
||||
|
||||
create_and_run_bot(email, password, parameters, llm_api_key)
|
||||
except ConfigError as ce:
|
||||
print(f"Configuration error: {str(ce)}")
|
||||
print("Refer to the configuration guide for troubleshooting: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
print(
|
||||
"Refer to the configuration guide for troubleshooting: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
except FileNotFoundError as fnf:
|
||||
print(f"File not found: {str(fnf)}")
|
||||
print("Ensure all required files are present in the data folder.")
|
||||
print("Refer to the file setup guide: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
print(
|
||||
"Refer to the file setup guide: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
except RuntimeError as re:
|
||||
|
||||
print(f"Runtime error: {str(re)}")
|
||||
|
||||
print("Refer to the configuration and troubleshooting guide: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
print(
|
||||
"Refer to the configuration and troubleshooting guide: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
except Exception as e:
|
||||
print(f"An unexpected error occurred: {str(e)}")
|
||||
print("Refer to the general troubleshooting guide: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
print(
|
||||
"Refer to the general troubleshooting guide: https://github.com/feder-cr/LinkedIn_AIHawk_automatic_job_application/blob/main/readme.md#configuration")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue