Skip to content
Snippets Groups Projects
coverdownloader.py 536 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

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

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