verbanote-server/verbanote/file_operations.py

50 lines
1.3 KiB
Python

from pathlib import Path
import logging
import requests
import subprocess
def download_from_url(url: str, input_path: Path) -> Path:
resp = requests.get(url)
if not resp.ok:
logging.error(f"Created error code: {resp.status_code}")
raise requests.exceptions.HTTPError()
# TODO think about implementing a naming scheme based on url path
fname = Path.joinpath(input_path, "inputfile")
with open(fname, mode="wb") as file:
file.write(resp.content)
logging.info(f"Downloaded input file: {fname}")
return fname
def upload_to_oxo(file: Path, url: str = "https://0x0.st", expires: int = 2) -> str:
resp = requests.post(
url=url, files={"file": open(file, "rb"), "expires": str(expires)}
)
if not resp.ok:
raise requests.exceptions.HTTPError()
logging.info(f"Uploaded file {file} to {str(resp.content)}")
return str(resp.content)
def convert_to_wav(file: Path, output_path: Path) -> Path:
fn = Path.joinpath(output_path, "interview.wav")
subprocess.run(
[
"ffmpeg",
"-i",
file,
"-vn",
"-acodec",
"pcm_s16le",
"-ar",
"16000",
"-ac",
"1",
"-y",
fn,
]
)
logging.info(f"Converted {file} to wav format: {fn}")
return fn