Spaces:
Sleeping
Sleeping
Update app.py
Browse filesforgot to grab vehicle locations
app.py
CHANGED
|
@@ -26,7 +26,6 @@ def getRouteList():
|
|
| 26 |
'Connection': 'keep-alive',
|
| 27 |
'Upgrade-Insecure-Requests': '1'
|
| 28 |
}
|
| 29 |
-
|
| 30 |
try:
|
| 31 |
response = requests.get(url, headers=headers, timeout=10)
|
| 32 |
response.raise_for_status()
|
|
@@ -65,7 +64,6 @@ def getRouteList():
|
|
| 65 |
|
| 66 |
# Replace dash between number and name with space (e.g., "26-Dupont" -> "26 Dupont")
|
| 67 |
text = re.sub(r'(\d+)-([A-Za-z])', r'\1 \2', text)
|
| 68 |
-
if not 'Night Bus' in text: continue
|
| 69 |
|
| 70 |
routes[rid] = text
|
| 71 |
|
|
@@ -84,7 +82,6 @@ def getRouteList():
|
|
| 84 |
text = re.sub(r'(\d+)-([A-Za-z])', r'\1 \2', text)
|
| 85 |
|
| 86 |
routes[rid] = text
|
| 87 |
-
|
| 88 |
return jsonify(routes)
|
| 89 |
|
| 90 |
@app.route('/listvehiclesbyroute', methods=['POST'])
|
|
@@ -115,6 +112,9 @@ def listVehiclesByRoute():
|
|
| 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")
|
|
@@ -153,12 +153,36 @@ def listVehiclesByRoute():
|
|
| 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":
|
| 161 |
-
"destination": destination
|
|
|
|
|
|
|
| 162 |
})
|
| 163 |
|
| 164 |
return jsonify({
|
|
|
|
| 26 |
'Connection': 'keep-alive',
|
| 27 |
'Upgrade-Insecure-Requests': '1'
|
| 28 |
}
|
|
|
|
| 29 |
try:
|
| 30 |
response = requests.get(url, headers=headers, timeout=10)
|
| 31 |
response.raise_for_status()
|
|
|
|
| 64 |
|
| 65 |
# Replace dash between number and name with space (e.g., "26-Dupont" -> "26 Dupont")
|
| 66 |
text = re.sub(r'(\d+)-([A-Za-z])', r'\1 \2', text)
|
|
|
|
| 67 |
|
| 68 |
routes[rid] = text
|
| 69 |
|
|
|
|
| 82 |
text = re.sub(r'(\d+)-([A-Za-z])', r'\1 \2', text)
|
| 83 |
|
| 84 |
routes[rid] = text
|
|
|
|
| 85 |
return jsonify(routes)
|
| 86 |
|
| 87 |
@app.route('/listvehiclesbyroute', methods=['POST'])
|
|
|
|
| 112 |
return jsonify({"error": "No vehicles found"}), 404
|
| 113 |
|
| 114 |
vehicles = []
|
| 115 |
+
all_popups = soup.find_all("div", class_="leaflet-popup-content")
|
| 116 |
+
|
| 117 |
+
|
| 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")
|
|
|
|
| 153 |
# If it's mm:ss format, keep as is
|
| 154 |
|
| 155 |
delay = ("-" if late else "+") + delay_value
|
| 156 |
+
|
| 157 |
+
# Extract coordinates from JavaScript AddMarker calls
|
| 158 |
+
lat, lon = None, None
|
| 159 |
+
|
| 160 |
+
# Find all script tags and look for AddMarker calls
|
| 161 |
+
scripts = soup.find_all("script")
|
| 162 |
+
for script in scripts:
|
| 163 |
+
if script.string and "AddMarker" in script.string:
|
| 164 |
+
# Split the script content by AddMarker calls and find the one with our vehicle ID
|
| 165 |
+
add_marker_calls = script.string.split("AddMarker(")
|
| 166 |
+
for call in add_marker_calls[1:]: # Skip the first split which is before AddMarker
|
| 167 |
+
if f'"{vehicle_id}"' in call:
|
| 168 |
+
# Extract coordinates from the first part [lat, lon]
|
| 169 |
+
coords_match = re.search(r'\[([0-9\.\-]+),\s*([0-9\.\-]+)\]', call)
|
| 170 |
+
if coords_match:
|
| 171 |
+
lat, lon = float(coords_match.group(1)), float(coords_match.group(2))
|
| 172 |
+
print(f"Found coordinates for vehicle {vehicle_id}: lat={lat}, lon={lon}")
|
| 173 |
+
break
|
| 174 |
+
if lat is not None:
|
| 175 |
+
break
|
| 176 |
+
sneakygolem = direction.split(' ')
|
| 177 |
+
branch = sneakygolem[-1] if len(sneakygolem) > 1 else ""
|
| 178 |
vehicles.append({
|
| 179 |
"vehicle_id": vehicle_id,
|
| 180 |
"delay": delay,
|
| 181 |
"time": time,
|
| 182 |
+
"branch": branch,
|
| 183 |
+
"destination": destination,
|
| 184 |
+
"latitude": lat,
|
| 185 |
+
"longitude": lon
|
| 186 |
})
|
| 187 |
|
| 188 |
return jsonify({
|