42Cummer commited on
Commit
cd1a1fd
·
verified ·
1 Parent(s): 5aadc1b

Update app.py

Browse files

Add CORS, function to get vehicle location

Files changed (1) hide show
  1. app.py +59 -2
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from flask import Flask, render_template, request, jsonify
 
2
  import easyocr
3
  import re
4
  import requests
@@ -7,7 +8,7 @@ import tempfile
7
  import os
8
 
9
  app = Flask(__name__)
10
-
11
  os.makedirs('/tmp/.EasyOCR', exist_ok=True)
12
 
13
  reader = easyocr.Reader(
@@ -18,6 +19,45 @@ reader = easyocr.Reader(
18
  user_network_directory='/tmp/.EasyOCR' # ✅ Also tell it where user models are
19
  )
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @app.route('/', methods=['GET'])
22
  def health_check():
23
  return 'Backend is running!', 200
@@ -128,5 +168,22 @@ def seek():
128
  'vehicles': vehicles
129
  })
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  if __name__ == '__main__':
132
- app.run(debug=True, port=5000)
 
1
  from flask import Flask, render_template, request, jsonify
2
+ from flask_cors import CORS
3
  import easyocr
4
  import re
5
  import requests
 
8
  import os
9
 
10
  app = Flask(__name__)
11
+ CORS(app)
12
  os.makedirs('/tmp/.EasyOCR', exist_ok=True)
13
 
14
  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',
26
+ 'Referer': 'https://www.transsee.ca/',
27
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
28
+ 'Accept-Language': 'en-US,en;q=0.9',
29
+ 'DNT': '1', # Do Not Track
30
+ 'Connection': 'keep-alive',
31
+ 'Upgrade-Insecure-Requests': '1'
32
+ }
33
+ response = requests.get(url, headers=headers)
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'])
62
  def health_check():
63
  return 'Backend is running!', 200
 
168
  'vehicles': vehicles
169
  })
170
 
171
+ @app.route('/vehicles', methods=['POST'])
172
+ def vehicle_status():
173
+ if not request.is_json:
174
+ return jsonify({'error': 'Request must be JSON'}), 400
175
+ data = request.get_json()
176
+ vehicle_numbers = data.get('vehicle_numbers')
177
+ if not vehicle_numbers or not isinstance(vehicle_numbers, list):
178
+ return jsonify({'error': 'vehicle_numbers must be a list'}), 400
179
+
180
+ info = get_vehicle_locations_and_occupancy(vehicle_numbers)
181
+ return jsonify({'vehicles': info})
182
+
183
+ #@app.route('/test_vehicle', methods=['GET'])
184
+ #def test_vehicle():
185
+ # info = get_vehicle_locations_and_occupancy(['1217'])
186
+ # return jsonify(info)
187
+
188
  if __name__ == '__main__':
189
+ app.run(debug=True, port=5000)