summaryrefslogtreecommitdiff
path: root/scripts/sub_api.py
blob: 8d1eb3288f0b5d65e4351369209bf8afae652e3d (plain)
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
#!/usr/bin/python

import json
import urllib
import time
import sys

import os.path

base_path = os.path.realpath(os.path.dirname(__file__))
base_path = os.path.join(base_path, '..')
json_template = os.path.join(base_path, "template", "template.spaceapi.json")
input_url = "http://taifun.local.sublab.org/subcan.json"
output_file = os.path.join(base_path, "public", "status.json")
status_dict = { "open" : True , "closed" : False }

sublab_status = json.load(open(json_template))
# What to set on unknown status
sublab_status["open"] = False

try:
    status_json = json.load(urllib.urlopen(input_url))
    if time.time() <= status_json["door.lock"]["ts"] + 120:
        door_status = status_json["door.lock"]["text"]
        sublab_status["open"] = status_dict[door_status]
except Exception:
    sys.excepthook(*sys.exc_info())

#atomically place new status
new_output_file = output_file + '.new'
with open(new_output_file, "w") as status_file:
    status_file.write(json.dumps(sublab_status, sort_keys=True, indent=4))
os.rename(new_output_file, output_file)