ticketfrei4/src/ticketfrei3/__init__.py

27 lines
938 B
Python

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()
def main() -> None:
asyncio.run(asyncmain(), debug=True)