Spaces:
Paused
Paused
Update app.py
Browse filescopied from github
app.py
CHANGED
|
@@ -19,7 +19,21 @@ reader = easyocr.Reader(
|
|
| 19 |
user_network_directory='/tmp/.EasyOCR' # ✅ Also tell it where user models are
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def get_vehicle_locations_and_occupancy(vehicle_numbers):
|
|
|
|
| 23 |
url = "https://bustime.ttc.ca/gtfsrt/vehicles?debug"
|
| 24 |
headers = {
|
| 25 |
'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',
|
|
@@ -30,32 +44,58 @@ def get_vehicle_locations_and_occupancy(vehicle_numbers):
|
|
| 30 |
'Connection': 'keep-alive',
|
| 31 |
'Upgrade-Insecure-Requests': '1'
|
| 32 |
}
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
text = response.text
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
entities = text.split('entity {')[1:] # skip the header
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
results = []
|
| 39 |
-
for entity in entities:
|
| 40 |
vehicle_id_matches = re.findall(r'vehicle\s*\{\s*id: "(\d+)"', entity)
|
| 41 |
if not vehicle_id_matches:
|
|
|
|
| 42 |
continue
|
| 43 |
vehicle_id = vehicle_id_matches[-1]
|
| 44 |
if vehicle_id not in vehicle_numbers:
|
|
|
|
| 45 |
continue
|
| 46 |
|
| 47 |
-
# Find latitude, longitude, occupancy_status
|
| 48 |
lat_match = re.search(r'latitude: (-?[\d\.]+)', entity)
|
| 49 |
lon_match = re.search(r'longitude: (-?[\d\.]+)', entity)
|
| 50 |
occ_match = re.search(r'occupancy_status: ([A-Z_]+)', entity)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
results.append({
|
| 53 |
'vehicle_id': vehicle_id,
|
| 54 |
'latitude': float(lat_match.group(1)) if lat_match else None,
|
| 55 |
'longitude': float(lon_match.group(1)) if lon_match else None,
|
| 56 |
-
'occupancy_status': occ_match.group(1) if occ_match else None
|
|
|
|
|
|
|
| 57 |
})
|
| 58 |
|
|
|
|
| 59 |
return results
|
| 60 |
|
| 61 |
@app.route('/', methods=['GET'])
|
|
@@ -134,16 +174,29 @@ def seek():
|
|
| 134 |
# Get minutes
|
| 135 |
minpred_span = p_tag.find('span', class_='minpred')
|
| 136 |
if minpred_span:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
vehicle_data['minutes'] =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
# Get delay text and color
|
| 143 |
-
delay_span = p_tag.find('span', style=re.compile(r'color'))
|
| 144 |
if delay_span:
|
| 145 |
vehicle_data['delay_text'] = delay_span.get_text(strip=True)
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
# Get vehicle number
|
| 149 |
vehicle_link = p_tag.find('a', href="#MapMain")
|
|
@@ -157,14 +210,14 @@ def seek():
|
|
| 157 |
vehicle_data['vehicle_number'] = match.group(1)
|
| 158 |
|
| 159 |
# Get stop location description
|
| 160 |
-
text_nodes = [t for t in p_tag.stripped_strings]
|
| 161 |
-
location = None
|
| 162 |
-
for text in reversed(text_nodes):
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
if location:
|
| 167 |
-
|
| 168 |
|
| 169 |
vehicles.append(vehicle_data)
|
| 170 |
|
|
@@ -188,8 +241,8 @@ def vehicle_status():
|
|
| 188 |
|
| 189 |
#@app.route('/test_vehicle', methods=['GET'])
|
| 190 |
#def test_vehicle():
|
| 191 |
-
# info = get_vehicle_locations_and_occupancy(['
|
| 192 |
-
# return
|
| 193 |
|
| 194 |
if __name__ == '__main__':
|
| 195 |
-
app.run(debug=True, port=5000)
|
|
|
|
| 19 |
user_network_directory='/tmp/.EasyOCR' # ✅ Also tell it where user models are
|
| 20 |
)
|
| 21 |
|
| 22 |
+
def get_trip_info(trip_id):
|
| 23 |
+
try:
|
| 24 |
+
with open('trips.txt', 'r') as f:
|
| 25 |
+
for line in f:
|
| 26 |
+
if trip_id in line:
|
| 27 |
+
# Split the line by commas and get the route information
|
| 28 |
+
parts = line.strip().split(',')
|
| 29 |
+
if len(parts) >= 2:
|
| 30 |
+
return parts[3].strip() # third part
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error reading trips.txt: {e}")
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
def get_vehicle_locations_and_occupancy(vehicle_numbers):
|
| 36 |
+
print("DEBUG: Called get_vehicle_locations_and_occupancy with vehicle_numbers:", vehicle_numbers)
|
| 37 |
url = "https://bustime.ttc.ca/gtfsrt/vehicles?debug"
|
| 38 |
headers = {
|
| 39 |
'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',
|
|
|
|
| 44 |
'Connection': 'keep-alive',
|
| 45 |
'Upgrade-Insecure-Requests': '1'
|
| 46 |
}
|
| 47 |
+
try:
|
| 48 |
+
print("DEBUG: Sending GET request to", url)
|
| 49 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 50 |
+
print("DEBUG: Response status code:", response.status_code)
|
| 51 |
+
response.raise_for_status()
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print("ERROR: Exception during requests.get:", e)
|
| 54 |
+
return jsonify({"error": f"Failed to fetch vehicle data: {e}"}), 500
|
| 55 |
+
|
| 56 |
text = response.text
|
| 57 |
+
print("DEBUG: Length of response text:", len(text))
|
| 58 |
+
if len(text) < 1000:
|
| 59 |
+
print("DEBUG: Response text (first 500 chars):", text[:500])
|
| 60 |
|
| 61 |
entities = text.split('entity {')[1:] # skip the header
|
| 62 |
+
print("DEBUG: Number of entities found:", len(entities))
|
| 63 |
+
|
| 64 |
+
# Ensure vehicle_numbers are strings for comparison
|
| 65 |
+
vehicle_numbers = set(str(num) for num in vehicle_numbers)
|
| 66 |
+
print("DEBUG: vehicle_numbers as strings:", vehicle_numbers)
|
| 67 |
|
| 68 |
results = []
|
| 69 |
+
for idx, entity in enumerate(entities):
|
| 70 |
vehicle_id_matches = re.findall(r'vehicle\s*\{\s*id: "(\d+)"', entity)
|
| 71 |
if not vehicle_id_matches:
|
| 72 |
+
print(f"DEBUG: No vehicle id found in entity {idx}")
|
| 73 |
continue
|
| 74 |
vehicle_id = vehicle_id_matches[-1]
|
| 75 |
if vehicle_id not in vehicle_numbers:
|
| 76 |
+
print(f"DEBUG: vehicle_id {vehicle_id} not in requested vehicle_numbers")
|
| 77 |
continue
|
| 78 |
|
|
|
|
| 79 |
lat_match = re.search(r'latitude: (-?[\d\.]+)', entity)
|
| 80 |
lon_match = re.search(r'longitude: (-?[\d\.]+)', entity)
|
| 81 |
occ_match = re.search(r'occupancy_status: ([A-Z_]+)', entity)
|
| 82 |
+
trip_id_match = re.search(r'trip_id: "(\d+)"', entity)
|
| 83 |
+
|
| 84 |
+
print(f"DEBUG: vehicle_id={vehicle_id}, lat={lat_match.group(1) if lat_match else None}, lon={lon_match.group(1) if lon_match else None}, occ={occ_match.group(1) if occ_match else None}")
|
| 85 |
+
|
| 86 |
+
trip_id = trip_id_match.group(1) if trip_id_match else None
|
| 87 |
+
route_info = get_trip_info(trip_id) if trip_id else None
|
| 88 |
|
| 89 |
results.append({
|
| 90 |
'vehicle_id': vehicle_id,
|
| 91 |
'latitude': float(lat_match.group(1)) if lat_match else None,
|
| 92 |
'longitude': float(lon_match.group(1)) if lon_match else None,
|
| 93 |
+
'occupancy_status': occ_match.group(1) if occ_match else None,
|
| 94 |
+
#'trip_id': trip_id, # not needed
|
| 95 |
+
'route_info': route_info
|
| 96 |
})
|
| 97 |
|
| 98 |
+
print("DEBUG: Final results to return:", results)
|
| 99 |
return results
|
| 100 |
|
| 101 |
@app.route('/', methods=['GET'])
|
|
|
|
| 174 |
# Get minutes
|
| 175 |
minpred_span = p_tag.find('span', class_='minpred')
|
| 176 |
if minpred_span:
|
| 177 |
+
secs_span = minpred_span.find('span', id=re.compile(r'SECS\d+')) # will always exist
|
| 178 |
+
secs_value = secs_span.text.strip()
|
| 179 |
+
if secs_value == "0" or secs_value.startswith("!"):
|
| 180 |
+
vehicle_data['minutes'] = "Now"
|
| 181 |
+
else:
|
| 182 |
+
vehicle_data['minutes'] = f"{secs_value} minutes"
|
| 183 |
+
|
| 184 |
+
# Get delay text and color (only spans with color: rgb(...) and NOT background-color)
|
| 185 |
+
delay_span = None
|
| 186 |
+
for span in p_tag.find_all('span', style=True):
|
| 187 |
+
style = span['style']
|
| 188 |
+
if 'color: rgb(' in style and 'background-color' not in style:
|
| 189 |
+
delay_span = span
|
| 190 |
+
break
|
| 191 |
|
|
|
|
|
|
|
| 192 |
if delay_span:
|
| 193 |
vehicle_data['delay_text'] = delay_span.get_text(strip=True)
|
| 194 |
+
# Extract just the rgb(...) value
|
| 195 |
+
rgb_match = re.search(r'color:\s*(rgb\([^)]+\))', delay_span['style'])
|
| 196 |
+
if rgb_match:
|
| 197 |
+
vehicle_data['delay_color'] = rgb_match.group(1)
|
| 198 |
+
else:
|
| 199 |
+
vehicle_data['delay_color'] = delay_span['style'] # fallback to full style if not matched
|
| 200 |
|
| 201 |
# Get vehicle number
|
| 202 |
vehicle_link = p_tag.find('a', href="#MapMain")
|
|
|
|
| 210 |
vehicle_data['vehicle_number'] = match.group(1)
|
| 211 |
|
| 212 |
# Get stop location description
|
| 213 |
+
#text_nodes = [t for t in p_tag.stripped_strings]
|
| 214 |
+
#location = None
|
| 215 |
+
#for text in reversed(text_nodes):
|
| 216 |
+
# if any(word in text.lower() for word in ['at', 'past', 'aprchg']):
|
| 217 |
+
# location = text
|
| 218 |
+
# break
|
| 219 |
+
#if location:
|
| 220 |
+
# vehicle_data['location'] = location
|
| 221 |
|
| 222 |
vehicles.append(vehicle_data)
|
| 223 |
|
|
|
|
| 241 |
|
| 242 |
#@app.route('/test_vehicle', methods=['GET'])
|
| 243 |
#def test_vehicle():
|
| 244 |
+
# info = get_vehicle_locations_and_occupancy(['3433', '1676'])
|
| 245 |
+
# return info
|
| 246 |
|
| 247 |
if __name__ == '__main__':
|
| 248 |
+
app.run(debug=True, port=5000)
|