From dbddcd637b37e178366992d9d0946b92c0116ba3 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 12 Feb 2014 19:52:32 +0100 Subject: added status twitter function --- scripts/status_script.py | 78 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 9 deletions(-) (limited to 'scripts/status_script.py') diff --git a/scripts/status_script.py b/scripts/status_script.py index a92d5bf..86ca88f 100755 --- a/scripts/status_script.py +++ b/scripts/status_script.py @@ -1,10 +1,13 @@ -#!/usr/bin/python +#!/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 @@ -32,15 +35,33 @@ class SublabStatus(object): else: return 'closed' -if __name__ == '__main__': - s = SublabStatus() - if s.door_status() == 'open': - colors = ('#0f0', '#222', 'status-open.png') - elif s.door_status() == 'closed': - colors = ('#222', '#f00', 'status-closed.png') - else: - colors = ('#222', '#222', 'status-unknown.png') +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; @@ -68,3 +89,42 @@ span.sublabclosed { 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') -- cgit v1.2.1