Created
September 19, 2019 17:46
-
-
Save msafadieh/4dee7eecaae857aad5047609c47219e3 to your computer and use it in GitHub Desktop.
Checks the paper status of campus printers by IP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
import re | |
from sys import argv | |
status = {"0": "Empty", "1": "Low", "2": "Normal", "3": "Full"} | |
data = { | |
"uri":"/rps/", | |
"userID":"", | |
"password":"" | |
} | |
def get_tray_levels(ip_address): | |
home_url = ip_address if ip_address.startswith("http://") else "http://" + ip_address | |
form = requests.get(home_url).text | |
action_url = re.findall(r"URL=(http(?:s?)\:\/\/.+)\/rps\/", form)[0] | |
url = action_url + "/login" | |
resp = requests.post(url, data=data, verify=False).text | |
trays_raw = re.findall(r"var cstInfo \= (\{.+\});\n", resp)[0] | |
trays = json.loads(trays_raw) | |
for tray in sorted(trays.values(), key=lambda t: t["cstName"]): | |
print(tray["cstName"] + ": " + status.get(tray["remainPapVol"], "Unknown status")) | |
if __name__ == "__main__": | |
if len(argv) < 2: | |
exit("No IP provided.") | |
get_tray_levels(argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment