From 886b9e56dc515c6536129711494f91262d402214 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 29 May 2021 22:45:38 +0200 Subject: initial commit --- bin/nvm | 71 +++++++++++++++++++++++++++++++++++++++ static/v0/css/wip.css | 78 +++++++++++++++++++++++++++++++++++++++++++ templates/departure_list.html | 34 +++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100755 bin/nvm create mode 100644 static/v0/css/wip.css create mode 100644 templates/departure_list.html diff --git a/bin/nvm b/bin/nvm new file mode 100755 index 0000000..8e506dd --- /dev/null +++ b/bin/nvm @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +# vim:tabstop=4 softtabstop=4 shiftwidth=4 textwidth=160 smarttab expandtab colorcolumn=160 +# +# Copyright (C) 2021 Daniel Friesel +# +# SPDX-License-Identifier: BSD-2-Clause + +import argparse + +import aiohttp +from aiohttp import web +from datetime import datetime +import dateutil.parser + +from jinja2 import Environment, FileSystemLoader, select_autoescape + +import json +import os + +headers = { + "Access-Control-Allow-Origin": "*", + "Content-Type": "text/html; charset=utf-8", +} + +db_rest_api = os.getenv("NVM_DB_REST_API", "https://v5.db.transport.rest") + +env = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape()) + +class Departure: + def __init__(self, obj): + self.__dict__.update(obj) + if "line" in obj: + self.line = Line(self.line) + self.when = dateutil.parser.parse(self.when) + self.plannedWhen = dateutil.parser.parse(self.plannedWhen) + +class Line: + def __init__(self, obj): + self.__dict__.update(obj) + +async def show_departure_board(request): + try: + eva = int(request.match_info.get("eva")) + except ValueError: + return web.HTTPBadRequest(text="EVA must be a number at the moment") + + async with aiohttp.ClientSession() as session: + async with session.get( + f"{db_rest_api}/stops/{eva}/departures?results=60&duration=120&stopovers=true" + ) as response: + departures = await response.text() + departures = json.loads(departures) + + if type(departures) is dict and departures.get("error", False): + return web.HTTPNotFound(body=json.dumps(departures), headers=headers) + + departures = list(map(Departure, departures)) + departure_board = env.get_template("departure_list.html") + return web.Response(body=departure_board.render(title="Noot", departures=departures), headers=headers) + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description="eva to efa gateway") + parser.add_argument("--port", type=int, metavar="PORT", default=8080) + parser.add_argument("--prefix", type=str, metavar="PATH", default="/") + args = parser.parse_args() + + app = web.Application() + app.router.add_get(f"{args.prefix}board/{{eva}}", show_departure_board) + app.router.add_static(f"{args.prefix}static", "static") + web.run_app(app, host="localhost", port=args.port) diff --git a/static/v0/css/wip.css b/static/v0/css/wip.css new file mode 100644 index 0000000..e3efc2a --- /dev/null +++ b/static/v0/css/wip.css @@ -0,0 +1,78 @@ +body { + margin: 0; + color: black; + background-color: white; +} + +html { + font-family: Sans-Serif; +} + +.container { + max-width: 60em; + margin-left: auto; + margin-right: auto; +} + +ul.departures { + position:relative; + width:100%; + + border-width:1px 2px; + margin-bottom: 5em; + + list-style-type:none; + margin:0; + padding:0; +} + +ul.departures > li { + display: block; + height: 3em; + width: 100%; + position: relative; + cursor: pointer; + border-bottom: 1px solid grey; + background-color: white; +} + +ul.departures li .line { + position: absolute; + bottom: 5px; + left: 2px; + max-width: 6em; + max-height: 3ex; + overflow: hidden; + font-size: 150%; +} + +ul.departures li .dest { + position: absolute; + top: 0.5ex; + left: 4em; + bottom: 0px; + width: 70%; + overflow: hidden; + background-color: transparent; + font-size: 200%; + color: black; +} + +ul.departures li .time { + position: absolute; + right: 5px; + top: 4px; + background-color: transparent; + font-size: 100%; + padding-left: 0.2em; + color: black; +} + +@media only screen and (max-width: 600px) { + ul.departures > li { + font-size: 85%; + } +} + +@media only screen and (min-width: 600px) { +} diff --git a/templates/departure_list.html b/templates/departure_list.html new file mode 100644 index 0000000..464951b --- /dev/null +++ b/templates/departure_list.html @@ -0,0 +1,34 @@ + + + + {{ title }} + + + + + + + + + + + + + + + +
+
+
    + {% for departure in departures %} +
  • + {{ departure.line.name }} + {{ departure.direction }} + {{ departure.when.strftime('%H:%M') }} +
  • + {% endfor %} +
+
+
+ + -- cgit v1.2.3