Utils¶
Pure helper functions for GoPro media selection, naming, and I/O.
gopro_api.utils.is_video_filename(filename: str) -> bool
¶
Return whether the filename looks like MP4 video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Basename or path ending (extension is checked case-insensitively). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in gopro_api/utils.py
gopro_api.utils.select_video_variation(variations: list[GoProMediaDownloadVariation], *, target_height: int | None = None, target_width: int | None = None) -> GoProMediaDownloadVariation
¶
Pick the best variation from variations.
When neither target is set, returns the variation with the greatest height.
Otherwise scores each candidate by the sum of squared deltas for the requested
dimensions; ties break toward the larger (height, width).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variations
|
list[GoProMediaDownloadVariation]
|
Candidate renditions from the API download metadata. |
required |
target_height
|
int | None
|
Desired height in pixels, or |
None
|
target_width
|
int | None
|
Desired width in pixels, or |
None
|
Returns:
| Type | Description |
|---|---|
GoProMediaDownloadVariation
|
The selected |
Raises:
| Type | Description |
|---|---|
NoVariationsError
|
If |
Source code in gopro_api/utils.py
gopro_api.utils.extension_from_url(url: str) -> str | None
¶
Return a file extension from a CDN download URL (no leading dot).
Prefers the filename in response-content-disposition when present,
otherwise the URL path suffix. Library filenames can disagree with the real
asset (for example MultiClipEdit rows named *.json whose baked source is
*.mp4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Absolute HTTPS URL, optionally with a query string. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Extension such as |
Source code in gopro_api/utils.py
gopro_api.utils.get_file_name(root_name: str, item_number: int, *, extension: str | None = None) -> str
¶
Build a part filename by inserting a zero-padded index before the extension.
Example: get_file_name("GX010001.MP4", 2) → "GX010001002.MP4".
When extension is set it replaces the suffix from root_name (useful
when the library name is .json but the CDN asset is .mp4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_name
|
str
|
Original media filename including extension. |
required |
item_number
|
int
|
Non-negative part index (three-digit zero padding). |
required |
extension
|
str | None
|
Optional override for the file format (no leading dot). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Derived filename string. |
Source code in gopro_api/utils.py
gopro_api.utils.pull_assets_for_response(result: GoProMediaDownloadResponse, *, target_height: int | None = None, target_width: int | None = None) -> dict[str, DownloadAsset]
¶
Map output filenames to assets to download for result.
Video (.mp4): picks one variation via select_video_variation.
Non-video: returns every file in _embedded.files in enumeration order
(no available filtering, preserving CLI behaviour for burst sets).
Output extensions come from each asset URL (content-disposition or path), not from the library filename alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
GoProMediaDownloadResponse
|
Parsed download-metadata response for one media id. |
required |
target_height
|
int | None
|
Optional preferred video height for variation scoring. |
None
|
target_width
|
int | None
|
Optional preferred video width for variation scoring. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, DownloadAsset]
|
Mapping of local filename to downloadable file or variation row. |
Raises:
| Type | Description |
|---|---|
NoVariationsError
|
For video media when no variations are present. |
Source code in gopro_api/utils.py
gopro_api.utils.write_bytes(path: str, data: bytes) -> None
¶
Write binary data to a path (blocking I/O).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination file path. |
required |
data
|
bytes
|
Raw bytes to persist. |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If the file cannot be opened or written. |