@@ -15,18 +15,55 @@ class ParsedRouteOrURL:
15
15
start_seconds : int
16
16
length_seconds : int
17
17
18
+ # These have a dash in the 2nd part and 4 parts
19
+ # https://connect.comma.ai/a2a0ccea32023010/2023-07-27--13-01-19/7/124
20
+ # Notably, instead of looking at routes_segments, look at the route itself
21
+ # curl https://api.commadotai.com/v1/route/99c94dc769b5d96e\|2019-05-17--17-31-58/
22
+ def parseRouteRelativeUrl (
23
+ route_or_url : str ,
24
+ jwt_token : str = None ,
25
+ ) -> ParsedRouteOrURL :
26
+ # Parse the URL
27
+ parsed_url = urlparse (route_or_url )
28
+ # Check the hostname
29
+ if parsed_url .hostname != "connect.comma.ai" :
30
+ raise ValueError ("Invalid hostname in URL" )
31
+ # Check the path
32
+ path_parts = parsed_url .path .split ("/" )
33
+ # There should be five parts
34
+ if len (path_parts ) != 5 :
35
+ raise ValueError ("Invalid path in URL" )
36
+ # The first part should be the dongle ID
37
+ dongle_id = path_parts [1 ]
38
+ # The second part should be the route name in url slash format
39
+ segment_name = path_parts [2 ]
40
+ internal_route_name = f"{ dongle_id } |{ segment_name } "
41
+ # The third part should be the start time relative to the start time of the route
42
+ start_time = int (path_parts [3 ])
43
+ # The fourth part should be end time relative to the start time of the route
44
+ end_time = int (path_parts [4 ])
18
45
19
- def parseRouteOrUrl (
46
+
47
+ start_seconds = start_time
48
+ # Compute the length seconds
49
+ length_seconds = (end_time - start_time )
50
+
51
+ print (f"Route: { internal_route_name } " )
52
+ print (f"Matched Route: { internal_route_name } " )
53
+ print (f"Start Seconds: { start_seconds } " )
54
+ print (f"Start Time: { start_time } " )
55
+ print (f"Length Seconds: { length_seconds } " )
56
+ # Return the parsed route
57
+ return ParsedRouteOrURL (internal_route_name , start_seconds , length_seconds )
58
+
59
+ # These have no dash in the 2nd part and 3 parts
60
+ # https://connect.comma.ai/a2a0ccea32023010/1690488131496/1690488151496
61
+ def parseAbsoluteTimeUrl (
20
62
route_or_url : str ,
21
63
start_seconds : int ,
22
64
length_seconds : int ,
23
65
jwt_token : str = None ,
24
66
) -> ParsedRouteOrURL :
25
- # if the route_or_url is a route, just return it
26
- # Assume that a route is a string with a pipe in it
27
- if "|" in route_or_url :
28
- return ParsedRouteOrURL (route_or_url , start_seconds , length_seconds )
29
-
30
67
# Check if the URL is like this:
31
68
# https://connect.comma.ai/a2a0ccea32023010/1690488084000/1690488085000
32
69
# * Hostname is connect.comma.ai
@@ -176,6 +213,41 @@ def parseRouteOrUrl(
176
213
return ParsedRouteOrURL (route_name , start_seconds , length_seconds )
177
214
178
215
216
+ def parseRouteOrUrl (
217
+ route_or_url : str ,
218
+ start_seconds : int ,
219
+ length_seconds : int ,
220
+ jwt_token : str = None ,
221
+ ) -> ParsedRouteOrURL :
222
+ # if the route_or_url is a route, just return it
223
+ # Assume that a route is a string with a pipe in it
224
+ if "|" in route_or_url :
225
+ return ParsedRouteOrURL (route_or_url , start_seconds , length_seconds )
226
+
227
+ # Check if the URL is an absolute time URL
228
+ # Parse URL and check if it's a valid URL
229
+ parsed_url = urlparse (route_or_url )
230
+ url_parts = parsed_url .path .split ("/" )
231
+
232
+ # Check if the host is connect.comma.ai
233
+ valid_hostname = parsed_url .hostname == "connect.comma.ai"
234
+ if not valid_hostname :
235
+ raise ValueError ("Invalid hostname in URL" )
236
+ if not parsed_url .hostname == "connect.comma.ai" :
237
+ raise ValueError ("Invalid hostname in URL" )
238
+
239
+ # Check if the path has 3 parts
240
+ if len (url_parts ) == 4 and "-" not in url_parts [2 ]:
241
+ return parseAbsoluteTimeUrl (route_or_url , start_seconds , length_seconds , jwt_token )
242
+ # Check if the path has 4 parts and a dash in the 2nd part
243
+ if len (url_parts ) == 5 and "-" in url_parts [2 ]:
244
+ return parseRouteRelativeUrl (route_or_url , jwt_token )
245
+
246
+ print ("Number of url parts: " , len (url_parts ))
247
+
248
+ # If the URL is not an absolute time URL, throw an exception
249
+ raise ValueError ("Invalid URL" )
250
+
179
251
# Make an argparse test for this
180
252
if __name__ == "__main__" :
181
253
import argparse
0 commit comments