forked from Deliagwath/OctoMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
45 lines (39 loc) · 1.27 KB
/
app.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
from flask import Flask, render_template
import datetime
import json
import requests
app = Flask(__name__)
printers = {}
credentials = []
responses = {}
with open('credentials.txt') as file:
credentials = file.readlines()
for credential in credentials:
if credential == '': continue
[hostname, api_key] = credential.split()
url = f'https://{hostname.strip()}.nolop.org/api/job'
printers[hostname] = (url, api_key.strip())
print(printers)
@app.route('/')
def check_status():
print(printers)
for (url, api_key) in printers.values():
print(url)
try:
req = requests.get(url, headers={"X-Api-Key": api_key}, timeout=10)
except requests.exceptions.ConnectionError:
print('Unable to connect to ' + url)
print()
continue
except Exception as ex:
print(f'Unexpected Error: {ex}')
print()
continue
response = json.loads(req.text)
print(response)
if "state" not in response:
print(f'Unexpected Response: {response}')
print()
continue
responses['p' + ''.join(filter( lambda x: x in '0123456789', url ))] = response
return render_template('index.html', printers = printers, responses = responses)