Skip to content

Commit 2439640

Browse files
committed
main.py: Add script
The script fetch the data from server in .json format and display the useful information. Trial API key is given for a valid time.
0 parents  commit 2439640

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

api.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"api": "c19kd5k38j"
3+
}

main.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import json
2+
import logging
3+
import os.path
4+
import requests
5+
6+
from tabulate import tabulate
7+
8+
9+
def getAPI():
10+
__dir__ = os.path.dirname(__file__)
11+
apifile = os.path.join(__dir__, 'api.json')
12+
with open(apifile, encoding='utf-8') as api_data:
13+
api = json.loads(api_data.read())
14+
15+
return api['api']
16+
17+
18+
def getData(url):
19+
try:
20+
response = requests.get(url)
21+
22+
except Exception as e:
23+
raise e
24+
25+
content = response.json()
26+
if content['response_code'] is 200:
27+
displayInfo(content)
28+
else:
29+
logging.warn("Given information is wrong...")
30+
31+
32+
def displayInfo(content):
33+
pnr = content['pnr']
34+
train_no = content['train']['number']
35+
train_name = content['train']['name']
36+
from_name = content['from_station']['name']
37+
boarding_name = content['boarding_point']['name']
38+
upto_name = content['reservation_upto']['name']
39+
journey_class = content['journey_class']['code']
40+
journey_date = content['doj']
41+
if content['chart_prepared']:
42+
chart_status = 'Yes'
43+
else:
44+
chart_status = 'No'
45+
passenger_count = content['total_passengers']
46+
status = [x for x in content['passengers']]
47+
booking_status = [status[i]['booking_status'] for i in range(len(status))]
48+
current_status = [status[i]['current_status'] for i in range(len(status))]
49+
50+
msg = ('\n*** You Queried For : PNR Number: {} ***\n'
51+
'Charting Status: {}\tNo. of Passengers: {}\n'.format(
52+
pnr, chart_status, passenger_count))
53+
table = tabulate([[train_no, train_name, journey_date, from_name,
54+
upto_name, upto_name, boarding_name, journey_class]],
55+
headers=['Train Number', 'Train Name', 'Boarding Date',
56+
'From', 'To', 'Reserved Upto', 'Boarding Point',
57+
'Class'])
58+
tablelist = [[i, booking_status[i], current_status[i]] for i \
59+
in range(passenger_count)]
60+
passenger_table = tabulate(tablelist,
61+
headers=['S. No.', 'Booking Status (Coach No ,'
62+
' Berth No., Quota)', '*Current Status'
63+
' (Coach No , Berth No.)'])
64+
print(msg)
65+
print(table)
66+
print(passenger_table)
67+
68+
69+
def main():
70+
pnr = str(input("Enter your PNR: "))
71+
if len(pnr) is not 10:
72+
logging.warn("Enter a valid PNR...")
73+
return
74+
75+
api = getAPI()
76+
url = ("https://api.railwayapi.com/v2/pnr-status/pnr/{}/apikey/"
77+
"{}".format(pnr, api))
78+
getData(url)
79+
80+
81+
if __name__ == '__main__':
82+
main()

0 commit comments

Comments
 (0)