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, **kwargs)
Makes a request to the given URL.
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 retries. Defaults to 3. |
3
|
delay
|
float
|
Delay between retries. Defaults to 0.5. |
0.5
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If method is post and response status code is equal or greater than 400. |
RuntimeError
|
If maximum retries limit is reached. |
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. |