refactor and fetch trips and stop times

master
0x90.space 2021-12-29 01:01:13 +01:00
parent f32047b243
commit c09d8a64f4
5 changed files with 97 additions and 23 deletions

View File

@ -23,6 +23,7 @@ install_requires =
aiosqlite
blacksheep
tortoise-orm
uvicorn
[options.packages.find]
where = src

View File

@ -1,26 +1,5 @@
import asyncio
from blacksheep.client import ClientSession
import json
from ticketfrei3.models import Haltestelle
from tortoise import Tortoise
async def asyncmain() -> None:
await Tortoise.init(
db_url="sqlite://db.sqlite3", modules={"models": ["ticketfrei3.models"]}
)
await Tortoise.generate_schemas()
async with ClientSession(connection_timeout=30) as client:
response = await client.get("https://start.vag.de/dm/api/haltestellen.json/vag")
haltestellen = json.loads(await response.text())
for haltestelle in haltestellen["Haltestellen"]:
if not await Haltestelle.exists(id=haltestelle["VGNKennung"]):
await Haltestelle.create(
id=haltestelle["VGNKennung"],
name=haltestelle["Haltestellenname"],
)
await Tortoise.close_connections()
import uvicorn
def main() -> None:
asyncio.run(asyncmain(), debug=True)
uvicorn.run("ticketfrei3.application:app")

View File

@ -0,0 +1,76 @@
from blacksheep.client import ClientSession
from blacksheep.server import Application
import json
from ticketfrei3.models import Fahrt, Halt, Haltestelle
from tortoise import Tortoise
from tortoise.transactions import in_transaction
app = Application()
async def init(app: Application) -> None:
await Tortoise.init(
db_url="sqlite://db.sqlite3", modules={"models": ["ticketfrei3.models"]}
)
await Tortoise.generate_schemas()
app.services.add_instance(ClientSession(connection_timeout=30))
app.on_start += init
async def finalize(app: Application) -> None:
client = app.service_provider.get(ClientSession)
await client.close()
await Tortoise.close_connections()
app.on_stop += finalize
@app.route("/fetch_haltestellen")
async def fetch_haltestellen(client: ClientSession) -> None:
response = await client.get("https://start.vag.de/dm/api/v1/haltestellen.json/vag")
haltestellen = json.loads(await response.text())
for haltestelle in haltestellen["Haltestellen"]:
if await Haltestelle.exists(id=haltestelle["VGNKennung"]):
continue
await Haltestelle.create(
id=haltestelle["VGNKennung"],
name=haltestelle["Haltestellenname"],
longitude=haltestelle.get("Longitude"),
latitude=haltestelle.get("Latitude"),
)
@app.route("/fetch_fahrten")
async def fetch_fahrten(client: ClientSession) -> None:
for zweig in ("bus", "tram", "ubahn"):
response = await client.get(
"https://start.vag.de/dm/api/v1/fahrten.json/%s?timespan=120" % zweig
)
fahrten = json.loads(await response.text())
for fahrt in fahrten["Fahrten"]:
if await Fahrt.exists(id=fahrt["Fahrtnummer"]):
continue
async with in_transaction() as connection:
fahrt = Fahrt(id=fahrt["Fahrtnummer"], linie=fahrt["Linienname"])
await fahrt.save(using_db=connection)
response = await client.get(
"https://start.vag.de/dm/api/v1/fahrten.json/%s/%d"
% (zweig, fahrt.id)
)
details = json.loads(await response.text())
for halt in details["Fahrtverlauf"]:
haltestelle = (
await Haltestelle.filter(id=halt["VGNKennung"])
.using_db(connection)
.first()
)
await Halt(
fahrt=fahrt,
haltestelle=haltestelle,
ankunft=halt.get("AnkunftszeitIst"),
abfahrt=halt.get("AbfahrtszeitIst"),
).save(using_db=connection)

View File

@ -2,6 +2,23 @@ from tortoise.models import Model
from tortoise import fields
class Fahrt(Model):
id = fields.IntField(pk=True)
linie = fields.TextField()
verlauf: fields.ReverseRelation["Halt"]
class Halt(Model):
id = fields.IntField(pk=True)
fahrt = fields.ForeignKeyField("models.Fahrt", "verlauf")
haltestelle = fields.ForeignKeyField("models.Haltestelle", "fahrten")
ankunft = fields.DatetimeField(null=True)
abfahrt = fields.DatetimeField(null=True)
class Haltestelle(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
longitude = fields.FloatField(null=True)
latitude = fields.FloatField(null=True)
fahrten: fields.ReverseRelation["Halt"]

1
src/uvicorn/__init__.pyi Normal file
View File

@ -0,0 +1 @@
def run(app: str) -> None: ...