[Broken] first try at train fetch, wrong http requests

This commit is contained in:
Debucquoy Anthony 2022-08-31 00:08:54 +02:00
parent ac23c63ae5
commit cefd3dc910
Signed by: tonitch
GPG Key ID: A78D6421F083D42E

28
main.py
View File

@ -17,11 +17,16 @@ async def main():
trains = [] trains = []
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
tasks = [] tasks = []
for station in stations: for station in stations[:10]:
tasks.append(fetch_station(station, session, trains)) tasks.append(fetch_station(station, session, trains))
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
print(trains) 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: 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: except aiohttp.client_exceptions.ServerDisconnectedError:
print(f"station {station} failed ❎ ") 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__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())