#!/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 try: with contextlib.closing(urllib.urlopen('http://[2a02:238:f02a:9800:caa0:30ff:feb4:2185]/')) as response: self.json = json.load(response) return self.json except IOError: 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(statusimage): css = ''' #statusimage { background: url(../img/%s) ; height: 40px; width: 40px; margin: 5px; } ''' % statusimage 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('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('status-closed.png') tweet('closed', last_change) else: save_statusfile(STATUSFILE, s.door_status()) update_css('status-unknown.png') tweet('unknown', last_change)