-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_notificater.py
89 lines (80 loc) · 3.07 KB
/
slack_notificater.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
import json
import os
import time
import datetime
import urllib.request
import sys
import requests
def format_message(data):
data = json.loads(data)
notify_data = {}
now_datetime_str = datetime.datetime.now().strftime("%Y/%m/%d %H")
now_datetime_formated = datetime.datetime.strptime(now_datetime_str,"%Y/%m/%d %H")
start_time = int(time.mktime(now_datetime_formated.timetuple()))
start_time_mod7200 = start_time%7200
if start_time_mod7200 == 0:
start_time = start_time
else :
start_time = start_time -3600
end_time = start_time - 7200 #2h unixtime
print(datetime.datetime.fromtimestamp(start_time))
for match_data in data['league']:
if match_data['start_time'] == start_time:
notify_data = match_data
payload = {
'username': 'Splatoon Stage Notificater',
'icon_emoji': ':splatoon_icon_league:',
'text': 'Splatoon Event Notification',
'attachments': [
{
'fallback': 'Detailed information on Splatoon Stage.',
'color': 'good',
'title': 'Battle from {} to {}'.format(datetime.datetime.fromtimestamp(notify_data['start_time']+3600*9).strftime("%H:%M"), datetime.datetime.fromtimestamp(notify_data['end_time']+3600*9).strftime("%H:%M")),
'fields': [
{
'title': 'Game Mode',
'value': notify_data['game_mode']['name'],
'short': False
},
{
'title': 'Rule',
'value': notify_data['rule']['name'],
'short': False
},
{
'title': 'Stage A',
'value': notify_data['stage_a']['name'],
'short': True
},
{
'title': 'Stage B',
'value': notify_data['stage_b']['name'],
'short': True
}
]
}
]
}
return payload
def notify_slack(url, payload):
data = json.dumps(payload).encode('utf-8')
method = 'POST'
headers = {'Content-Type': 'application/json'}
request = urllib.request.Request(url, data = data, method = method, headers = headers)
with urllib.request.urlopen(request) as response:
return response.read().decode('utf-8')
def get_splatoon_information(url,iksm_session):
cookies = dict(iksm_session=iksm_session)
r = requests.get(url, cookies=cookies)
if r.status_code == 200:
return r.json()
else:
print(r.status_code)
sys.exit(1)
def lambda_handler(event, context):
webhook_url = os.environ['WEBHOOK_URL']
iksm_session = os.environ['IKSM_SESSION']
splatoon_information = get_splatoon_information("https://app.splatoon2.nintendo.net/api/schedules",iksm_session)
payload = format_message(json.dumps(splatoon_information))
response = notify_slack(webhook_url, payload)
return response