|
| 1 | +import requests |
| 2 | +api_key = "<your API KEY>" |
| 3 | +base_url = "https://api.railwayapi.com/v2/pnr-status/pnr/" |
| 4 | +pnr_number = str(input("enter the PNR number")) |
| 5 | +# you'll get traceback error if you don't enter your api key |
| 6 | +complete_url = base_url + pnr_number + "/apikey/" + api_key + "/" |
| 7 | +response_ob = requests.get(complete_url) |
| 8 | +result = response_ob.json() |
| 9 | +if result["response_code"] == 200: |
| 10 | + train_name = result["train"]["name"] |
| 11 | + train_number = result["train"]["number"] |
| 12 | + from_station = result["from_station"]["name"] |
| 13 | + to_station = result["to_station"]["name"] |
| 14 | + boarding_point = result["boarding_point"]["name"] |
| 15 | + reservation_unto = result["reservation_unto"]["name"] |
| 16 | + pnr_num = result["pnr"] |
| 17 | + date_of_journey = result["doj"] |
| 18 | + total_passengers = result["total_passengers"] |
| 19 | + passengers_list = result["passengers"] |
| 20 | + chart_prepared = result["chart_prepared"] |
| 21 | + print(" train name : " + str(train_name) |
| 22 | + + "\n train number : " + str(train_number) |
| 23 | + + "\n from station : " + str(from_station) |
| 24 | + + "\n to station : " + str(to_station) |
| 25 | + + "\n boarding point : " + str(boarding_point) |
| 26 | + + "\n reservation unto : " + str(reservation_unto) |
| 27 | + + "\n pnr number : " + str(pnr_num) |
| 28 | + + "\n date of journey : " + str(date_of_journey) |
| 29 | + + "\n total no. of passengers: " + str(total_passengers) |
| 30 | + + "\n chart prepared : " + str(chart_prepared)) |
| 31 | + # for multiple passengers against same PNR |
| 32 | + for passenger in passengers_list: |
| 33 | + passenger_num = passenger["no"] |
| 34 | + current_status = passenger["current_status"] |
| 35 | + booking_status = passenger["booking_status"] |
| 36 | + # print following values |
| 37 | + print(" passenger number : " + str(passenger_num) |
| 38 | + + "\n current status : " + str(current_status) |
| 39 | + + "\n booking_status : " + str(booking_status)) |
| 40 | +else: |
| 41 | + print("Record Not Found") |
0 commit comments