-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.py
42 lines (36 loc) · 1.26 KB
/
monitor.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
import datetime
import json
import requests
printers: dict = {}
credentials: list[str]
with open('credentials') as file:
credentials = file.readlines()
for credential in credentials:
if credential == "": continue
[base_url, api_key] = credential.split()
url = f'https://{base_url.strip()}/api/job'
printers[url] = api_key.strip()
for url, api_key in printers.items():
print(f'Printer: {url}')
try:
req = requests.get(url, headers={"X-Api-Key": api_key})
except requests.exceptions.ConnectionError:
print('Unable to connect')
print()
continue
except Exception as ex:
print(f'Unexpected Error: {ex}')
print()
continue
response = json.loads(req.text)
if "state" not in response or "progress" not in response:
print(f'Unexpected Response: {response}')
print()
continue
print(response["state"])
progress = response["progress"]
if progress["completion"] is None: print('Progress: N/A')
else: print(f'Progress: {progress["completion"]:.2f}%')
if progress["printTimeLeft"] is None: print('Time left: N/A')
else: print(f'Time left: {datetime.timedelta(seconds=progress["printTimeLeft"])}')
print()