Skip to content

Commit fa9f78a

Browse files
committed
Enhance public call to action
1 parent 2de6c38 commit fa9f78a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

downloader.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77
import subprocess
88
import re
9+
from datetime import datetime
910

1011
# Filelist type
1112

@@ -27,6 +28,21 @@ class RouteInfoDict(TypedDict):
2728
segment_end_times: List[int]
2829
segment_start_times: List[int]
2930

31+
def convert_to_unix_timestamp(date_str: str) -> Optional[int]:
32+
date_format = "%Y-%m-%d--%H-%M-%S"
33+
34+
try:
35+
# Parse the string into a datetime object
36+
dt = datetime.strptime(date_str, date_format)
37+
38+
# Convert to a Unix timestamp
39+
unix_timestamp = int(dt.timestamp())
40+
41+
return unix_timestamp
42+
except ValueError:
43+
# Handle the case where the input string does not match the expected format
44+
return None
45+
3046
def downloadSegments(
3147
data_dir: Union[str, Path],
3248
route_or_segment: str,
@@ -57,6 +73,7 @@ def downloadSegments(
5773
# a2a0ccea32023010|2023-07-27--13-01-19 -> a2a0ccea32023010|2023-07-27--13-01-19
5874
# a2a0ccea32023010|2023-07-27--13-01-19--5 -> a2a0ccea32023010|2023-07-27--13-01-19
5975
route = re.sub(r"--\d+$", "", route_or_segment)
76+
route_date = re.sub(r"^[^|]+\|", "", route)
6077
# Dongle ID is the part before the |
6178
dongle_id = route.split("|")[0]
6279

@@ -82,7 +99,13 @@ def downloadSegments(
8299
# If it isn't, throw an error
83100
route_files_response = requests.get(filelist_url)
84101
if route_files_response.status_code != 200:
85-
raise ValueError(f"Route {route} is not accessible. You may need to set the route to be public. Visit https://connect.comma.ai/{dongle_id}, view the route, dropdown the \"More Info\" button, and toggle \"Public\".")
102+
# Add 5 seconds to the route date. It seems the date inside the
103+
# route ID is ~2-3 seconds behind the first segment's start time.
104+
# 5 is a safe number to use.
105+
route_date_int = convert_to_unix_timestamp(route_date) + 5
106+
route_date_ms = route_date_int * 1000
107+
108+
raise ValueError(f"Route {route} is not accessible. You may need to set the route to be public. Visit https://connect.comma.ai/{dongle_id}/{route_date_ms}/{route_date_ms + 30 * 1000}, dropdown the \"More Info\" button, and toggle \"Public\". You can disable the Public setting after finishing using this tool.")
86109
filelist: FileListDict = route_files_response.json()
87110

88111
# Get beginning and end times of the route for error message reasons
@@ -146,7 +169,7 @@ def downloadSegments(
146169

147170
# Make the date directory. It's just the route but with the ID stripped off the front.
148171
# E.g. a2a0ccea32023010|2023-07-27--13-01-19 -> 2023-07-27--13-01-19
149-
route_date = re.sub(r"^[^|]+\|", "", route)
172+
150173

151174
# Generate the list of URLs and paths to download to
152175
downloader = parfive.Downloader(

0 commit comments

Comments
 (0)