Skip to content
Snippets Groups Projects
coverdownloader.py 495 B
Newer Older
kjk's avatar
kjk committed
import requests
from pathlib import Path

class CoverDownloader:
kjk's avatar
kjk committed
    def __init__(self, outdir: Path, cache: bool):
kjk's avatar
kjk committed
        self.outdir = outdir
kjk's avatar
kjk committed
        self.cache = cache
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
    def download(self, url: str, filename: str):
        if (Path(self.outdir) / f"{filename}.jpg").is_file() and self.cache:
kjk's avatar
kjk committed
            return

kjk's avatar
kjk committed
        Path(self.outdir).mkdir(parents=True, exist_ok=True)
TheJoKlLa's avatar
TheJoKlLa committed
        with open(Path(self.outdir) / f"{filename}.jpg", "wb") as f:
kjk's avatar
kjk committed
            f.write(requests.get(url).content)