Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -64,6 +64,8 @@ def get_vehicle_locations_and_occupancy(vehicle_numbers):
|
|
| 64 |
return results
|
| 65 |
|
| 66 |
def get_vehicle_destinations(route, stopcall):
|
|
|
|
|
|
|
| 67 |
url = f'https://www.transsee.ca/predict?s=ttc.{route}.{stopcall}'
|
| 68 |
headers = {
|
| 69 |
'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',
|
|
@@ -78,7 +80,9 @@ def get_vehicle_destinations(route, stopcall):
|
|
| 78 |
try:
|
| 79 |
response = requests.get(url, headers=headers, timeout=10)
|
| 80 |
response.raise_for_status()
|
|
|
|
| 81 |
except Exception as e:
|
|
|
|
| 82 |
return jsonify({"error": f"Failed to fetch vehicle data: {e}"}), 500
|
| 83 |
|
| 84 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
@@ -89,13 +93,20 @@ def get_vehicle_destinations(route, stopcall):
|
|
| 89 |
dest_par = soup.find('p', id=pattern)
|
| 90 |
|
| 91 |
if dest_par:
|
|
|
|
| 92 |
contents = dest_par.contents
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
# Remove directional prefixes
|
| 97 |
destination_text = re.sub(r'^(North|South|East|West)\s+', '', destination_text, flags=re.IGNORECASE)
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
else:
|
| 100 |
vehicle_info['destination'] = []
|
| 101 |
new_pattern = re.compile(rf'^{route}_{stopcall}\d*_\d*$')
|
|
@@ -105,20 +116,25 @@ def get_vehicle_destinations(route, stopcall):
|
|
| 105 |
vehicle_link = p_tag.find('a', href="#MapMain")
|
| 106 |
if vehicle_link and vehicle_link.text.strip().isdigit():
|
| 107 |
vehicle_data['vehicle_number'] = vehicle_link.text.strip()
|
|
|
|
| 108 |
else:
|
| 109 |
# Case 2: Vehicle number is in plain text like "Vehicle 3417 Load"
|
| 110 |
text = p_tag.get_text()
|
| 111 |
match = re.search(r'Vehicle\s+(\d+)', text)
|
| 112 |
if match:
|
| 113 |
vehicle_data['vehicle_number'] = match.group(1)
|
|
|
|
| 114 |
span_element = p_tag.find('span', class_='tsfont')
|
| 115 |
if span_element:
|
| 116 |
destination_text = span_element.next_sibling.strip()
|
| 117 |
branch = span_element.previous_sibling.strip()
|
| 118 |
# Remove directional prefixes
|
| 119 |
-
destination_text = re.sub(r'^(North|South|East|West)
|
| 120 |
vehicle_data['destination'] = branch + " " + destination_text
|
|
|
|
| 121 |
vehicle_info['destination'].append(vehicle_data)
|
|
|
|
|
|
|
| 122 |
|
| 123 |
return vehicle_info
|
| 124 |
|
|
@@ -295,7 +311,7 @@ def destinations():
|
|
| 295 |
if not route:
|
| 296 |
return jsonify({'error': 'route is required'}), 400
|
| 297 |
info = get_vehicle_destinations(stopCall, route)
|
| 298 |
-
return jsonify(info)
|
| 299 |
|
| 300 |
@app.route('/test_destinations')
|
| 301 |
def test_destinations():
|
|
|
|
| 64 |
return results
|
| 65 |
|
| 66 |
def get_vehicle_destinations(route, stopcall):
|
| 67 |
+
print(f"DEBUG: Fetching vehicle destinations for route: {route}, stopcall: {stopcall}")
|
| 68 |
+
|
| 69 |
url = f'https://www.transsee.ca/predict?s=ttc.{route}.{stopcall}'
|
| 70 |
headers = {
|
| 71 |
'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',
|
|
|
|
| 80 |
try:
|
| 81 |
response = requests.get(url, headers=headers, timeout=10)
|
| 82 |
response.raise_for_status()
|
| 83 |
+
print("DEBUG: Successfully fetched data from Transsee.")
|
| 84 |
except Exception as e:
|
| 85 |
+
print(f"ERROR: Failed to fetch vehicle data: {e}")
|
| 86 |
return jsonify({"error": f"Failed to fetch vehicle data: {e}"}), 500
|
| 87 |
|
| 88 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
| 93 |
dest_par = soup.find('p', id=pattern)
|
| 94 |
|
| 95 |
if dest_par:
|
| 96 |
+
print(f"DEBUG: Found destination paragraph with id: {dest_par['id']}")
|
| 97 |
contents = dest_par.contents
|
| 98 |
+
print(f"DEBUG: Contents: {contents}")
|
| 99 |
+
if len(contents) > 1 and "going" in contents[1]:
|
| 100 |
+
stuff = contents[1].strip().split(" ")
|
| 101 |
+
if len(stuff) > 1: branch = stuff[1]
|
| 102 |
+
destination_text = dest_par.contents[3].strip()
|
| 103 |
# Remove directional prefixes
|
| 104 |
destination_text = re.sub(r'^(North|South|East|West)\s+', '', destination_text, flags=re.IGNORECASE)
|
| 105 |
+
if branch:
|
| 106 |
+
vehicle_info['destination'] = branch + " " + destination_text
|
| 107 |
+
else:
|
| 108 |
+
vehicle_info['destination'] = destination_text
|
| 109 |
+
print(f"DEBUG: Extracted destination text: {destination_text}")
|
| 110 |
else:
|
| 111 |
vehicle_info['destination'] = []
|
| 112 |
new_pattern = re.compile(rf'^{route}_{stopcall}\d*_\d*$')
|
|
|
|
| 116 |
vehicle_link = p_tag.find('a', href="#MapMain")
|
| 117 |
if vehicle_link and vehicle_link.text.strip().isdigit():
|
| 118 |
vehicle_data['vehicle_number'] = vehicle_link.text.strip()
|
| 119 |
+
print(f"DEBUG: Found vehicle number: {vehicle_data['vehicle_number']}")
|
| 120 |
else:
|
| 121 |
# Case 2: Vehicle number is in plain text like "Vehicle 3417 Load"
|
| 122 |
text = p_tag.get_text()
|
| 123 |
match = re.search(r'Vehicle\s+(\d+)', text)
|
| 124 |
if match:
|
| 125 |
vehicle_data['vehicle_number'] = match.group(1)
|
| 126 |
+
print(f"DEBUG: Extracted vehicle number: {vehicle_data['vehicle_number']}")
|
| 127 |
span_element = p_tag.find('span', class_='tsfont')
|
| 128 |
if span_element:
|
| 129 |
destination_text = span_element.next_sibling.strip()
|
| 130 |
branch = span_element.previous_sibling.strip()
|
| 131 |
# Remove directional prefixes
|
| 132 |
+
destination_text = re.sub(r'^(North|South|East|West)\s+', '', destination_text, flags=re.IGNORECASE)
|
| 133 |
vehicle_data['destination'] = branch + " " + destination_text
|
| 134 |
+
print(f"DEBUG: Extracted destination: {vehicle_data['destination']}")
|
| 135 |
vehicle_info['destination'].append(vehicle_data)
|
| 136 |
+
else:
|
| 137 |
+
print("WARNING: No destination paragraph found.")
|
| 138 |
|
| 139 |
return vehicle_info
|
| 140 |
|
|
|
|
| 311 |
if not route:
|
| 312 |
return jsonify({'error': 'route is required'}), 400
|
| 313 |
info = get_vehicle_destinations(stopCall, route)
|
| 314 |
+
return jsonify({'vehicles': info})
|
| 315 |
|
| 316 |
@app.route('/test_destinations')
|
| 317 |
def test_destinations():
|