diff --git a/main.py b/main.py index 89b3d0f..b2f469b 100644 --- a/main.py +++ b/main.py @@ -17,11 +17,16 @@ async def main(): trains = [] async with aiohttp.ClientSession() as session: tasks = [] - for station in stations: + for station in stations[:10]: tasks.append(fetch_station(station, session, trains)) await asyncio.gather(*tasks) print(trains) + async with aiohttp.ClientSession() as session: + tasks = [] + for train in trains[:5]: + tasks.append(fetch_train(train, session)) + await asyncio.gather(*tasks) async def fetch_station(station: str, session: aiohttp.ClientSession, trains: list) -> None: @@ -59,6 +64,27 @@ async def fetch_station(station: str, session: aiohttp.ClientSession, trains: li except aiohttp.client_exceptions.ServerDisconnectedError: print(f"station {station} failed ❎ ") +async def fetch_train(train:str, session: aiohttp.ClientSession): + url = "http://www.belgianrail.be/jp/nmbs-realtime/trainsearch.exe/fn?ld=std&" + url_header = {'User-Agent': 'python script to fetch trains of belgium. (d.tonitch@gmail.com - in case it is a problem)', + 'From': 'd.tonitch@gmail.com'} + url_data = { + 'trainname':train.replace(" ", "+"), + 'selectDate':'oneday', + 'date':datetime.now().strftime('%d/%m/%Y'), + 'productClassFilter':'85', + 'start':'Chercher' + } + try: + async with session.post(url, data=url_data, headers=url_header) as resp: + if resp.status == 200: + print(f"train {train} success ✅") + soup = BeautifulSoup(await resp.text(), 'html.parser') + products = soup.body.find_all("td") + print(soup.body.prettify()) + except aiohttp.client_exceptions.ServerDisconnectedError: + print(f"train {train} failed ❎ ") + if __name__ == "__main__": asyncio.run(main())