-
Notifications
You must be signed in to change notification settings - Fork 3
/
ffapi-updater.py
executable file
·114 lines (99 loc) · 4.32 KB
/
ffapi-updater.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
# ------------------------------------------------------------------------------
#
# ffapi-updater - Update Freifunk API file
# Copyright (C) 2016,2019 Benjamin Schmitt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Source code available at: https://github.com/Little-Ben/ffapi-updater
#
# ------------------------------------------------------------------------------
# This python script counts all active nodes (online = true)
# and updates the node count and lastchange date (UTC) in APIFILE.
# The source is a meshviewer's nodes.json in version 2.
# ------------------------------------------------------------------------------
# Configuration:
# - APIFILE is a local file containing your API-File,
# file must be writeable for executing user
# - NODESFILE_REMOTE is a remote source
# (https://example.com/meshviewer/nodes.json)
# - NODESFILE_LOCAL is a local source
# and will only be used if NODESFILE_REMOTE is not given (empty string '')
# - SITE_CODE is the abbreviation of your community, i.e. 'ffwp'
# (it has to be given, and could be used as a filter in multi-community setups)
#
# Configuration start
APIFILE = '../api/FreifunkWestpfalz-api.json'
NODESFILE_LOCAL = '../meshviewer/nodes.json'
NODESFILE_REMOTE = ''
SITE_CODE = 'ffwp'
# Configuration end
# ------------------------------------------------------------------------------
VERSION = 'V1.2.0'
import json,requests
from pprint import pprint
from datetime import datetime
print("\nffapi-updater",VERSION,"Copyright (C) 2016,2019 Benjamin Schmitt")
print("----------------------------------------------------------------------")
print("This program comes with ABSOLUTELY NO WARRANTY.")
print("This is free software, and you are welcome to redistribute it,")
print("see LICENSE for details.")
print("Source code available at: https://github.com/Little-Ben/ffapi-updater")
print("----------------------------------------------------------------------")
with open(APIFILE) as data_file:
data = json.load(data_file)
if NODESFILE_REMOTE != "":
headers = {
'User-Agent': 'ffapi-updater by Little-Ben, ' + VERSION + ', used by: ' + SITE_CODE + ', see: https://github.com/Little-Ben/ffapi-updater'
}
r = requests.get(NODESFILE_REMOTE, headers=headers)
dataNodes = r.json()
else:
with open(NODESFILE_LOCAL) as node_file:
dataNodes = json.load(node_file)
#count data
iNodeCount=0
iNodeCountSite=0
iNodeCountFile=0
for node in dataNodes["nodes"]:
if node["nodeinfo"]["system"].get("site_code","n/a") == SITE_CODE:
if node["flags"].get("online",False) == True:
iNodeCount = iNodeCount + 1
iNodeCountSite = iNodeCountSite + 1
iNodeCountFile = iNodeCountFile + 1
#data update
print("UTC time:\t\t\t ", datetime.utcnow().strftime("%Y-%m-%dT%T.%f"))
iNodeCountOld=data["state"]["nodes"]
print("node count site/online old:\t ", str(iNodeCountOld))
data["state"]["nodes"] = iNodeCount
data["state"]["lastchange"] = datetime.utcnow().strftime("%Y-%m-%dT%T.%f")
print("node count site/online new:\t ", str(data["state"]["nodes"]))
print("node count site/all:\t\t ", str(iNodeCountSite))
print("node count file/all:\t\t ", str(iNodeCountFile))
if iNodeCountOld != iNodeCount:
#write new api file, sorted and prettyprinted - only if node count changed
with open(APIFILE, 'w') as outfile:
json.dump(data, outfile, sort_keys=True, indent=4, separators=(',', ': '))
bApiChanged=True
else:
bApiChanged=False
#write end message depending if changes happend
print("----------------------------------------------------------------------")
if bApiChanged == True:
strEndMessage="API file updated successfully."
else:
strEndMessage="API file unchanged."
print(strEndMessage, "DONE.\n")
exit(0)