forked from tvansteenburgh/greenhouse
-
Notifications
You must be signed in to change notification settings - Fork 5
/
post-job.py
executable file
·631 lines (552 loc) · 22.8 KB
/
post-job.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#!/usr/bin/env python3
import argparse
import json
import os
import time
from textwrap import dedent
import selenium
import selenium.webdriver.support.ui as ui
from appdirs import user_data_dir
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
gh_url = "https://canonical.greenhouse.io"
JOB_BOARD = "Canonical - Jobs"
JOB_BOARDS_PROTECTED = ["Canonical", "INTERNAL"]
REGIONS = {
"americas": [
# United States
"Home based - Americas, Albany",
"Home based - Americas, Anchorage",
"Home based - Americas, Atlanta",
"Home based - Americas, Austin",
"Home based - Americas, Baltimore",
"Home based - Americas, Boston",
"Home based - Americas, Buffalo",
"Home based - Americas, Charlotte",
"Home based - Americas, Chicago",
"Home based - Americas, Cincinnati",
"Home based - Americas, Cleveland",
"Home based - Americas, Dallas",
"Home based - Americas, Dayton",
"Home based - Americas, Detroit",
"Home based - Americas, Honolulu",
"Home based - Americas, Houston",
"Home based - Americas, Kansas City",
"Home based - Americas, Los Angeles",
"Home based - Americas, Madison",
"Home based - Americas, Miami",
"Home based - Americas, Milwaukee",
"Home based - Americas, Minneapolis",
"Home based - Americas, New York",
"Home based - Americas, Oklahoma City",
"Home based - Americas, Omaha",
"Home based - Americas, Philadelphia",
"Home based - Americas, Phoenix",
"Home based - Americas, Pittsburgh",
"Home based - Americas, Portland",
"Home based - Americas, Raleigh",
"Home based - Americas, Rochester",
"Home based - Americas, Sacramento",
"Home based - Americas, San Diego",
"Home based - Americas, San Francisco",
"Home based - Americas, Salt Lake City",
"Home based - Americas, Seattle",
"Home based - Americas, Tulsa",
"Home based - Americas, Wichita",
"Home based - Americas, Washington",
# Canada
"Home based - Americas, Calgary",
"Home based - Americas, Montreal",
"Home based - Americas, Ottawa",
"Home based - Americas, Toronto",
"Home based - Americas, Vancouver",
# South America
"Home based - Americas, Belo Horizonte",
"Home based - Americas, Buenos Aires",
"Home based - Americas, Bogota",
"Home based - Americas, Caracas",
"Home based - Americas, Córdoba",
"Home based - Americas, Curitiba",
"Home based - Americas, Florianópolis",
"Home based - Americas, Guadalajara",
"Home based - Americas, Lima",
"Home based - Americas, Manaus",
"Home based - Americas, Monterrey",
"Home based - Americas, Mexico City",
"Home based - Americas, Porto Alegre",
"Home based - Americas, Rio de Janeiro",
"Home based - Americas, Santiago",
"Home based - Americas, São Paulo",
],
"us-boston": [
"Office based - Americas, Brockton, Massachusetts",
"Office based - Americas, Brookline, Massachusetts",
"Office based - Americas, Cambridge, Massachusetts",
"Office based - Americas, Cranston, Rhode Island",
"Office based - Americas, Fall River, Massachusetts",
"Office based - Americas, Framingham, Massachusetts",
"Office based - Americas, Haverhill, Massachusetts",
"Office based - Americas, Lawrence, Massachusetts",
"Office based - Americas, Lowell, Massachusetts",
"Office based - Americas, Lynn, Massachusetts",
"Office based - Americas, Malden, Massachusetts",
"Office based - Americas, Manchester, New Hampshire",
"Office based - Americas, Medford, Massachusetts",
"Office based - Americas, Methuen, Massachusetts",
"Office based - Americas, Nashua, New Hampshire",
"Office based - Americas, New Bedford, Massachusetts",
"Office based - Americas, Newton, Massachusetts",
"Office based - Americas, Pawtucket, Rhode Island",
"Office based - Americas, Peabody, Massachusetts",
"Office based - Americas, Plymouth, Massachusetts",
"Office based - Americas, Providence, Rhode Island",
"Office based - Americas, Quincy, Massachusetts",
"Office based - Americas, Revere, Massachusetts",
"Office based - Americas, Somerville, Massachusetts",
"Office based - Americas, Taunton, Massachusetts",
"Office based - Americas, Waltham, Massachusetts",
"Office based - Americas, Warwick, Rhode Island",
"Office based - Americas, Weymouth, Massachusetts",
"Office based - Americas, Worcester, Massachusetts",
],
"emea": [
"Home based - Africa, Accra",
"Home based - Africa, Cairo",
"Home based - Africa, Cape Town",
"Home based - Africa, Lagos",
"Home based - Africa, Nairobi",
"Home based - Europe, Amsterdam",
"Home based - Europe, Ankara",
"Home based - Europe, Athens",
"Home based - Europe, Barcelona",
"Home based - Europe, Berlin",
"Home based - Europe, Bratislava",
"Home based - Europe, Brno",
"Home based - Europe, Brussels",
"Home based - Europe, Bucharest",
"Home based - Europe, Budapest",
"Home based - Europe, Cluj-Napoca",
"Home based - Europe, Dublin",
"Home based - Europe, Edinburgh",
"Home based - Europe, Eindhoven",
"Home based - Europe, Frankfurt",
"Home based - Europe, Gdańsk",
"Home based - Europe, Glasgow",
"Home based - Europe, Helsinki",
"Home based - Europe, Istanbul",
"Home based - Europe, Kraków",
"Home based - Europe, Lisbon",
"Home based - Europe, Ljubljana",
"Home based - Europe, London",
"Home based - Europe, Lyon",
"Home based - Europe, Madrid",
"Home based - Europe, Manchester",
"Home based - Europe, Marousi", # Athens
"Home based - Europe, Milan",
"Home based - Europe, Moscow",
"Home based - Europe, Munich",
"Home based - Europe, Oslo",
"Home based - Europe, Paris",
"Home based - Europe, Plovdiv",
"Home based - Europe, Prague",
"Home based - Europe, Riga",
"Home based - Europe, Rome",
"Home based - Europe, Sofia",
"Home based - Europe, St. Petersburg",
"Home based - Europe, Stockholm",
"Home based - Europe, Tallinn",
"Home based - Europe, Timișoara",
"Home based - Europe, Thessaloniki",
"Home based - Europe, Vienna",
"Home based - Europe, Vilnius",
"Home based - Europe, Warsaw",
"Home based - Europe, Wrocław",
"Home based - Europe, Zagreb",
],
"apac": [
"Home based - Asia Pacific, Auckland",
"Home based - Asia Pacific, Bangalore",
"Home based - Asia Pacific, Beijing",
"Home based - Asia Pacific, Brisbane",
"Home based - Asia Pacific, Chennai",
"Home based - Asia Pacific, Christchurch",
"Home based - Asia Pacific, Delhi",
"Home based - Asia Pacific, Gurgaon",
"Home based - Asia Pacific, Fukuoka",
"Home based - Asia Pacific, Hangzhou",
"Home based - Asia Pacific, Hsinchu",
"Home based - Asia Pacific, Hong Kong",
"Home based - Asia Pacific, Hyderabad",
"Home based - Asia Pacific, Kyoto",
"Home based - Asia Pacific, Melbourne",
"Home based - Asia Pacific, Mumbai",
"Home based - Asia Pacific, Nagoya",
"Home based - Asia Pacific, Osaka",
"Home based - Asia Pacific, Pune",
"Home based - Asia Pacific, Sapporo",
"Home based - Asia Pacific, Seoul",
"Home based - Asia Pacific, Shanghai",
"Home based - Asia Pacific, Singapore",
"Home based - Asia Pacific, Sydney",
"Home based - Asia Pacific, Taipei",
"Home based - Asia Pacific, Tokyo",
"Home based - Asia Pacific, Wellington",
"Home based - Asia Pacific, Yokohama",
],
}
###############################################################
def parse_credentials():
# print("Inside: parse_credentials()")
# Read configuration from secured file in $HOME/.config/
creds = os.path.join(user_data_dir("greenhouse"), "login.tokens")
with open(os.path.expanduser(creds), "r") as auth:
try:
creds = json.load(auth)
ghsso_user = creds["username"]
ghsso_pass = creds["password"]
return (ghsso_user, ghsso_pass)
except FileNotFoundError:
print("file {} does not exist".format(creds))
###############################################################
def sso_authenticate(browser, args):
# print("Inside: sso_authenticate()")
(ghsso_user, ghsso_pass) = parse_credentials()
browser.get(gh_url)
# click Accept Cookies button
accept_cookies_btn = browser.find_elements(
By.XPATH, '//*[@id="cookie-policy-button-accept"]'
)
if accept_cookies_btn:
accept_cookies_btn[0].click()
# enter Ubuntu SSO email and password
email_txt = browser.find_element(By.ID, "id_email")
if email_txt:
email_txt.send_keys(ghsso_user)
password_txt = browser.find_element(By.ID, "id_password")
if password_txt:
password_txt.send_keys(ghsso_pass)
continue_btn = browser.find_elements(By.XPATH, '//button[@name="continue"]')
if continue_btn:
continue_btn[0].click()
# accept cookies so the popup doesn't obstruct clicks
cookie_accept_btn = browser.find_elements(By.CSS_SELECTOR, "#inform-cookies button")
for btn in cookie_accept_btn:
try:
# click can raise if element exists but is in a hidden block
btn.click()
except selenium.common.exceptions.ElementNotInteractableException:
pass
# click "Got it" button for new tips
got_it_btn = browser.find_elements(By.XPATH, '//a[text()="Got it"]')
if got_it_btn:
got_it_btn[0].click()
# minimize trays so they don't obstruct clicks
trays = browser.find_elements(By.XPATH, '//div[@data-provides="tray-close"]')
for tray in trays:
tray.click()
if args.headless:
mfa_token = input("Enter your 2FA token: ")
time.sleep(0.2)
mfa_txt = browser.find_element(By.XPATH, '//*[@id="id_oath_token"]')
mfa_txt.send_keys(mfa_token)
auth_button = browser.find_elements(By.XPATH, '//*[@id="login-form"]/button')[
0
].click()
###############################################################
def delete_posts(browser, wait, job_id):
browser.get(f"{gh_url}/plans/{job_id}/jobapp")
job_post_offset = 0
while True:
remove_tooltips(browser)
browser.refresh()
job_posts = len(
wait.until(
lambda browser: browser.find_elements(
By.XPATH, '//*[@id="job_applications"]/tbody/tr'
)
)
)
if job_posts == job_post_offset:
break
job_post = browser.find_element(
By.XPATH,
'//*[@id="job_applications"]/tbody/tr[' + str(job_post_offset + 1) + "]",
)
if job_post is None:
break
job_post_board = job_post.find_element(By.CSS_SELECTOR, ".board-column").text
if job_post_board in JOB_BOARDS_PROTECTED:
job_post_offset += 1
continue
if "live" in job_post.get_attribute("class").split():
job_post_unpublish = job_post.find_element(
By.CSS_SELECTOR, ".unpublish-application-button"
)
job_post_unpublish.click()
browser.find_element(By.LINK_TEXT, "Unpublish").click()
job_post_name = job_post.find_element(
By.CSS_SELECTOR, ".job-application__name"
).text.replace("\n", " ")
print(f"Deleting post '{job_post_name}' from job {job_id} ...")
# Click options menu (Delete/Duplicate)
job_post.find_element(By.XPATH, "td[3]/div/div[1]").click()
job_post.find_element(By.XPATH, "td[3]/div/div[2]/span/a").click()
browser.find_element(By.XPATH, '//*[@id="confirm-delete-post"]').click()
time.sleep(0.2)
###############################################################
def parse_args():
# print("Inside: parse_args()")
parser = argparse.ArgumentParser(
description="Duplicate Greenhouse job postings to multiple locations."
)
parser.add_argument(
"job_ids",
nargs="+",
help="The numeric Greenhouse job id (the number in the URL when on the Job Dashboard)",
)
parser.add_argument(
"--region",
dest="regions",
nargs="+",
choices=sorted(REGIONS.keys()),
help="The regions in which to create job postings",
)
parser.add_argument(
"--browser",
dest="browser",
choices=["chrome", "firefox"],
default="chrome",
help="The browser to use (default is chrome)",
)
parser.add_argument(
"--reset-all",
action="store_true",
help="Delete ALL posts under a given job_id (no --limit support)",
)
parser.add_argument(
"--headless", action="store_true", help="Run the automation without the GUI"
)
parser.add_argument(
"--limit", dest="limit", help="The specific job post to clone inside a REQ"
)
# arg parsing debug
# print(vars(parser.parse_args()))
return parser.parse_args()
###############################################################
def remove_tooltips(browser):
browser.execute_script(
dedent(
"""
const tooltipElements = document.getElementsByClassName("introjs-tooltiptext")
if (tooltipElements.length) {
const closeButton = tooltipElements[0].getElementsByClassName("close")[0]
closeButton.click()
}"""
)
)
def main():
args = parse_args()
options = Options()
prefs = {
"profile.default_content_setting_values": {
"plugins": 2,
"popups": 2,
"geolocation": 2,
"notifications": 2,
"fullscreen": 2,
"ssl_cert_decisions": 2,
"site_engagement": 2,
"durable_storage": 2,
}
}
if args.headless:
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")
else:
options.add_experimental_option("prefs", prefs)
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
if args.browser == "firefox":
browser = webdriver.Firefox()
else:
browser = webdriver.Chrome(options=options)
browser.maximize_window()
sso_authenticate(browser, args)
for job_id in args.job_ids:
job_posts_page_url = f"{gh_url}/plans/{job_id}/jobapp"
browser.get(job_posts_page_url)
wait = ui.WebDriverWait(browser, 60) # timeout after 60 seconds
if args.reset_all:
delete_posts(browser, wait, job_id)
break
multipage = False
page = 1
existing_ids = []
existing_types = []
existing_names = []
existing_locations = []
print(f"[Harvesting job details]")
while True:
print(f"-> Processing page {page}")
time.sleep(3.5)
remove_tooltips(browser)
# Ensure page navigation and job details have had sufficient time to load
job_locations = wait.until(
lambda browser: browser.find_elements(
By.CLASS_NAME, "job-application__offices"
)
)
job_names = browser.find_elements(By.CLASS_NAME, "job-application__name")
job_ids = browser.find_elements(By.CLASS_NAME, "job-edit-pencil")
job_types = browser.find_elements(By.CLASS_NAME, "board-column")
# harvest job details from each page of results
existing_types += [result.text for result in job_types]
existing_ids += [
result.get_attribute("href").split("/")[4] for result in job_ids
]
existing_names += [result.text.split("\n")[0] for result in job_names]
existing_locations += [result.text.strip("()") for result in job_locations]
next_page = browser.find_elements(By.CLASS_NAME, "next_page")
if not next_page:
break
if "disabled" not in next_page[0].get_attribute("class"):
multipage = True
page += 1
next_page[0].click()
else:
break
# return to first page of job posts
if multipage:
browser.get(job_posts_page_url)
time.sleep(3.5)
# Process updates for each `Canonical` job unless a limit arg is passed
if args.limit:
canonical_list = [args.limit]
else:
canonical_list = [
existing_ids[i]
for i, x in enumerate(existing_types)
if x in JOB_BOARDS_PROTECTED
]
for canonical_job_id in canonical_list:
canonical_job_name = [
existing_names[i]
for i, x in enumerate(existing_ids)
if canonical_job_id == x
][0]
limited_locations = [
existing_locations[i]
for i, x in enumerate(existing_names)
if canonical_job_name in x
]
print(f'[Creating posts for "{canonical_job_name}"]')
for region in args.regions:
print(f"-> Processing {region}")
region_locations = REGIONS[region]
new_locations = set(region_locations) - set(limited_locations)
if not new_locations:
print(f"--> All locations already exist.")
continue
for location_text in sorted(new_locations):
print(f"--> Processing {location_text}")
publish_location_text = location_text.split(",", 1)[-1].strip()
browser.get(
f"{job_posts_page_url}s/new?from=duplicate&greenhouse_job_application_id={canonical_job_id}"
)
time.sleep(3.5)
remove_tooltips(browser)
browser.refresh()
job_name_txt = browser.find_elements(
By.XPATH,
'//input[contains(@class, "Input__InputElem-sc-ipbxf8-0")]',
)[0]
job_name = (
job_name_txt.get_attribute("value")
.replace("Copy of ", "")
.strip()
)
job_name_txt.clear()
job_name_txt.send_keys(job_name)
post_to = browser.find_elements(
By.XPATH, '//label[text()="Post To"]/..//input[1]'
)[0]
post_to.send_keys(JOB_BOARD)
post_to.send_keys(Keys.ENTER)
location = browser.find_elements(
By.XPATH, '//label[text()="Location"]/..//input[1]'
)[0]
location.clear()
location.send_keys(location_text)
## Publish the posts out to our external partner sites
# try:
# browser.find_elements(By.XPATH, '//label[text()="Glassdoor"]/input[1]')[0].click()
# except:
# print("INFO: Glassdoor board not available at the moment")
try:
browser.find_elements(
By.XPATH, '//label[text()="Indeed"]/input[1]'
)[0].click()
except:
print("INFO: Indeed board not available at the moment")
publish_location = browser.find_elements(
By.XPATH, '//input[@placeholder="Select location"]'
)[0]
publish_location.clear()
publish_location.send_keys(publish_location_text)
popup_menu_xpath = (
f'//ul[contains(@class, "ui-menu")]'
f'/li[contains(@class, "ui-menu-item")]'
f'/div[contains(text(), "{publish_location_text}")]'
)
location_choices = wait.until(
lambda browser: browser.find_elements(
By.XPATH, popup_menu_xpath
)
)
publish_location.send_keys(Keys.DOWN)
publish_location.send_keys(Keys.TAB)
browser.find_elements(
By.XPATH, '//label[text()="Remote"]/input[1]'
)[0].click()
time.sleep(0.5)
# click the Save button
save_btn = browser.find_elements(By.XPATH, '//a[text()="Save"]')[0]
save_btn.click()
wait.until(
lambda browser: browser.find_elements(
By.CLASS_NAME, "job-application__offices"
)
)
print(f"[Marking all job posts live]")
browser.get(job_posts_page_url)
page = 1
while True:
print(f"-> Processing page {page}")
time.sleep(3.5)
# Ensure page navigation and job details have had sufficient time to load
wait.until(
lambda browser: browser.find_elements(
By.CLASS_NAME, "job-application__offices"
)
)
## Click the "Enable" button on each new post created, to make it live
publish_btns = browser.find_elements(
By.XPATH,
'//tr[@class="job-application draft external"]//img[@class="publish-application-button"]',
)
for btn in publish_btns:
btn.click()
time.sleep(0.5)
next_page = browser.find_elements(By.CLASS_NAME, "next_page")
if not next_page:
break
if "disabled" not in next_page[0].get_attribute("class"):
next_page[0].click()
page += 1
else:
break
print("All done! Now go bring those candidates through to offers!")
if __name__ == "__main__":
main()