@@ -0,0 +1,2557 @@
+import pickle%0Afrom datetime import datetime%0Aimport requests%0A%0Afrom push import send_push%0A%0AAPI_URL = 'https://api.twitch.tv/kraken/streams?channel='%0A%0A%0Adef get_stream_info(channel_list):%0A    %22%22%22Returns list of streams from channel_list that are online%22%22%22%0A    channel_list_string = ','.join(str(x) for x in channel_list)%0A    response = requests.get(API_URL + channel_list_string)%0A    if not response.status_code == requests.codes.ok:%0A        return%0A    channels = response.json()%5B'streams'%5D%0A    keys = %5B%0A        'status',%0A        'url',%0A        'name',%0A        %5D%0A    time = datetime.now()%0A    online_channels = %5B%5D%0A    for channel in channels:%0A        channel_dict = %7Bkey: channel%5B'channel'%5D%5Bkey%5D for key in keys%7D%0A        channel_dict%5B'time'%5D = time%0A        channel_dict%5B'online'%5D = True%0A        online_channels.append(channel_dict)%0A    return online_channels%0A%0A%0Adef save_streams(stream_list):%0A    %22%22%22Saves the stream info so that we don't need to send an alert%0A    if one has already been sent. Returns streams and new stream names%22%22%22%0A%0A    with open('streams.pickle', 'r') as f:%0A        old = pickle.load(f)%0A%0A    updated_streams = %5B%5D%0A    new_stream_names = %5Bx%5B'name'%5D for x in stream_list%5D%0A    old_stream_names = %5Bx%5B'name'%5D for x in old%5D%0A    newly_online_names = list(set(new_stream_names)-set(old_stream_names))%0A    now_offline_streams = list(set(old_stream_names)-set(new_stream_names))%0A%0A    # Ignore streams that are offline for%0A    # more than 15 mins since last check%0A    time = datetime.now()%0A    for stream in old:%0A        if stream%5B'name'%5D in now_offline_streams:%0A            if (time - stream%5B'time'%5D).total_seconds() %3C 15*60:%0A                stream%5B'online'%5D = False%0A                updated_streams.append(stream)%0A%0A    # Brand new streams%0A    for stream in stream_list:%0A        if stream%5B'name'%5D in newly_online_names:%0A            updated_streams.append(stream)%0A%0A    # Still online streams%0A    for stream in stream_list:%0A        if stream%5B'name'%5D in old_stream_names:%0A            if stream%5B'online'%5D == True:%0A                updated_streams.append(stream)%0A%0A    with open('streams.pickle', 'wb') as f:%0A        pickle.dump(updated_streams, f)%0A%0A    # also return newly online stream names so we know what to push%0A    return updated_streams, newly_online_names%0A%0A%0Aif __name__ == '__main__':%0A%0A    with open('streams.txt') as f:%0A        channel_list = f.read().split('%5Cn')%0A%0A    stream_info = get_stream_info(channel_list)%0A    streams, new = save_streams(stream_info)%0A%0A    for stream in streams:%0A        if stream%5B'name'%5D in new:%0A            send_push(stream)%0A

Add main module