From 4b5bad262c11b1410a88f82f65dd68f19a7f2e46 Mon Sep 17 00:00:00 2001 From: kantorkel Date: Tue, 1 Dec 2015 21:44:45 +0100 Subject: [PATCH] =?UTF-8?q?node=5Fnumber.py=20fuer=20meta.hamburg.freifunk?= =?UTF-8?q?.net=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node_number.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 node_number.py diff --git a/node_number.py b/node_number.py new file mode 100644 index 0000000..b965a01 --- /dev/null +++ b/node_number.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +#Bibliotheken importieren +import time +import datetime +import json + +#Datei oeffnen +f = open('/var/www/meshviewer/nodelist.json') + +#JSON einlesen +data = json.load(f) + +#Nodes attribut aussortieren +nodes = data['nodes'] + +#Zaehler mit Wert 0 anlegen +num_nodes = 0 + +#Fuer jeden Knoten in nodes +for node in nodes: + #Status Attribut aussortieren + status = node['status'] + + #Wenn der Status online entaehlt, hochzaehlen + if status['online']: + num_nodes += 1 + +#Zeit holen +thetime = datetime.datetime.now().isoformat() + +ffhh = None + +#Freifunk API-Datei einladen und JSON lesen +with open('/var/www/meta/ffhh.json', 'r') as fp: + ffhh = json.load(fp) + +#Attribute Zeitstempel und Knotenanzahl setzen +ffhh['state']['lastchange'] = thetime +ffhh['state']['nodes'] = num_nodes + +#Freifunk API-Datein mit geaenderten werten schreiben +with open('/var/www/meta/ffhh.json', 'w') as fp: + json.dump(ffhh, fp, indent=2, separators=(',', ': '))