summaryrefslogtreecommitdiff
path: root/scripts/status_script.py
blob: a5595acfbebf66710abdb4466078f3f04e5f0317 (plain)
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
#!/usr/bin/env python

import urllib
import contextlib
import json
import time
import os
from credentials import *
from twitter import *
from datetime import datetime

class SublabStatus(object):
    json = None
    def get_json(self):
        if self.json is not None:
            return self.json

        with contextlib.closing(urllib.urlopen('http://beaglebone.local.sublab.org/')) as response:
            self.json = json.load(response)
        return self.json

    def door_status(self):
        try:
            self.get_json()
        except Exception:
            return 'unkown'

        door_status = self.json['door.lock']

        if door_status['ts'] + 120 < time.time():
            return 'unknown'

        if not door_status['value']:
            return 'open'
        else:
            return 'closed'

def tweet(status, last_change):

    # Contents of the status tweet
    # Max. 110 Characters, or tweet will be too long!
    SEPARATOR = ' - '
    DATE_MSG = 'Last change was on'
    SUBLAB_CLOSED = 'OH NOEZ! @sublab is now closed.' + SEPARATOR + DATE_MSG + ' '
    SUBLAB_OPEN = 'YAY! @sublab is now open' + SEPARATOR + DATE_MSG + ' '
    SUBLAB_UNKNOWN = "DAMN SON! Current status of @sublab couldn't be retrieved" + ' '

    if status == 'open':
        tweet = SUBLAB_OPEN + last_change
    elif status == 'closed':
        tweet = SUBLAB_CLOSED + last_change
    elif status == 'unknown':
        tweet = SUBLAB_UNKNOWN

    if len(tweet) <= 140:

        #instance of the twitter-class to post status changes
        t = Twitter(auth=OAuth(token,
            token_key,
            con_secret,
            con_secret_key))

        t.statuses.update(status=tweet)
    else:
        print "Message too long: " + tweet

def update_css(colors):
    css = '''
span.sublabopen {
    color: %s;
}

span.sublabclosed {
    color: %s;
}

#statusimage {
    background: url(../img/%s) ;
    height: 100px;
    width: 100px;
    position: absolute;
    right: 20px;
}


''' % colors

    path = os.path.realpath(os.path.dirname(__file__))
    path = os.path.join(path, '..', 'public', 'css', 'sublab.status.css')
    path_new = path + '.new'

    with open(path_new, 'w') as css_file:
        css_file.write(css)
    os.rename(path_new, path)



def save_statusfile(STATUSFILE, status):
    date_now = datetime.now().strftime("%a %d. %b %Y at %H:%M")
    with open(STATUSFILE, 'w') as status_file:
        status_file.write(status + '\n' + date_now + '\n')


if __name__ == '__main__':

    STATUSFILE = 'sublab-doorstatus.last'

    # instance of class which gets current status
    s = SublabStatus()

    # read file with last status
    try:
        with open(STATUSFILE, 'r') as status_file:
            old_status = status_file.readline().rstrip()
            last_change = status_file.readline().rstrip()
    except Exception:
        save_statusfile(STATUSFILE, s.door_status())
        exit()

    # check if status changed, if yes, update css and tweet it
    if s.door_status() != old_status:
        if s.door_status() == 'open':
            update_css(('#0f0', '#222', 'status-open.png'))
            save_statusfile(STATUSFILE, s.door_status())
            tweet('open', last_change)
        elif s.door_status() == 'closed':
            save_statusfile(STATUSFILE, s.door_status())
            update_css(('#222', '#f00', 'status-closed.png'))
            tweet('closed', last_change)
        else:
            save_statusfile(STATUSFILE, s.door_status())
            update_css(('#222', '#222', 'status-unknown.png'))
            tweet('unknown', last_change)