|
| 1 | +# Parses a route or URL string, throwing an exception if it's invalid. |
| 2 | + |
| 3 | +import dataclasses |
| 4 | + |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +# Dataclass for a parsed route or URL |
| 10 | + |
| 11 | + |
| 12 | +@dataclasses.dataclass |
| 13 | +class ParsedRouteOrURL: |
| 14 | + route: str |
| 15 | + start_seconds: int |
| 16 | + length_seconds: int |
| 17 | + |
| 18 | + |
| 19 | +def parseRouteOrUrl( |
| 20 | + route_or_url: str, start_seconds: int, length_seconds: int |
| 21 | +) -> ParsedRouteOrURL: |
| 22 | + # if the route_or_url is a route, just return it |
| 23 | + # Assume that a route is a string with a pipe in it |
| 24 | + if "|" in route_or_url: |
| 25 | + return ParsedRouteOrURL(route_or_url, start_seconds, length_seconds) |
| 26 | + |
| 27 | + # Check if the URL is like this: |
| 28 | + # https://connect.comma.ai/a2a0ccea32023010/1690488084000/1690488085000 |
| 29 | + # * Hostname is connect.comma.ai |
| 30 | + # * Path is "dongle id"/"start time"/"end time" |
| 31 | + # * Start time and end time are in milliseconds since the epoch |
| 32 | + # * Start time is before end time |
| 33 | + |
| 34 | + # Parse the URL |
| 35 | + parsed_url = urlparse(route_or_url) |
| 36 | + |
| 37 | + # Check the hostname |
| 38 | + if parsed_url.hostname != "connect.comma.ai": |
| 39 | + raise ValueError("Invalid hostname in URL") |
| 40 | + |
| 41 | + # Check the path |
| 42 | + path_parts = parsed_url.path.split("/") |
| 43 | + # There should be three parts |
| 44 | + if len(path_parts) != 4: |
| 45 | + raise ValueError("Invalid path in URL") |
| 46 | + # The first part should be the dongle ID |
| 47 | + dongle_id = path_parts[1] |
| 48 | + # The second part should be the start time |
| 49 | + start_time = int(path_parts[2]) |
| 50 | + # The third part should be the end time |
| 51 | + end_time = int(path_parts[3]) |
| 52 | + # Start time should be before end time |
| 53 | + if start_time >= end_time: |
| 54 | + raise ValueError("Invalid start and end times in URL") |
| 55 | + |
| 56 | + # The above URL is equivalent to this API call: |
| 57 | + # https://api.comma.ai/v1/devices/a2a0ccea32023010/routes_segments?end=1690488851596&start=1690488081496 |
| 58 | + |
| 59 | + # Make the API call |
| 60 | + api_url = f"https://api.comma.ai/v1/devices/{dongle_id}/routes_segments?end={end_time}&start={start_time}" |
| 61 | + response = requests.get(api_url) |
| 62 | + # Check the response |
| 63 | + if response.status_code != 200: |
| 64 | + raise ValueError("Invalid API response") |
| 65 | + |
| 66 | + json = response.json() |
| 67 | + |
| 68 | + # Response (Excerpt) is like this |
| 69 | + # [ |
| 70 | + # { |
| 71 | + # "fullname": "a2a0ccea32023010|2023-07-27--13-01-19", |
| 72 | + # "segment_end_times": [ |
| 73 | + # 1690488142995, |
| 74 | + # 1690488203050, |
| 75 | + # 1690488263032, |
| 76 | + # 1690488322998, |
| 77 | + # 1690488383009, |
| 78 | + # 1690488443000, |
| 79 | + # 1690488503010, |
| 80 | + # 1690488563006, |
| 81 | + # 1690488623013, |
| 82 | + # 1690488683016, |
| 83 | + # 1690488743014, |
| 84 | + # 1690488803019, |
| 85 | + # 1690488851596 |
| 86 | + # ], |
| 87 | + # "segment_numbers": [ |
| 88 | + # 0, |
| 89 | + # 1, |
| 90 | + # 2, |
| 91 | + # 3, |
| 92 | + # 4, |
| 93 | + # 5, |
| 94 | + # 6, |
| 95 | + # 7, |
| 96 | + # 8, |
| 97 | + # 9, |
| 98 | + # 10, |
| 99 | + # 11, |
| 100 | + # 12 |
| 101 | + # ], |
| 102 | + # "segment_start_times": [ |
| 103 | + # 1690488081496, |
| 104 | + # 1690488143038, |
| 105 | + # 1690488203035, |
| 106 | + # 1690488263028, |
| 107 | + # 1690488323037, |
| 108 | + # 1690488383025, |
| 109 | + # 1690488443035, |
| 110 | + # 1690488503030, |
| 111 | + # 1690488563038, |
| 112 | + # 1690488623040, |
| 113 | + # 1690488683035, |
| 114 | + # 1690488743039, |
| 115 | + # 1690488803035 |
| 116 | + # ], |
| 117 | + # } |
| 118 | + # ] |
| 119 | + # And keep in mind there can be multiple unrelated routes in the response. |
| 120 | + # It seems filtering does not work and it returns unrelated routes. |
| 121 | + # Try to find the route of interest |
| 122 | + # As an example, https://connect.comma.ai/a2a0ccea32023010/1690488152777/1690488186013 |
| 123 | + # Should return |
| 124 | + # Route: a2a0ccea32023010|2023-07-27--13-01-19 |
| 125 | + # Start Seconds: 71 |
| 126 | + # Length Seconds: 104 |
| 127 | + # Ignore all the milliseconds too |
| 128 | + |
| 129 | + # Discover what the start and end times of each route returned are |
| 130 | + matched_route = None |
| 131 | + |
| 132 | + for route_info in json: |
| 133 | + start_in_route = False |
| 134 | + end_in_route = False |
| 135 | + # Assume the first segment_start_time is the start of the route |
| 136 | + route_start_time = route_info["segment_start_times"][0] |
| 137 | + # Assume the last segment_end_time is the end of the route |
| 138 | + route_end_time = route_info["segment_end_times"][-1] |
| 139 | + # Check if the start time is in the route |
| 140 | + if start_time >= route_start_time and start_time <= route_end_time: |
| 141 | + start_in_route = True |
| 142 | + # Check if the end time is in the route |
| 143 | + if end_time >= route_start_time and end_time <= route_end_time: |
| 144 | + end_in_route = True |
| 145 | + # If both the start and end times are in the route, we found our match |
| 146 | + if start_in_route and end_in_route: |
| 147 | + matched_route = route_info |
| 148 | + break |
| 149 | + |
| 150 | + # If we didn't find a match, throw an exception |
| 151 | + if matched_route is None: |
| 152 | + raise ValueError(f"Route not found from URL. Route is possibly private. Visit the URL {route_or_url} and make sure Public is toggled under the \"More Info\" drop-down.") |
| 153 | + |
| 154 | + # Get the route name |
| 155 | + route_name = matched_route["fullname"] |
| 156 | + # Compute the start seconds |
| 157 | + start_seconds = (start_time - route_start_time) // 1000 |
| 158 | + # Compute the length seconds |
| 159 | + length_seconds = (end_time - start_time) // 1000 |
| 160 | + |
| 161 | + # Return the parsed route |
| 162 | + return ParsedRouteOrURL(route_name, start_seconds, length_seconds) |
| 163 | + |
| 164 | + |
| 165 | +# Make an argparse test for this |
| 166 | +if __name__ == "__main__": |
| 167 | + import argparse |
| 168 | + |
| 169 | + parser = argparse.ArgumentParser(description="parse a route or URL") |
| 170 | + parser.add_argument("route_or_url", type=str, help="Route or URL to parse") |
| 171 | + parser.add_argument("start_seconds", type=int, help="Start time in seconds") |
| 172 | + parser.add_argument( |
| 173 | + "length_seconds", type=int, help="Length of the segment to render" |
| 174 | + ) |
| 175 | + args = parser.parse_args() |
| 176 | + |
| 177 | + parsed_route = parseRouteOrUrl( |
| 178 | + args.route_or_url, args.start_seconds, args.length_seconds |
| 179 | + ) |
| 180 | + |
| 181 | + print(f"Route: {parsed_route.route}") |
| 182 | + print(f"Start Seconds: {parsed_route.start_seconds}") |
| 183 | + print(f"Length Seconds: {parsed_route.length_seconds}") |
0 commit comments