-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
229 lines (182 loc) · 6.4 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from bs4 import BeautifulSoup
from flask import Flask, request, jsonify
from time import sleep
#import csv
import requests
import threading
import json
from datetime import datetime
from io import StringIO
class individualPrinter(object):
"""
class to store the information from a single printer
it's not much more than a way to structure our data
"""
def __init__(self, ip):
self.ipAddress = ip
self.inkStatus = None
self.paperSupply = None
self.productName = None
self.deviceName = None
self.deviceLocation = None
self.numRecentTickets = None
try:
self.indexResponse = requests.get('http://' + str(self.ipAddress), verify=False, timeout=3.00)
self.deviceResponse = requests.get('http://' + str(self.ipAddress) + '/hp/device/DeviceInformation/View',verify=False, timeout=3.00)
noAuthenticatedPrinters = [
"10.30.10.104",
"10.30.10.113",
"10.30.10.122",
"10.30.10.225",
"10.30.10.32",
"10.30.10.85",
"10.30.10.93"]
if self.ipAddress in noAuthenticatedPrinters:
self.jobResponse = requests.get("http://" + str(self.ipAddress) + "/hp/device/JobLogReport/Index", verify=False, timeout=3.00)
if self.jobResponse.status_code is not 200:
print('Request Failed: ' + str(self.jobResponse.status_code) + ' from ' + self.jobResponse.url)
if self.indexResponse.status_code is not 200:
print('Request Failed: ' + str(self.indexResponse.status_code) + ' from ' + self.indexResponse.url)
if self.deviceResponse.status_code is not 200:
print('Request Failed: ' + str(self.deviceResponse.status_code) + ' from ' + self.deviceResponse.url)
else:
print("Request Succeeded")
self.parseResponse()
except:
pass
"""
\todo add extra job log status checking
"""
def parseResponse(self):
#print('hi')
indexSoup = BeautifulSoup(self.indexResponse.text, "lxml")
self.inkStatus = str(indexSoup.find("span", {"id": "SupplyPLR0"})).split(">")[1].split("%")[0]
deviceSoup = BeautifulSoup(self.deviceResponse.text, "lxml")
self.productName = deviceSoup.find("p", {"id": "ProductName"}).string
self.deviceName = deviceSoup.find("p", {"id": "DeviceName"}).string
self.deviceLocation = deviceSoup.find("p", {"id": "DeviceLocation"}).string
self.inkStatus = int(str(indexSoup.find("span", {"id": "SupplyPLR0"})).split(">")[1].split("%")[0])
isFull = None
sheetCapacity = None
totalCount = 0
table = str(indexSoup.find("table", {"id": "MediaTable"}).tbody)
for entry in table.split("span class="):
if "status" in entry:
isFull = "status-full" in entry
sheetCapacity = entry.split("td")[2].split(">")[-1][:-2]
if isFull and sheetCapacity:
totalCount = totalCount + int(sheetCapacity.split(" ")[0])
self.paperSupply = totalCount
#print(self.inkStatus, self.paperSupply)
noAuthenticatedPrinters = [
"10.30.10.104",
"10.30.10.113",
"10.30.10.122",
"10.30.10.225",
"10.30.10.32",
"10.30.10.85",
"10.30.10.93"]
if self.ipAddress in noAuthenticatedPrinters:
jobSoup = BeautifulSoup(self.jobResponse.text, "lxml")
name = "JobLogName_"
user = "JobLogUser_"
status = "JobLogStatus_"
date = "JobLogData_"
for line in self.jobResponse.text:
if "PrintJobTicket" in line:
print(line)
numTickets = self.jobResponse.text.count('PrintJobTicket')
if numTickets == 0:
self.numRecentTickets = None
numRecentTickets = 0
for i in range(0, numTickets):
time = jobSoup.find("td", {"id": date + str(i)}).string
if len(time) < 22:
print("bleh")
time = time.split(" ")[0] + " 0" + time.split(" ")[1] + " " + time.split(" ")[2]
print(int(datetime.strptime(time, "%m/%d/%Y %I:%M:%S %p").now().strftime("%s")))
print(int(datetime.now().strftime("%s")))
if True or int(datetime.strptime(time, "%m/%d/%Y %I:%M:%S %p").now().strftime("%s")) * 1000 + 1800 > int(datetime.now().strftime("%s")):
self.numRecentTickets = numRecentTickets + 1
class printerStatus(object):
"""
A class to store the information from all printers on campus
not much more than a dictionary of individual printers
And a function to go through the list and update our information
"""
def __init__(self, printerListFile):
self.printerDicts = {}
self.printerInfoDict = {}
with open(printerListFile, 'r') as fil:
red = fil.read()
#print(red)
red = red.replace('printerList = [', '{"printerList":[') + "}"
io = StringIO(red)
data = json.load(io)["printerList"]
#print(data)
for item in data:
self.printerDicts[item["IPAddress"]] = item
def query(self):
for key in self.printerDicts.keys():
#print(key)
printer = individualPrinter(key)
self.printerInfoDict[key] = printer
def worker():
"""
An extra worker to do all the printer data collection
Intended to be used as a separate thread
"""
while(True):
allPrinterStatuses = printerStatus("static/data/PrinterList3.json")
allPrinterStatuses.query()
sleep(60)
class Seat(object):
"""
class to store the information from a single printer
it's not much more than a way to structure our data
"""
def __init__(self, seatID):
self.seatID = seatID
self.available = False
app = Flask(__name__, static_url_path="")
allPrinterStatuses = printerStatus("static/data/PrinterList3.json")
allPrinterStatuses.query()
seats = []
seats.append(Seat(0))
seats.append(Seat(1))
seats.append(Seat(2))
@app.route("/")
def run_app():
return "Hello, world"
@app.route("/api/v1/printers", methods=['GET'])
def get_printers():
printers = {}
for key in allPrinterStatuses.printerInfoDict:
printers[key] ={
'deviceName': allPrinterStatuses.printerInfoDict[key].deviceName,
'inkStatus': allPrinterStatuses.printerInfoDict[key].inkStatus,
'productName': allPrinterStatuses.printerInfoDict[key].productName,
'deviceLocation': allPrinterStatuses.printerInfoDict[key].deviceLocation,
'paperSupply': allPrinterStatuses.printerInfoDict[key].paperSupply,
}
return jsonify({'printers': printers})
@app.route("/api/v1/seats/<int:seat_id>", methods=['POST'])
def post_seat(seat_id):
print(request.json)
seats[seat_id].available = request.json['available']
return 'Done', 201
@app.route("/api/v1/seats", methods=['GET'])
def get_seats():
seat_json = []
for seat in seats:
seat_json.append({
'seatID': seat.seatID,
'available': seat.available
})
return jsonify({'seats': seat_json})
@app.route("/<path:path>")
def status_file(path):
return app.send_static_file(path)
threads = []
t = threading.Thread(target=worker)
t.start()