24 lines
884 B
Python
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)))
|
|
)])
|