-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.py
79 lines (66 loc) · 2.38 KB
/
main.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
import requests
import json
import random
from base64 import b64decode
from lxml import html
def handler(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
# Set CORS headers for the preflight request
if request.method == 'OPTIONS':
print(request.method)
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': 'https://itsjafer.com',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type'
}
return ('', 204, headers)
# Set CORS headers for the main request
responseHeaders = {
'Access-Control-Allow-Methods': 'POST,GET',
'Access-Control-Allow-Origin': 'https://itsjafer.com'
}
# Get the request body
request_json = request.get_json()
# decode the resume from base64
resume = b64decode(request_json['content'])
# pick a random job posting
# (lever's demo API has ~350 postings)
postingSkip = random.randint(1,350)
# get information about this posting
postingInfo = requests.get(
f'https://api.lever.co/v0/postings/leverdemo?skip={postingSkip}&limit=1&mode=json'
)
# get the URL for this posting
postingURL = postingInfo.json()[0]['applyUrl']
# get CSRF token and posting ID from the posting
posting = requests.get(postingURL)
root = html.fromstring(posting.text)
csrf = root.get_element_by_id("csrf-token").get('value')
postingID = root.get_element_by_id("posting-id").get('value')
# ask Lever to parse our resume
headers = {
'Referer': 'https://jobs.lever.co/',
'Origin': 'https://jobs.lever.co/'
}
parseResponse = requests.post(
'https://jobs.lever.co/parseResume',
files=dict(
resume=('resume.pdf', resume),
csrf=(None,csrf),
postingId=(None,postingID)
),
headers=headers
)
response = {
"body": json.dumps(parseResponse.json())
}
return (json.dumps(response, default=str), 200, responseHeaders)