-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseek.py
132 lines (110 loc) · 4.18 KB
/
seek.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
import os
import sendgrid
import urllib.request
from datetime import datetime
from bs4 import BeautifulSoup
from sendgrid.helpers.mail import Mail, Email, To, Content
def lambda_handler(event, context):
jobs = get_markup()
target_jobs = search_for_target(jobs)
notify(target_jobs)
def get_markup():
"""Requests HTML from webpage, specifically tag that holds list of jobs"""
fp = urllib.request.urlopen(os.environ.get("SITE"))
markup_bytes = fp.read()
markup_str = markup_bytes.decode("utf8")
fp.close()
soup = BeautifulSoup(markup_str, "html.parser")
jobs = soup.find_all("div", {"class": "jobblock block"})
return jobs
def search_for_target(jobs):
"""Goes through HTML to look for keywords"""
terms = ["systems analyst", "programmer"]
target_jobs = []
for job in jobs:
position = job.get("data-title")
for term in terms:
if term in position.casefold():
close_date = job.get("data-expires_at")
url = job.get("data-url")
metadata = {"position": position, "close_date": close_date, "url": url}
# ensure that only unique data is appended
if len(target_jobs) == 0:
target_jobs.append(metadata)
else:
for found_jobs in target_jobs:
if url not in found_jobs.values():
target_jobs.append(metadata)
return target_jobs
def notify(target_jobs):
"""Creates and sends email to recipient about the result of the search"""
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))
if len(target_jobs) > 0:
print("Target job(s) found!")
for job in target_jobs:
from_email = Email(os.environ.get("SENDER"))
to_email = To(os.environ.get("RECIPIENT"))
subject = "Job Vacancy Found!!"
message = get_found_message(job)
content = Content("text/html", message)
mail = Mail(from_email, to_email, subject, content)
# Get a JSON-ready representation of the Mail object
mail_json = mail.get()
# Send an HTTP POST request to /mail/send
response = sg.client.mail.send.post(request_body=mail_json)
print(response.status_code)
print(response.headers)
print("Notification sent via email")
else:
print("Target job(s) NOT found")
from_email = Email(os.environ.get("SENDER"))
to_email = To(os.environ.get("RECIPIENT"))
subject = "No Jobs Found :("
message = get_not_found_message()
content = Content("text/html", message)
mail = Mail(from_email, to_email, subject, content)
# Get a JSON-ready representation of the Mail object
mail_json = mail.get()
# Send an HTTP POST request to /mail/send
response = sg.client.mail.send.post(request_body=mail_json)
print(response.status_code)
print(response.headers)
print("Notification sent via email")
def get_found_message(job):
message = """
<p>Hi %s,</p>
<br/>
<p>Great News!</p>
<p>There is a vacancy for <b>%s</b> at FFA. This vacancy will be closed on <i>%s</i>.
More information can be found <a href="%s">here</a>.</p>
<br/>
<p>Cheers,</p>
<p>Your favorite bot :)</p>
""" % (
os.environ.get("NAME"),
job["position"],
convert_date_format(job["close_date"]),
job["url"],
)
return message
def get_not_found_message():
message = """
<p>Hi %s,</p>
<br/>
<p>Unfortunately, no relevant vacancies were found on the FFA website.
I will check again next month! ;)</p>
<br/>
<p>Cheers,</p>
<p>Your favorite bot :)</p>
""" % (
os.environ.get("NAME")
)
return message
def convert_date_format(datetime_str):
"""Helper function that changes the date format of date"""
dto = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S %z")
# convert = datetime.strftime(dto, '%d-%B-%Y %H:%M:%S')
convert = datetime.strftime(dto, "%c")
return convert
if __name__ == "__main__":
lambda_handler(None, None)