Find Live Running Status of a train in Python
Ever been frustrated while having to open the Indian Railways website multiple times to check the status of trains? If yes, then it’s time to automate that using our favorite programming language – Python. This tutorial creates a program to find the live running status of a train in Python using the pyinrail API.
Python makes it easy to install these modules and run them on our computers. A simple ‘pip install’ command is all that is required.
Type the following in Command Prompt:
pip install pyinrail
Importing the module
In order to use this module in our application, we need to import it. This we do by adding following code at the beginning of our program.
from pyinrail import pyinrail
Create Inquiry Object
Now, to access the various functionalities of this particular module, we create an object of class Railway Inquiry in the following manner.
enq = pyinrail.RailwayEnquiry(src='kolkata', dest='mumbai', date='07-06-2020'
Here the parameters are:
src – source of journey
dest -destination of journey
date – date of journey
List of trains
Next to get the list of all the trains available under the given conditions, we use the following piece of code:
data = enq.get_trains_between_stations(as_df=True) print(data)
The as_df parameter returns the query in the form of a Python data frame which makes it very easy to read.
This is the best feature of the pyinrail API. It lets you see the seats available in a particular train without having to scroll through numerous web pages and ads. A simple command and you voila! you can start planning your trip.
data = enq.get_seat_availability(12958, classc='2A', as_df=True)
The variables are train number and coach type.
Find the Live Running Status of a train in Python
This function gives you the live status of any train i.e if it is on time, how much late it is running, and the related details, all in a concise manner.
train_detail = enq.get_train_status(12958, as_df=True)
These are all the tools you need to make your own application to determine the status of your train. Feel free to play around with this module and find the various other methods available.
What are you waiting for then? Start planning your next trip now!