#!/usr/bin/python import json import urllib import os.path base_path = os.path.realpath(os.path.dirname(__file__)) base_path = os.path.join(base_path, '..') #declarations input_url = "http://taifun.local.sublab.org/subcan.json" json_template = os.path.join(base_path, "template", "template.spaceapi.json") output_file = os.path.join(base_path, "public", "status.json") status_dict = { "open" : True , "closed" : False } #read door status status_json = json.load(urllib.urlopen(input_url)) door_status = status_json["door.lock"]["text"] #fill status in template sublab_status = json.load(open(json_template)) sublab_status["open"] = status_dict[door_status] #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)