-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert.py
89 lines (73 loc) · 2.77 KB
/
alert.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 pickle
from datetime import datetime
import requests
from config import config
from push import send_push
API_URL = 'https://api.twitch.tv/kraken/streams?channel='
def get_stream_info(channel_list):
"""Returns list of streams from channel_list that are online"""
channel_list_string = ','.join(channel_list)
CLIENT_ID = config.get('TWITCH_CLIENT_ID')
headers = {'Client-ID': CLIENT_ID}
response = requests.get(API_URL + channel_list_string,
headers=headers)
if not response.status_code == requests.codes.ok:
return
channels = response.json()['streams']
keys = [
'status',
'url',
'name',
]
time = datetime.now()
online_channels = []
for channel in channels:
channel_dict = {key: channel['channel'][key] for key in keys}
channel_dict['time'] = time
channel_dict['online'] = True
online_channels.append(channel_dict)
return online_channels
def save_streams(stream_list):
"""Saves the stream info so that we don't need to send an alert
if one has already been sent. Returns streams and new stream names"""
try:
with open('streams.pickle') as f:
old = pickle.load(f)
except (IOError, EOFError):
with open('streams.pickle', 'w+') as f:
pickle.dump([], f)
old = []
updated_streams = []
new_stream_names = [x['name'] for x in stream_list]
old_stream_names = [x['name'] for x in old]
newly_online_names = list(set(new_stream_names)-set(old_stream_names))
now_offline_streams = list(set(old_stream_names)-set(new_stream_names))
# Ignore streams that are offline for
# more than 15 mins since last check
time = datetime.now()
for stream in old:
if stream['name'] in now_offline_streams:
if (time - stream['time']).total_seconds() < 15*60:
stream['online'] = False
updated_streams.append(stream)
# Brand new streams
for stream in stream_list:
if stream['name'] in newly_online_names:
updated_streams.append(stream)
# Still online streams
for stream in stream_list:
if stream['name'] in old_stream_names:
if stream['online'] == True:
updated_streams.append(stream)
with open('streams.pickle', 'wb') as f:
pickle.dump(updated_streams, f)
# also return newly online stream names so we know what to push
return updated_streams, newly_online_names
if __name__ == '__main__':
with open('streams.txt') as f:
channel_list = f.read().splitlines()
stream_info = get_stream_info(channel_list)
streams, new = save_streams(stream_info)
for stream in streams:
if stream['name'] in new:
send_push(stream)