-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_script.py
More file actions
140 lines (106 loc) · 3.86 KB
/
selenium_script.py
File metadata and controls
140 lines (106 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import os
import shutil
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
download_path = os.path.abspath(os.path.join(os.getcwd(), 'temp'))
os.makedirs(download_path, exist_ok=True)
class Browser(webdriver.Chrome):
def __init__(self):
# Set Chrome preferences
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
options.add_experimental_option(
'prefs',
{
'profile.default_content_settings.popups': 0,
'download.default_directory': download_path,
},
)
super().__init__(options=options)
self.wait = WebDriverWait(self, 60)
def open(self, url):
self.get(url)
def fetch_latex(self):
# wait for the latex to load
self.wait.until(
lambda driver: driver.find_element(
By.CSS_SELECTOR,
'div[data-language="latex"]',
)
)
latex = ''
# the latex code is fetched in a websocket request
for entry in browser.get_log('performance'):
try:
message = json.loads(entry['message'])['message']
if message['method'] == 'Network.webSocketFrameReceived':
payload = message['params']['response']['payloadData']
# print(payload)
if '\\begin{document}' in payload:
x = payload.find('[')
arr = json.loads(payload[x:])[1]
latex = '\n'.join(arr)
if latex == '':
print(payload)
raise ValueError("Couldn't parse ⚠️")
break
except:
pass
if latex == '':
raise ValueError('Unable to fetch ⚠️')
return latex
def download_pdf(self):
# click on the download button to get the pdf
download_btn = self.wait.until(
lambda driver: driver.find_element(
By.CSS_SELECTOR,
'a[aria-label="Download PDF"][data-disabled="false"]',
)
)
download_btn.click()
time.sleep(5) # Wait for the download to complete
print('Pdf downloaded 🎉')
# download the pdf in a temp folder, move it to the root and then delete it
pdf = os.listdir('temp')[0]
shutil.copy(f'temp/{pdf}', 'resume.pdf')
os.remove(f'temp/{pdf}')
def save_latex_if_updated(latex, filename):
# if there are no changes in latex file do sys.exit(0) and skip next steps
try:
with open(filename) as file:
if latex == file.read():
return False
except FileNotFoundError:
print('First run 🏃♂️')
# if latex file is not found or if it has changed, write it and download pdf
with open('resume.tex', 'w') as file:
file.write(latex)
return True
# url = 'https://www.overleaf.com/read/nsgsskwncdmy#a859ac'
url = sys.argv[1]
browser = Browser()
browser.get(url)
try:
latex = browser.fetch_latex()
except Exception as e:
print(e)
sys.exit(1)
changes_detected = save_latex_if_updated(latex, 'resume.tex')
if not changes_detected:
print('No changes detected ✅️')
# setting github env var: skip=True
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
fh.write('skip=True\n')
sys.exit(0)
browser.download_pdf()
browser.quit()
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
fh.write('skip=False\n')