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
|
#!/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 + ' '
if status == 'open':
tweet = SUBLAB_OPEN + last_change
elif status == 'closed':
tweet = SUBLAB_CLOSED + last_change
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)
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 = '/tmp/sublab-doorstatus.last'
# instance of class which gets current status
s = SublabStatus()
status = s.door_status()
# read file with last status
with open(STATUSFILE, 'r') as status_file:
old_status = status_file.readline().rstrip()
last_change = status_file.readline().rstrip()
# check if status changed, if yes, update css and tweet it
if s.door_status() == 'open' and old_status == 'closed':
save_statusfile(STATUSFILE, status)
update_css('#0f0', '#222', 'status-open.png')
try:
tweet('open', last_change)
except TweetLengthException:
print("Tweet was too long")
elif s.door_status() == 'closed' and old_status == 'open':
save_statusfile(STATUSFILE, status)
update_css('#222', '#f00', 'status-closed.png')
try:
tweet('closed', last_change)
except TweetLengthException:
print("Tweet was too long")
else:
update_css = ('#222', '#222', 'status-unknown.png')
|