-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkable.py
64 lines (53 loc) · 2.11 KB
/
workable.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
import requests
from notion_client import Client
# Replace with your actual tokens and subdomain
ACCESS_TOKEN = 'Workable API Token'
NOTION_TOKEN = 'Notion API Token'
WORKABLE_SUBDOMAIN = 'subdomain'
NOTION_DATABASE_ID = 'Notion Database ID'
workable_headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {ACCESS_TOKEN}"
}
notion = Client(auth=NOTION_TOKEN)
def fetch_jobs_from_workable():
response = requests.get(f"https://{WORKABLE_SUBDOMAIN}.workable.com/spi/v3/jobs?state=published", headers=workable_headers)
response.raise_for_status()
return response.json()['jobs']
def reformat_job_data(jobs):
reformatted_data = []
for job in jobs:
if job['state'] == 'published': # Ensure job is in the 'published' state
reformatted_data.append({
"Title": job['title'],
"Location": job['location']['country'],
"Link": job['shortlink']
})
return reformatted_data
def clear_existing_entries():
query = {"database_id": NOTION_DATABASE_ID}
response = notion.databases.query(**query)
for page in response['results']:
notion.pages.update(page_id=page['id'], archived=True)
def create_notion_pages(notion_data):
for job in notion_data:
notion.pages.create(parent={"database_id": NOTION_DATABASE_ID},
properties={
"Title": {
"title": [{"text": {"content": job["Title"]}}]
},
"Location": {
"rich_text": [{"text": {"content": job["Location"]}}]
},
"Link": {
"url": job["Link"]
}
})
def main():
jobs = fetch_jobs_from_workable()
notion_data = reformat_job_data(jobs)
clear_existing_entries()
create_notion_pages(notion_data)
print("Notion database has been refreshed with new data.")
if __name__ == "__main__":
main()