-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_notifications.py
149 lines (114 loc) · 5.38 KB
/
event_notifications.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
import requests
import json
import datetime
import math
import asyncio
import os
from json import JSONDecodeError
cwd = os.getcwd()
config_json = json.loads("{}")
config_path = str(cwd) + "/config.json"
print("config at " + config_path)
def is_json_key_present(json, key):
try:
buf = json[key]
except KeyError:
return False
return True
def get_time_until_string(minutes_until):
weeks = math.floor(minutes_until / 10080)
days = math.floor(minutes_until / 1440)
hours = math.floor(minutes_until / 60)
minutes_until = math.floor(minutes_until)
if weeks > 0:
return str(weeks) + " week(s)"
elif days > 0:
return str(days) + " day(s)"
elif hours > 0:
return str(hours) + " hour(s)"
else:
return str(minutes_until) + " minute(s)"
try:
with open(config_path, 'r') as config_file:
config_json = json.loads(config_file.read())
except IOError:
open(config_path, "a+")
print("No config found. Check github for example config and populate your config.json file")
exit(0)
except JSONDecodeError:
print("invalid config. Check github for example config and populate your config.json file")
exit(0)
api_key = config_json['api_key']
async def do_loop():
calendars = config_json['calendars']
if len(calendars) == 0:
print("No calendars found in the config. Check github for example config and populate your config.json file")
exit(0)
while True:
for calendar in calendars:
cal_id = calendar['calendar_id']
notification_times = calendar['notifications']
timezones = calendar['timezones']
discord_info = calendar['discord_info']
try:
result = requests.request('GET', 'https://www.googleapis.com/calendar/v3/calendars/' + str(cal_id) + '/events?key=' + str(api_key))
json_response = json.loads(result.content)
events = json_response['items']
for event in events:
name = event['summary']
link = event['htmlLink']
start_raw = event['start']['dateTime']
end_raw = event['end']['dateTime']
if start_raw.endswith("Z"):
start_raw = start_raw.replace("Z", "+00:00")
if end_raw.endswith("Z"):
end_raw = end_raw.replace("Z", "+00:00")
start = datetime.datetime.fromisoformat(start_raw)
end = datetime.datetime.fromisoformat(end_raw)
now = datetime.datetime.now(tz=start.tzinfo)
duration = end - start
duration_text = str(duration)
if start < now:
continue
date_different = start - now
minutes_diff = math.floor((date_different.total_seconds() / 60))
print(str(name) + " in " + str(minutes_diff) + " minutes")
if minutes_diff in notification_times:
start_time_value = ""
for timezone in timezones:
to_add = datetime.timedelta(hours=timezone['utc_offset'])
this_zone_time = start + to_add
start_time_value += str(timezone['name']) + " - " + this_zone_time.strftime("%a %d %B %Y, %H:%M") + "\n"
how_long_message = str(get_time_until_string(minutes_diff))
webhook_body = {
"embeds": [
{
"title": name + " in " + how_long_message,
"url": link,
"color": int(discord_info['color'][1:], 16),
"timestamp": start.isoformat(),
"fields": [
{
"name": "Start Time(s)",
"value": start_time_value,
"inline": False
},
{
"name": "Duration",
"value": duration_text,
"inline": "False"
}
]
}
]
}
if is_json_key_present(discord_info, 'bot_name'):
webhook_body['username'] = discord_info['bot_name']
if is_json_key_present(discord_info, 'bot_icon'):
webhook_body['avatar_url'] = discord_info['bot_icon']
send = requests.request('POST', discord_info['webhook_url'], json=webhook_body)
print(str(send.status_code) + " " + str(send.content))
except Exception as e:
print(str(e))
await asyncio.sleep(60)
asyncio.get_event_loop().run_until_complete(do_loop())