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()