-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (48 loc) · 1.5 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
'''
This script uses the https://calendarific.com/ api
'''
import requests
from datetime import date
import smtplib
from email.message import EmailMessage
def email_alert(sender, password, subject, body, receiver):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = receiver
msg['from'] = sender
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender, password)
server.send_message(msg)
server.quit()
SENDER = "" # make sure less secure app access is turned on
RECEIVER = ""
PASSWORD = "" # sender's gmail password
with open('api_key.txt', 'r') as key: # <-- use your own api key here
api_key = key.read().strip()
country = 'IN' # <-- you can also change the country... just look up the iso code from the docs
now = date.today()
day = str(now.day)
month = str(now.month)
year = str(now.year)
url = f'https://calendarific.com/api/v2/holidays?&api_key={api_key}&country={country}&day={day}&month={month}&year={year}'
try:
r = requests.get(url)
data = r.json()
holidays = data['response']['holidays']
except Exception:
holidays = None
response = ""
if holidays is None:
response += "Could not fetch data. Terminating"
elif len(holidays) == 0:
response += "No holidays today..."
else:
response += "\nToday's holidays :\n"
response += "*************************\n"
for holiday in holidays:
response += "\n"
response += holiday['name'] + "\n"
response += holiday['description']+ "\n"
email_alert(SENDER, PASSWORD, "Holidays", response, RECEIVER)