meissa/meissa/utils.py

57 lines
1.5 KiB
Python

# Meissa - A trainable and simple text to speech server
#
# Copyright (c) 2023 Sameer Rahmani <lxsameer@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path
import requests
import click
def log(level: str, msg: str):
click.echo(f"[{level}]: {msg}")
def info(msg: str):
log(click.style("INFO", fg="green"), msg)
def warn(msg: str):
log(click.style("WARN", fg="yellow"), msg)
def error(msg: str):
log(click.style("ERR", fg="red"), msg)
def home(ctx) -> Path:
return Path(ctx.obj["home"])
def config(ctx) -> dict:
return ctx.obj["config"]
def download(url: str, path: Path, fname: str = None) -> Path:
local_filename = url.split("/")[-1] if not fname else fname
output = path / local_filename
with requests.get(url, stream=True, timeout=120) as r:
r.raise_for_status()
with open(output, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return output