This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
hackerspace-status-api/state.py

49 lines
859 B
Python
Raw Normal View History

2013-11-19 02:33:03 +01:00
#!/usr/bin/python
import urllib2
import json
import time
import logging
fileName = "json"
2014-02-14 22:41:22 +01:00
url = "http://status.krautspace.de/status/button/"
2013-11-19 02:33:03 +01:00
data = None
# Raum Status abrufen
try:
response = urllib2.urlopen(url, None, 8)
except:
logging.exception("urllib2")
else:
# Status lessen
data = response.read(1)
try:
data = int(data)
except:
logging.exception("ungueltigen Zustand gelesen")
data = None
# JSON Vorlage einlesen
try:
fp = file(fileName)
raw = json.load(fp)
fp.close()
except:
logging.exception("file")
exit(1)
# Bereich in dem der Status steht
state = raw["state"]
# letzte Verarbeitungszeit setzen
2014-02-14 22:02:35 +01:00
state["lastchange"] = int(time.time())
2013-11-19 02:33:03 +01:00
# Status setzen
if data and data == 0:
2014-02-14 22:02:35 +01:00
state["open"] = False
2013-11-19 02:33:03 +01:00
elif data and data == 1:
2014-02-14 22:02:35 +01:00
state["open"] = True
2013-11-19 02:33:03 +01:00
else:
2014-02-14 22:02:35 +01:00
state["open"] = None
2013-11-19 02:33:03 +01:00
# JSON Decodieren und ausgeben
print json.dumps(raw)