-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchargy.py
executable file
·80 lines (63 loc) · 1.79 KB
/
chargy.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
#!/usr/bin/env python3
import argparse
import json
import os
import re
import requests
import xmltodict
import yaml
from pprint import pprint
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.yml', 'r') as config_data:
api = yaml.load(config_data, yaml.BaseLoader)['api']['charging_stations']
parser = argparse.ArgumentParser(description='Chargy station statuses.')
parser.add_argument('-t', '--total', action='store_true', help='only return totals')
args = parser.parse_args()
def get_chargy_stations():
raw = xmltodict.parse(
requests.get(api['chargy']).text,
dict_constructor=dict
)
return raw['kml']['Document']['Placemark']
def get_chargy_connectors():
connectors = []
total = {
'available': 0,
'charging': 0,
'finishing': 0,
'offline': 0,
'unavailable': 0,
'preparing': 0,
}
for station in get_chargy_stations():
location = {
'address': station['address'],
'coordinates': list(map(float, station['Point']['coordinates'].split(',')))
}
for chg_device in station['ExtendedData']['Data']:
if chg_device['@name'] != 'chargingdevice':
continue
for raw_connector in json.loads(chg_device['value'])['connectors']:
status = raw_connector['description'].lower()
connector = {
'id': int(raw_connector['id']),
'name': raw_connector['name'],
'status': status,
'chg_speed': raw_connector['maxchspeed'],
'location': location
}
total[status] += 1
connectors.append(connector)
if args.total:
return total
return {
'total': total,
'connectors': connectors
}
def get_charging_stations():
return (
get_chargy_connectors()
)
if __name__ == '__main__':
print(
json.dumps(get_charging_stations())
)