mqtt-topic-exporter/node_api.py
2019-07-19 01:06:00 +02:00

24 lines
716 B
Python

import urllib.request, urllib.parse, json
nodes = {}
class NodeAPI:
def get(self, node):
if node in nodes:
return nodes[node]
try:
with urllib.request.urlopen("http://logistics.dev.uctrl.net/api/nodes/"+node) as url:
data = json.loads(url.read().decode())
response = {
'title': data['data']['description'] if data['data']['description'] is not None else data['data']['project']['title'],
'location': data['data']['location']
}
nodes[node] = response['title']
return nodes[node]
except urllib.error.HTTPError:
return node.capitalize()