Temporary addition of instrumetation for debugging
This commit is contained in:
parent
a2169e82b1
commit
6dab29af3a
3 changed files with 36 additions and 17 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -8,3 +8,5 @@ open_ai_calls.json
|
||||||
_*
|
_*
|
||||||
data_folder*
|
data_folder*
|
||||||
.venv
|
.venv
|
||||||
|
generated_cv
|
||||||
|
resume.html
|
||||||
|
|
@ -6,6 +6,7 @@ import time
|
||||||
import traceback
|
import traceback
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from typing import List, Optional, Any, Tuple
|
from typing import List, Optional, Any, Tuple
|
||||||
|
import uuid
|
||||||
from reportlab.lib.pagesizes import letter
|
from reportlab.lib.pagesizes import letter
|
||||||
from reportlab.pdfgen import canvas
|
from reportlab.pdfgen import canvas
|
||||||
from selenium.common.exceptions import NoSuchElementException
|
from selenium.common.exceptions import NoSuchElementException
|
||||||
|
|
@ -179,24 +180,44 @@ class LinkedInEasyApplier:
|
||||||
self._create_and_upload_cover_letter(element)
|
self._create_and_upload_cover_letter(element)
|
||||||
|
|
||||||
def _create_and_upload_resume(self, element):
|
def _create_and_upload_resume(self, element):
|
||||||
"""Creates a resume in PDF format, uploads it using the upload element, and ensures cleanup of the temporary file."""
|
|
||||||
max_retries = 3
|
max_retries = 3
|
||||||
retry_delay = 1 # seconds
|
retry_delay = 1 # seconds
|
||||||
|
folder_path = 'generated_cv'
|
||||||
|
|
||||||
|
# Create the directory if it doesn't exist
|
||||||
|
if not os.path.exists(folder_path):
|
||||||
|
os.makedirs(folder_path)
|
||||||
|
|
||||||
for attempt in range(max_retries):
|
for attempt in range(max_retries):
|
||||||
try:
|
try:
|
||||||
html_string = self.gpt_answerer.get_resume_html()
|
html_string = self.gpt_answerer.get_resume_html()
|
||||||
html_string = html_string.replace('\n', '')
|
file_name = 'resume.html'
|
||||||
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_file:
|
with open(file_name, 'w', encoding='utf-8') as file:
|
||||||
pdf_path = temp_file.name
|
file.write(html_string)
|
||||||
|
file_path = os.path.abspath(file_name)
|
||||||
|
self.driver.execute_script("window.open('');")
|
||||||
|
self.driver.switch_to.window(self.driver.window_handles[1])
|
||||||
|
self.driver.get(f"file:///{file_path}")
|
||||||
|
time.sleep(1)
|
||||||
|
page_source = self.driver.page_source
|
||||||
|
self.driver.close()
|
||||||
|
self.driver.switch_to.window(self.driver.window_handles[0])
|
||||||
|
|
||||||
# Convert HTML to PDF
|
file_name_pdf = f"resume_{uuid.uuid4().hex}.pdf"
|
||||||
pisa_status = pisa.CreatePDF(html_string, dest=temp_file)
|
file_path_pdf = os.path.join(folder_path, file_name_pdf)
|
||||||
element.send_keys(pdf_path)
|
|
||||||
if pisa_status.err:
|
|
||||||
raise Exception(f"PDF generation failed with error: {pisa_status.err}")
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
|
# Convert HTML to PDF and save it to the specified folder
|
||||||
|
with open(file_path_pdf, 'wb') as pdf_file:
|
||||||
|
pisa_status = pisa.CreatePDF(page_source, dest=pdf_file)
|
||||||
|
|
||||||
|
file_path_pdf = os.path.abspath(file_path_pdf)
|
||||||
|
# Upload the file
|
||||||
|
element.send_keys(file_path_pdf)
|
||||||
|
|
||||||
|
if pisa_status.err:
|
||||||
|
raise Exception(f"PDF generation failed with error: {pisa_status.err}")
|
||||||
|
|
||||||
|
time.sleep(2) # Give some time for the upload process
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
@ -206,10 +227,6 @@ class LinkedInEasyApplier:
|
||||||
tb_str = traceback.format_exc()
|
tb_str = traceback.format_exc()
|
||||||
raise Exception(f"Max retries reached. Upload failed: \nTraceback:\n{tb_str}")
|
raise Exception(f"Max retries reached. Upload failed: \nTraceback:\n{tb_str}")
|
||||||
|
|
||||||
finally:
|
|
||||||
if os.path.exists(pdf_path):
|
|
||||||
os.remove(pdf_path)
|
|
||||||
|
|
||||||
def _upload_resume(self, element: WebElement) -> None:
|
def _upload_resume(self, element: WebElement) -> None:
|
||||||
element.send_keys(str(self.resume_dir))
|
element.send_keys(str(self.resume_dir))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -139,8 +139,8 @@ html_template = """
|
||||||
</span>
|
</span>
|
||||||
<span style="
|
<span style="
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 50%;
|
top: 16.5%;
|
||||||
left: 50%;
|
left: 18%;
|
||||||
transform: translate(-50%, -50%);
|
transform: translate(-50%, -50%);
|
||||||
padding: 6px;
|
padding: 6px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue