42Cummer commited on
Commit
f034e6c
·
verified ·
1 Parent(s): 206377a

Update app.py

Browse files

Added new API method to get vehicles by route

Files changed (1) hide show
  1. app.py +78 -0
app.py CHANGED
@@ -87,6 +87,84 @@ def getRouteList():
87
 
88
  return jsonify(routes)
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  @app.route('/', methods=['GET'])
92
  def health_check():
 
87
 
88
  return jsonify(routes)
89
 
90
+ @app.route('/listvehiclesbyroute', methods=['POST'])
91
+ def listVehiclesByRoute():
92
+ data = request.get_json()
93
+ if not data or "route" not in data:
94
+ return jsonify({"error": "Missing route parameter"}), 400
95
+ route = data["route"]
96
+ url = f"https://www.transsee.ca/routeveh?a=ttc&r={route}"
97
+ headers = {
98
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
99
+ 'Referer': 'https://www.transsee.ca/',
100
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
101
+ 'Accept-Language': 'en-US,en;q=0.9',
102
+ 'DNT': '1',
103
+ 'Connection': 'keep-alive',
104
+ 'Upgrade-Insecure-Requests': '1'
105
+ }
106
+ try:
107
+ response = requests.get(url, headers=headers, timeout=10)
108
+ response.raise_for_status()
109
+ except Exception as e:
110
+ return jsonify({"error": f"Failed to fetch route list: {e}"}), 500
111
+
112
+ soup = BeautifulSoup(response.text, "html.parser")
113
+ core_div = soup.find("div", class_="core")
114
+ if not core_div:
115
+ return jsonify({"error": "No vehicles found"}), 404
116
+
117
+ vehicles = []
118
+ for p in core_div.find_all("p", id=True):
119
+ vehicle_id = p.find("a").get_text(strip=True) if p.find("a") else None
120
+ spans = p.find_all("span")
121
+ delay = None
122
+ if spans:
123
+ first_text = spans[0].get_text(strip=True).lower()
124
+ if "waiting" in first_text and len(spans) > 1:
125
+ delay = spans[1].get_text(strip=True)
126
+ else:
127
+ delay = spans[0].get_text(strip=True)
128
+
129
+ time = p.find("time").get_text(strip=True) if p.find("time") else None
130
+ direction = None
131
+ destination = None
132
+
133
+ for part in p.stripped_strings:
134
+ if part.startswith("going"):
135
+ direction = part
136
+ elif "to " in part: # destination line
137
+ destination = part
138
+
139
+ late = delay.split(' ')[-1].lower() == 'behind'
140
+ delay_value = delay.split(' ')[0]
141
+
142
+ # Convert delay from hh:mm:ss to mm:ss format
143
+ if ":" in delay_value:
144
+ parts = delay_value.split(":")
145
+ if len(parts) == 3: # hh:mm:ss format
146
+ try:
147
+ h, m, s = parts
148
+ # Convert to mm:ss by adding hours to minutes
149
+ total_minutes = int(h) * 60 + int(m)
150
+ delay_value = f"{total_minutes}:{s}"
151
+ except:
152
+ pass
153
+ # If it's mm:ss format, keep as is
154
+
155
+ delay = ("-" if late else "+") + delay_value
156
+ vehicles.append({
157
+ "vehicle_id": vehicle_id,
158
+ "delay": delay,
159
+ "time": time,
160
+ "branch": direction.split(' ')[-1],
161
+ "destination": destination
162
+ })
163
+
164
+ return jsonify({
165
+ "route": route,
166
+ "vehicles": vehicles
167
+ })
168
 
169
  @app.route('/', methods=['GET'])
170
  def health_check():