-
Notifications
You must be signed in to change notification settings - Fork 21
/
apireader.py
executable file
·65 lines (47 loc) · 1.82 KB
/
apireader.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
#!/usr/bin/env python3
import sys
import os.path
import requests
import json
from argparse import ArgumentParser
DUMP_NAME = "api_data.dump"
DIRECTORY_URI = "https://raw.githubusercontent.com/freifunk/directory.api.freifunk.net/master/directory.json"
def get_api_dict():
response = requests.get(DIRECTORY_URI)
api_dict = json.loads(response.text)
return api_dict
def get_api_data_from_git(api_dict):
data = dict()
for community in api_dict:
api_url = api_dict[community]
print("Loading {:<20} {} ...".format(community, api_url), file=sys.stderr)
try:
response = requests.get(api_url, timeout=2)
community_api = response.json()
data[community] = community_api
except (requests.exceptions.RequestException, ValueError):
print("Error: Couldn't load api file for {} from {}.".format(community, api_url), file=sys.stderr)
return data
def get_api_data_from_file(filename):
with open(filename, 'r') as dump_file:
api_data = json.load(dump_file)
return api_data
def get_api_data(api_dict):
if os.path.isfile("api_data.dump"):
return get_api_data_from_file(DUMP_NAME)
else:
return get_api_data_from_git(api_dict)
def dump_api_data(dest):
api_dict = get_api_dict()
api_data = get_api_data_from_git(api_dict)
with open(dest, "w") as dump_file:
json.dump(api_data, dump_file)
if __name__ == "__main__":
PARSER = ArgumentParser(description="Dump freifunk-api data from all communities.")
PARSER.add_argument("--dest",
metavar="FILE", dest="dest",
default=DUMP_NAME,
help="Filename of the data dump. Default: " +
DUMP_NAME)
ARGS = PARSER.parse_args()
dump_api_data(ARGS.dest)