Skip to content

Commit 0dc81a2

Browse files
committed
Handle new url format
Closes #78
1 parent 07804ef commit 0dc81a2

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ predict-url-ui-new-format:
4141
./cog/generate.sh
4242
../cog/cog predict -i route="https://connect.comma.ai/fe18f736cb0d7813/1712798688347/1712798721553" -i renderType=ui
4343

44+
# This is a private URL
45+
predict-url-ui-route-url-format:
46+
./cog/generate.sh
47+
../cog/cog predict -i route="https://connect.comma.ai/a2a0ccea32023010/2023-07-27--13-01-19/7/124" -i renderType=ui
48+
4449
predict-wide:
4550
./cog/generate.sh
4651
../cog/cog predict -i renderType=wide

route_or_url.py

+78-6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,55 @@ class ParsedRouteOrURL:
1515
start_seconds: int
1616
length_seconds: int
1717

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])
1845

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(
2062
route_or_url: str,
2163
start_seconds: int,
2264
length_seconds: int,
2365
jwt_token: str = None,
2466
) -> 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-
3067
# Check if the URL is like this:
3168
# https://connect.comma.ai/a2a0ccea32023010/1690488084000/1690488085000
3269
# * Hostname is connect.comma.ai
@@ -176,6 +213,41 @@ def parseRouteOrUrl(
176213
return ParsedRouteOrURL(route_name, start_seconds, length_seconds)
177214

178215

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+
179251
# Make an argparse test for this
180252
if __name__ == "__main__":
181253
import argparse

0 commit comments

Comments
 (0)