-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrains.py
88 lines (69 loc) · 2.99 KB
/
trains.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
from flask import Flask, jsonify
from datetime import datetime, timedelta
auth_api_url = "http://20.244.56.144/train/auth"
train_api_url = "http://20.244.56.144/train/trains"
data = {
"companyName": "Train Central",
"clientID": "ab5767e9-9d45-4826-8ecc-9e0851673e3d",
"ownerName": "Sankarshan",
"rollNo": "2004060",
"ownerEmail": "[email protected]",
"clientSecret": "ranxGMNCcCdlEBYq"
}
app = Flask(__name__)
def get_train_details():
try:
auth_response = requests.post(auth_api_url, json=data)
if auth_response.status_code == 200:
access_token = auth_response.json().get("access_token")
print("Access Token Obtained Successfully")
else:
# Access token request failed, print the error response
print("Access Token Request Failed")
print("Error Response:")
print(auth_response.content)
return None
headers = {
"Authorization": f"Bearer {access_token}"
}
train_response = requests.get(train_api_url, headers=headers)
if train_response.status_code == 200:
# Train details fetched successfully
train_data = train_response.json()
# Filter out trains with departure time more than 30 minutes and consider delay
filtered_trains = []
for train in train_data:
departure_time = train.get("departureTime")
delayed_by = train.get("delayedBy")
if departure_time["Minutes"] <= 30 and delayed_by <= 30:
filtered_trains.append(train)
# Sort filtered_trains first in ascending order based on 'price' (AC class)
filtered_trains = sorted(filtered_trains, key=lambda x: x['price']['AC'])
# Sort the same list in descending order based on available seats for AC class
filtered_trains = sorted(filtered_trains, key=lambda x: x['seatsAvailable']['AC'], reverse=True)
# Sort the same list in descending order based on departure time
filtered_trains = sorted(filtered_trains, key=lambda x: x['departureTime']['Hours'], reverse=True)
return filtered_trains
else:
# Train details request failed, print the error response
print("Train Details Request Failed")
print("Error Response:")
print(train_response.content)
return None
except requests.exceptions.RequestException as e:
# Handle request exceptions (e.g., connection error, timeout)
print("Request Error:", e)
return None
except Exception as e:
print("Error:", e)
return None
@app.route('/trains', methods=['GET'])
def get_trains():
train_details = get_train_details()
if train_details:
return jsonify(train_details)
else:
return jsonify({"message": "Error fetching train details"}), 500
if __name__ == '__main__':
app.run(debug=True, port=3000)