69 lines
4.6 KiB
Python
69 lines
4.6 KiB
Python
from datetime import datetime
|
|
import requests, json
|
|
# nr wyjazdowe z pkp pdf
|
|
trip_nr_list = [59400, 59402, 59404, 59406, 59408, 59410, 59412, 59414, 59416, 59418, 59420, 59422, 59424, 59426, 59428, 59430, 59432, 59434, 59436, 59438, 59440, 59442, 59444, 59446, 59448, 59450, 59452, 59454, 59456, 59458, 59460, 59462, 59464, 59466, 59468, 59470, 59472, 59474, 59476, 59478, 59480, 59482, 59484, 59486, 59600, 59602, 59604, 59606, 59608, 59610, 59612, 59620, 59690, 59692, 59694, 59696, 59698, 59702, 59706, 59708, 59710, 59712, 59714, 59716, 59718, 59720, 59722, 59724, 59726, 59728, 59730, 59732, 59734, 59736, 59738, 59740, 59742, 59744, 59746, 59748, 59750, 59752, 59754, 59756, 59758, 59760, 59762, 59764, 59766, 59768, 59770, 59772, 59774, 59776, 59778, 59780, 59782, 59784, 59786, 59788, 59790, 59792, 59794, 59796, 59798, 59800, 59802, 59804, 59806, 59808, 59810, 59812, 59814, 95401, 95403, 95405, 95407, 95409, 95411, 95413, 95415, 95417, 95419, 95421, 95423, 95425, 95427, 95429, 95431, 95433, 95435, 95437, 95439, 95441, 95443, 95445, 95447, 95449, 95451, 95453, 95455, 95457, 95459, 95461, 95463, 95465, 95467, 95469, 95471, 95473, 95475, 95477, 95479, 95481, 95483, 95485, 95487, 95489, 95601, 95603, 95605, 95607, 95609, 95633, 95687, 95689, 95691, 95693, 95695, 95697, 95699, 95703, 95707, 95709, 95711, 95713, 95715, 95717, 95719, 95721, 95723, 95725, 95727, 95729, 95731, 95733, 95735, 95737, 95739, 95741, 95743, 95745, 95747, 95749, 95751, 95753, 95755, 95757, 95759, 95761, 95763, 95765, 95767, 95769, 95771, 95773, 95775, 95777, 95779, 95781, 95783, 95785, 95787, 95789, 95791, 95793, 95795, 95797, 95799, 95801, 95803, 95805, 95807, 95809, 95811, 95813, 95815, 95817]
|
|
trip_nr_list = [59400, 59402, 59486, 59600, 59602, 59604] # shor for testing
|
|
|
|
|
|
def get_trips(date, trip_nr_list): # Date Y-M-D datetime.now().strftime("%Y-%m-%d")
|
|
|
|
trip_id_list = []
|
|
for i in trip_nr_list:
|
|
try: # get the train id using the train number, except KeyError as the given journey may not have a schedule for a given day (mostly sundays)
|
|
trip_id_list.append([i, requests.get(f'https://koleo.pl/pl/train_calendars?&brand=SKMT&nr={i}').json()["train_calendars"][0]["date_train_map"][date]])
|
|
except KeyError: print('no date for this train')
|
|
# trip_id_list = [ (59400,148352609), (59400,148352609) ]
|
|
all_trips_list = []
|
|
for i in trip_id_list:
|
|
one_trip = [i]
|
|
for i in requests.get(f'https://koleo.pl/pl/trains/{i[1]}').json()["stops"]:
|
|
one_trip.append(f'{i["station_name"]} {i["arrival"]["hour"]}:{i["arrival"]["minute"]} {i["departure"]["hour"]}:{i["departure"]["minute"]}')
|
|
all_trips_list.append(one_trip)
|
|
|
|
return all_trips_list
|
|
|
|
print(get_trips(datetime.now().strftime("%Y-%m-%d"),trip_nr_list))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
def get_live_trains(date, trip_nr_list): # Date Y-M-D
|
|
train_id = ''
|
|
all_journeys = []
|
|
|
|
for i in train_number_list: # For each train number
|
|
|
|
#getting the train id using the train number and current date by parsing the json response from the koleo.pl server
|
|
# request json data train numer = i parse the data - get the train ids
|
|
train_id = requests.get(f'https://koleo.pl/pl/train_calendars?&brand=SKMT&nr={i}').json()["train_calendars"][0]["date_train_map"][date]
|
|
all_stops = requests.get(f'https://koleo.pl/pl/trains/{train_id}').json()["stops"] # we turned the train number into a train id, now we are requesting the train ride data using the train id from koleo.pl
|
|
journey = [i]
|
|
|
|
arrival = (all_stops[0]["arrival"]["hour"], all_stops[0]["arrival"]["minute"]) # get the arrival time from the first station (begining)
|
|
departure = (all_stops[-1]["departure"]["hour"],all_stops[-1]["departure"]["minute"]) # get the departure time from the last station (end)
|
|
|
|
if arrival <= (datetime.now().time().hour,datetime.now().time().minute) <= departure: # check if the train is currently in a journey
|
|
for i in all_stops:
|
|
journey.append(f'{i["station_name"]} {i["arrival"]["hour"]}:{i["arrival"]["minute"]} {i["departure"]["hour"]}:{i["departure"]["minute"]}')
|
|
|
|
all_journeys.append(journey)
|
|
return all_journeys
|
|
|
|
with open("LIVETRAIN.json", "w", encoding="utf-8") as file:
|
|
json.dump([x for x in get_live_trains(datetime.now().strftime("%Y-%m-%d"),wyjazd)], file, indent=4, ensure_ascii=False)
|
|
|
|
print('DONE')
|
|
|
|
|
|
#get_trains(datetime.now().strftime("%Y-%m-%d"),wyjazd):
|
|
#print(f'https://koleo.pl/pociag/SKMT/{i}/{current_date}')
|
|
|
|
''' |