Skip to content

Commit

Permalink
Generate a json status file for SpaceAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
timhawes committed Feb 6, 2014
1 parent f53d74d commit 8b76f65
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
46 changes: 46 additions & 0 deletions etc/spaceapi-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"api": "0.13",
"logo": "http://edinburghhacklab.com/spaceapi/logo.png",
"projects": [
"http://wiki.edinburghhacklab.com/",
"https://github.com/edinburghhacklab"
],
"icon": {
"open": "http://edinburghhacklab.com/spaceapi/open.png",
"closed": "http://edinburghhacklab.com/spaceapi/closed.png"
},
"space": "Edinburgh Hacklab",
"url": "http://edinburghhacklab.com/",
"issue_report_channels": [
"issue_mail"
],
"state": {
"icon": {
"open": "http://edinburghhacklab.com/spaceapi/open.png",
"closed": "http://edinburghhacklab.com/spaceapi/closed.png"
}
},
"contact": {
"issue_mail": "[email protected]",
"twitter": "@edinhacklab",
"phone": "+44 131 516 6856",
"facebook": "https://www.facebook.com/edinburghhacklab",
"irc": "irc://irc.freenode.net/#edinhacklab",
"email": "[email protected]"
},
"location": {
"lat": 55.939797,
"lon": -3.18162,
"address": "1 Summerhall, Edinburgh, EH9 1PL, United Kingdom"
},
"feeds": {
"blog": {
"url": "http://edinburghhacklab.com/feed/",
"type": "rss"
},
"calendar": {
"url": "https://www.google.com/calendar/ical/vo52ojfgltbpbkh8fk52vbhb90%40group.calendar.google.com/public/basic.ics",
"type": "ical"
}
}
}
50 changes: 49 additions & 1 deletion services/kay/status-handler
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import sys
sys.path.insert(0, "/srv/hacksense/lib")

import hacksense
import json
import logging
import os
import time
import urllib
import urllib2
Expand All @@ -25,6 +27,9 @@ irc_channel = "*DEFAULT*"
# secrets for sending status to twitter (via http://edinhacklab.com/)
twitter_secrets_file = '/srv/hacksense/etc/twitter_secrets.conf'

# spaceapi output file
spaceapi_json_path = '/srv/hacksense/var/spaceapi.json'

last_activity = time.time()
last_lcd = time.time()
last_ircstatus = 0
Expand All @@ -50,6 +55,42 @@ def push_status_to_server(status):
else:
return False

def write_spaceapi_json(status):
spaceapi = {
"api": "0.13",
"space": "Edinburgh Hacklab",
"logo": "http://edinburghhacklab.com/spaceapi/logo.png",
"url": "http://edinburghhacklab.com/",
"location": {"address": "1 Summerhall, Edinburgh, EH9 1PL, United Kingdom",
"lat": 55.939797,
"lon": -3.18162},
"state": {"open": False,
"lastchange": int(time.time()),
"icon": {"open": "http://edinburghhacklab.com/spaceapi/open.png",
"closed": "http://edinburghhacklab.com/spaceapi/closed.png"}},
"contact": {
"phone": "+44 131 516 6856",
"irc": "irc://irc.freenode.net/#edinhacklab",
"twitter": "@edinhacklab",
"email": "[email protected]",
"issue_mail": "[email protected]",
"facebook": "https://www.facebook.com/edinburghhacklab"},
"issue_report_channels": ["issue_mail"],
"projects": ["http://wiki.edinburghhacklab.com/", "https://github.com/edinburghhacklab"],
"feeds": {"blog": {"type": "rss", "url": "http://edinburghhacklab.com/feed/"},
"calendar": {"type": "ical", "url": "https://www.google.com/calendar/ical/vo52ojfgltbpbkh8fk52vbhb90%40group.calendar.google.com/public/basic.ics"}}
}
if status == 'open':
spaceapi['state']['open'] = True
else:
spaceapi['state']['open'] = False
spaceapi['open'] = spaceapi['state']['open'] # for compatibility with older API
spaceapi['icon'] = spaceapi['state']['icon'] # for compatibility with older API
f = open(spaceapi_json_path + '.tmp', 'w')
json.dump(spaceapi, f, indent=2)
f.close()
os.rename(spaceapi_json_path + '.tmp', spaceapi_json_path)

def lcd(line1, line2=""):
global last_lcd
last_lcd = max(last_lcd, time.time())
Expand Down Expand Up @@ -90,6 +131,7 @@ def set_status(new_status):
logging.info("set to open, tweet failed")
lcd("Status: Open", "Tweet failed")
say("Sorry, I couldn't reach twitter")
write_spaceapi_json(new_status)

elif new_status == "closed":
rconn.set('hacksense:labstatus:timestamp', time.time())
Expand All @@ -105,6 +147,7 @@ def set_status(new_status):
logging.info("set to closed, tweet failed")
lcd("Status: Closed", "Tweet failed")
say("Sorry, I couldn't reach twitter")
write_spaceapi_json(new_status)

def ircstatus_callback(timestamp, topic, headers, body):
global last_ircstatus
Expand Down Expand Up @@ -205,6 +248,11 @@ def callback(ch, method, properties, body):

logging.info("%s %s %r" % (timestamp, topic, headers))

if topic == "update-spaceapi.open":
write_spaceapi_json("open")
if topic == "update-spaceapi.closed":
write_spaceapi_json("closed")

if topic.startswith("doorbot.buttons.") or topic in ["doorbot.card.swipe", "doorbot.pir"]:
activity_callback(timestamp, topic, headers, body)

Expand All @@ -224,4 +272,4 @@ def callback(ch, method, properties, body):

rconn = hacksense.RedisConnection().redis
conn = hacksense.AMQPTopic()
conn.subscribe_callback(["timer.60", "doorbot.#", "irc.message"], callback)
conn.subscribe_callback(["timer.60", "doorbot.#", "irc.message", "update-spaceapi.#"], callback)
Empty file added var/.placeholder
Empty file.

0 comments on commit 8b76f65

Please sign in to comment.