Skip to content

Commit

Permalink
Enhance public call to action
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonjchen committed Oct 22, 2023
1 parent 2de6c38 commit fa9f78a
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
import subprocess
import re
from datetime import datetime

# Filelist type

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

def convert_to_unix_timestamp(date_str: str) -> Optional[int]:
date_format = "%Y-%m-%d--%H-%M-%S"

try:
# Parse the string into a datetime object
dt = datetime.strptime(date_str, date_format)

# Convert to a Unix timestamp
unix_timestamp = int(dt.timestamp())

return unix_timestamp
except ValueError:
# Handle the case where the input string does not match the expected format
return None

def downloadSegments(
data_dir: Union[str, Path],
route_or_segment: str,
Expand Down Expand Up @@ -57,6 +73,7 @@ def downloadSegments(
# a2a0ccea32023010|2023-07-27--13-01-19 -> a2a0ccea32023010|2023-07-27--13-01-19
# a2a0ccea32023010|2023-07-27--13-01-19--5 -> a2a0ccea32023010|2023-07-27--13-01-19
route = re.sub(r"--\d+$", "", route_or_segment)
route_date = re.sub(r"^[^|]+\|", "", route)
# Dongle ID is the part before the |
dongle_id = route.split("|")[0]

Expand All @@ -82,7 +99,13 @@ def downloadSegments(
# If it isn't, throw an error
route_files_response = requests.get(filelist_url)
if route_files_response.status_code != 200:
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\".")
# Add 5 seconds to the route date. It seems the date inside the
# route ID is ~2-3 seconds behind the first segment's start time.
# 5 is a safe number to use.
route_date_int = convert_to_unix_timestamp(route_date) + 5
route_date_ms = route_date_int * 1000

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.")
filelist: FileListDict = route_files_response.json()

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

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


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

0 comments on commit fa9f78a

Please sign in to comment.