Created
September 3, 2021 16:24
-
-
Save derf/8301359f1c1806f3248007e09621a5ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import aiohttp | |
| import asyncio | |
| from datetime import datetime | |
| import json | |
| class EFA: | |
| def __init__(self, url, proximity_search=False): | |
| self.dm_url = url + "/XML_DM_REQUEST" | |
| self.dm_post_data = { | |
| "language": "de", | |
| "mode": "direct", | |
| "outputFormat": "JSON", | |
| "type_dm": "stop", | |
| "useProxFootSearch": "0", | |
| "useRealtime": "1", | |
| } | |
| if proximity_search: | |
| self.dm_post_data["useProxFootSearch"] = "1" | |
| async def get_departures(self, place, name, ts): | |
| self.dm_post_data.update( | |
| { | |
| "itdDateDay": ts.day, | |
| "itdDateMonth": ts.month, | |
| "itdDateYear": ts.year, | |
| "itdTimeHour": ts.hour, | |
| "itdTimeMinute": ts.minute, | |
| "name_dm": name, | |
| } | |
| ) | |
| if place is None: | |
| self.dm_post_data.pop("place_dm", None) | |
| else: | |
| self.dm_post_data.update({"place_dm": place}) | |
| departures = list() | |
| async with aiohttp.ClientSession() as session: | |
| async with session.post(self.dm_url, data=self.dm_post_data) as response: | |
| # EFA may return JSON with a text/html Content-Type, which response.json() does not like. | |
| departures = json.loads(await response.text()) | |
| return departures | |
| async def main(): | |
| now = datetime.now() | |
| departures = await EFA("https://efa.vrr.de/standard/").get_departures( | |
| "Essen", "Hbf", now | |
| ) | |
| print(json.dumps(departures)) | |
| if __name__ == "__main__": | |
| asyncio.get_event_loop().run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment