-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_dump.py
57 lines (43 loc) · 1.28 KB
/
json_dump.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
import asyncio
import aiohttp
import json
from getpass import getpass
from pyhomepilot import HomePilotAPI
from pyhomepilot.auth import Auth, ConnectionError, AuthError
DEVICETYPES = {
"Actuator",
"Sensor",
"Camera",
"Transmitter"
}
FILENAME = "homepilot_dump.json"
async def async_fetch_data(auth):
data = {}
for devtype in DEVICETYPES:
data[devtype] = await auth.request("get", f"v4/devices?devtype={devtype}")
return data
def save_data(data):
with open(FILENAME, "w") as outfile:
json.dump(data, outfile)
print(f"Saved data under '{FILENAME}'.")
async def main():
data = {}
async with aiohttp.ClientSession() as session:
host = input("Hostname/IP: ")
auth = Auth(session, host)
try:
auth = Auth(session, host)
data = await async_fetch_data(auth)
except ConnectionError:
print(f"Host under '{host}' is not reachable!")
except AuthError:
try:
auth.password = getpass(prompt="Password: ")
data = await async_fetch_data(auth)
except AuthError:
print("Invalid password!")
else:
save_data(data)
else:
save_data(data)
asyncio.run(main())