This repository has been archived on 2025-07-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
circuitbodge/circuit-laundry-notifier/web.py
AKP 87987553e4
Add basic API for machine states
Signed-off-by: AKP <tom@tdpain.net>
2022-11-08 01:29:49 +00:00

24 lines
884 B
Python

import flask
from circuit_scraper import CircuitScraper, ScraperError
def new() -> flask.Flask:
app = flask.Flask(__name__)
app.add_url_rule("/api/v1/machines/<site_id>", view_func=_api_get_machines, methods=["GET"])
return app
def _api_get_machines(site_id: str):
try:
machines = CircuitScraper.get_site_machine_states(site_id)
except ScraperError:
return flask.jsonify({"ok": False, "message": "This laundry room is unavailable via CircuitView."}), 400
return flask.jsonify([machine.to_dict() for machine in sorted(
machines,
# It was annoying me that the machines weren't sorted by number. This sorts the machines by number, taking into
# account the fact that some machine numbers include letters, which are quietly ignored.
key=lambda x: int("".join(filter(lambda y: y.isdigit(), x.number)))
)])