save old answer for not use too much money
This commit is contained in:
parent
3c09856232
commit
a6d1d9fe42
@ -28,27 +28,23 @@ class LinkedInEasyApplier:
|
|||||||
self.set_old_answers = set_old_answers
|
self.set_old_answers = set_old_answers
|
||||||
self.gpt_answerer = gpt_answerer
|
self.gpt_answerer = gpt_answerer
|
||||||
self.resume_generator_manager = resume_generator_manager
|
self.resume_generator_manager = resume_generator_manager
|
||||||
self.questions_data = []
|
self.all_data = self._load_questions_from_json()
|
||||||
|
|
||||||
|
|
||||||
def _load_questions_from_json(self) -> List[dict]:
|
def _load_questions_from_json(self) -> List[dict]:
|
||||||
output_file = 'answers.json'
|
output_file = 'answers.json'
|
||||||
try:
|
try:
|
||||||
# Leggi i dati esistenti dal file
|
|
||||||
try:
|
try:
|
||||||
with open(output_file, 'r') as f:
|
with open(output_file, 'r') as f:
|
||||||
try:
|
try:
|
||||||
all_data = json.load(f)
|
data = json.load(f)
|
||||||
if not isinstance(all_data, list):
|
if not isinstance(data, list):
|
||||||
raise ValueError("JSON file format is incorrect. Expected a list of questions.")
|
raise ValueError("JSON file format is incorrect. Expected a list of questions.")
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
# Se il file è vuoto o non contiene JSON valido, inizializza come lista vuota
|
data = []
|
||||||
all_data = []
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# Se il file non esiste, inizializza come lista vuota
|
data = []
|
||||||
all_data = []
|
return data
|
||||||
|
|
||||||
return all_data
|
|
||||||
except Exception:
|
except Exception:
|
||||||
tb_str = traceback.format_exc()
|
tb_str = traceback.format_exc()
|
||||||
raise Exception(f"Error loading questions data from JSON file: \nTraceback:\n{tb_str}")
|
raise Exception(f"Error loading questions data from JSON file: \nTraceback:\n{tb_str}")
|
||||||
@ -245,13 +241,22 @@ class LinkedInEasyApplier:
|
|||||||
if radios:
|
if radios:
|
||||||
question_text = section.text.lower()
|
question_text = section.text.lower()
|
||||||
options = [radio.text.lower() for radio in radios]
|
options = [radio.text.lower() for radio in radios]
|
||||||
|
|
||||||
|
existing_answer = None
|
||||||
|
for item in self.all_data:
|
||||||
|
if self._sanitize_text(question_text) in item['question'] and item['type'] == 'radio':
|
||||||
|
existing_answer = item
|
||||||
|
break
|
||||||
|
if existing_answer:
|
||||||
|
self._select_radio(radios, existing_answer['answer'])
|
||||||
|
return True
|
||||||
|
|
||||||
answer = self.gpt_answerer.answer_question_from_options(question_text, options)
|
answer = self.gpt_answerer.answer_question_from_options(question_text, options)
|
||||||
self._select_radio(radios, answer)
|
|
||||||
self._save_questions_to_json({'type': 'radio', 'question': question_text, 'answer': answer})
|
self._save_questions_to_json({'type': 'radio', 'question': question_text, 'answer': answer})
|
||||||
|
self._select_radio(radios, answer)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _find_and_handle_textbox_question(self, section: WebElement) -> bool:
|
def _find_and_handle_textbox_question(self, section: WebElement) -> bool:
|
||||||
text_fields = section.find_elements(By.TAG_NAME, 'input') + section.find_elements(By.TAG_NAME, 'textarea')
|
text_fields = section.find_elements(By.TAG_NAME, 'input') + section.find_elements(By.TAG_NAME, 'textarea')
|
||||||
if text_fields:
|
if text_fields:
|
||||||
@ -259,13 +264,23 @@ class LinkedInEasyApplier:
|
|||||||
question_text = section.find_element(By.TAG_NAME, 'label').text.lower()
|
question_text = section.find_element(By.TAG_NAME, 'label').text.lower()
|
||||||
is_numeric = self._is_numeric_field(text_field)
|
is_numeric = self._is_numeric_field(text_field)
|
||||||
if is_numeric:
|
if is_numeric:
|
||||||
answer = self.gpt_answerer.answer_question_numeric(question_text)
|
|
||||||
question_type = 'numeric'
|
question_type = 'numeric'
|
||||||
|
answer = self.gpt_answerer.answer_question_numeric(question_text)
|
||||||
else:
|
else:
|
||||||
answer = self.gpt_answerer.answer_question_textual_wide_range(question_text)
|
|
||||||
question_type = 'textbox'
|
question_type = 'textbox'
|
||||||
self._enter_text(text_field, answer)
|
answer = self.gpt_answerer.answer_question_textual_wide_range(question_text)
|
||||||
|
|
||||||
|
|
||||||
|
existing_answer = None
|
||||||
|
for item in self.all_data:
|
||||||
|
if item['question'] == self._sanitize_text(question_text) and item['type'] == question_type:
|
||||||
|
existing_answer = item
|
||||||
|
break
|
||||||
|
if existing_answer:
|
||||||
|
self._enter_text(text_field, existing_answer['answer'])
|
||||||
|
return True
|
||||||
self._save_questions_to_json({'type': question_type, 'question': question_text, 'answer': answer})
|
self._save_questions_to_json({'type': question_type, 'question': question_text, 'answer': answer})
|
||||||
|
self._enter_text(text_field, answer)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -273,9 +288,22 @@ class LinkedInEasyApplier:
|
|||||||
date_fields = section.find_elements(By.CLASS_NAME, 'artdeco-datepicker__input ')
|
date_fields = section.find_elements(By.CLASS_NAME, 'artdeco-datepicker__input ')
|
||||||
if date_fields:
|
if date_fields:
|
||||||
date_field = date_fields[0]
|
date_field = date_fields[0]
|
||||||
|
question_text = section.text.lower()
|
||||||
answer_date = self.gpt_answerer.answer_question_date()
|
answer_date = self.gpt_answerer.answer_question_date()
|
||||||
self._enter_text(date_field, answer_date.strftime("%Y-%m-%d"))
|
answer_text = answer_date.strftime("%Y-%m-%d")
|
||||||
self._save_questions_to_json({'type': 'date', 'question': section.text.lower(), 'answer': answer_date.strftime("%Y-%m-%d")})
|
|
||||||
|
|
||||||
|
existing_answer = None
|
||||||
|
for item in self.all_data:
|
||||||
|
if self._sanitize_text(question_text) in item['question'] and item['type'] == 'date':
|
||||||
|
existing_answer = item
|
||||||
|
break
|
||||||
|
if existing_answer:
|
||||||
|
self._enter_text(date_field, existing_answer['answer'])
|
||||||
|
return True
|
||||||
|
|
||||||
|
self._save_questions_to_json({'type': 'date', 'question': question_text, 'answer': answer_text})
|
||||||
|
self._enter_text(date_field, answer_text)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -287,9 +315,19 @@ class LinkedInEasyApplier:
|
|||||||
if dropdown:
|
if dropdown:
|
||||||
select = Select(dropdown)
|
select = Select(dropdown)
|
||||||
options = [option.text for option in select.options]
|
options = [option.text for option in select.options]
|
||||||
|
|
||||||
|
existing_answer = None
|
||||||
|
for item in self.all_data:
|
||||||
|
if self._sanitize_text(question_text) in item['question'] and item['type'] == 'dropdown':
|
||||||
|
existing_answer = item
|
||||||
|
break
|
||||||
|
if existing_answer:
|
||||||
|
self._select_dropdown_option(dropdown, existing_answer['answer'])
|
||||||
|
return True
|
||||||
|
|
||||||
answer = self.gpt_answerer.answer_question_from_options(question_text, options)
|
answer = self.gpt_answerer.answer_question_from_options(question_text, options)
|
||||||
self._select_dropdown_option(dropdown, answer)
|
|
||||||
self._save_questions_to_json({'type': 'dropdown', 'question': question_text, 'answer': answer})
|
self._save_questions_to_json({'type': 'dropdown', 'question': question_text, 'answer': answer})
|
||||||
|
self._select_dropdown_option(dropdown, answer)
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
@ -323,22 +361,21 @@ class LinkedInEasyApplier:
|
|||||||
try:
|
try:
|
||||||
with open(output_file, 'r') as f:
|
with open(output_file, 'r') as f:
|
||||||
try:
|
try:
|
||||||
all_data = json.load(f)
|
data = json.load(f)
|
||||||
if not isinstance(all_data, list):
|
if not isinstance(data, list):
|
||||||
raise ValueError("JSON file format is incorrect. Expected a list of questions.")
|
raise ValueError("JSON file format is incorrect. Expected a list of questions.")
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
all_data = []
|
data = []
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
all_data = []
|
data = []
|
||||||
all_data.append(question_data)
|
data.append(question_data)
|
||||||
with open(output_file, 'w') as f:
|
with open(output_file, 'w') as f:
|
||||||
json.dump(all_data, f, indent=4)
|
json.dump(data, f, indent=4)
|
||||||
except Exception:
|
except Exception:
|
||||||
tb_str = traceback.format_exc()
|
tb_str = traceback.format_exc()
|
||||||
raise Exception(f"Error saving questions data to JSON file: \nTraceback:\n{tb_str}")
|
raise Exception(f"Error saving questions data to JSON file: \nTraceback:\n{tb_str}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _sanitize_text(self, text: str) -> str:
|
def _sanitize_text(self, text: str) -> str:
|
||||||
sanitized_text = text.lower()
|
sanitized_text = text.lower()
|
||||||
sanitized_text = sanitized_text.strip()
|
sanitized_text = sanitized_text.strip()
|
||||||
|
Loading…
Reference in New Issue
Block a user