Skip to content

Utils

General utilities used in the mediacatch package

compress_to_ogg(input_path, output_path, sample_rate=16000)

Compresses a file to OGG format.

Parameters:

Name Type Description Default
input_path str

Input file path.

required
output_path str

Output file path.

required
sample_rate int

Sample rate. Defaults to 16.

16000

Raises:

Type Description
MediacatchUploadError

If failed to compress file to OGG format.

get_data_from_url(url)

Get data from a URL.

Parameters:

Name Type Description Default
url str

The URL to get data from.

required

Returns:

Name Type Description
dict dict

Data dictionary from url.

load_data_from_json(json_file)

Load data from a JSON file.

Parameters:

Name Type Description Default
json_file str

JSON file with data.

required

Returns:

Name Type Description
dict dict

Data dictionary from JSON file.

make_request(method, url, headers, max_retries=3, delay=0.5, log_prefix=None, also_transient=None, **kwargs)

Makes a request to the given URL, retrying transient failures.

Connection errors, timeouts, 5xx and 429 responses are retried with exponential backoff (delay, 2delay, 4delay, ..., at most 60 s). Other non-2xx responses raise immediately, unless also_transient accepts them. A seekable data body is rewound to its starting position before every attempt. On the last attempt a connection error or timeout is re-raised unchanged; pass max_retries=1 for non-idempotent calls.

Parameters:

Name Type Description Default
method str

Request method.

required
url str

The URL to make the request to.

required
headers dict[str, str]

Headers to include in the request.

required
max_retries int

Maximum attempts (at least 1). Defaults to 3.

3
delay float

Initial delay between retries. Defaults to 0.5.

0.5
log_prefix str

If set, log each failed attempt that will be retried.

None
also_transient Callable

Called with a non-2xx response; if it returns True the response is retried like a 5xx/429. Defaults to None.

None
**kwargs Any

Passed to requests. timeout defaults to (10, 300).

{}

Raises:

Type Description
MediacatchAPIError

On a non-2xx response that is not 5xx/429 (unless also_transient accepts it), or when retries run out. After a 5xx, a later 404 or a final 5xx raises the first 5xx instead.

RETRYABLE_REQUEST_ERRORS

If retries run out on a network failure.

Returns:

Type Description
Response

requests.Response: Response object.

read_file_in_chunks(file_, chunk_size=100 * 1024 * 1024)

Reads a file in chunks.

Parameters:

Name Type Description Default
file_ BufferedReader

File to read.

required
chunk_size int

Size . Defaults to 10010241024.

100 * 1024 * 1024

Yields:

Type Description
bytes

Generator[bytes, None, None]: Chunk of the file.