From 6a4385912629cc8789177f8e90f8da56b9897c2b Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Tue, 22 Nov 2022 12:56:49 +0100 Subject: [PATCH] refactored using the song and src object instead of dict --- .../audio_source/sources/musify.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/music_kraken/audio_source/sources/musify.py b/src/music_kraken/audio_source/sources/musify.py index e7557d2..f0de8aa 100644 --- a/src/music_kraken/audio_source/sources/musify.py +++ b/src/music_kraken/audio_source/sources/musify.py @@ -135,7 +135,13 @@ class Musify(AudioSource): return None @classmethod - def download_from_musify(cls, file, url): + def download_from_musify(cls, target: song_objects.Target, url): + # returns if target hasn't been set + if target.path is None or target.file is None: + logger.warning(f"target hasn't been set. Can't download. Most likely a bug.") + return False + + # download the audio data logger.info(f"downloading: '{url}'") try: r = session.get(url, timeout=15) @@ -151,8 +157,10 @@ class Musify(AudioSource): logger.error(f"\"{url}\" returned {r.status_code}: {r.text}") return False - # write to the file - with open(file, "wb") as mp3_file: + # write to the file and create folder if it doesn't exist + if not os.path.exists(target.path): + os.makedirs(target.path, exist_ok=True) + with open(target.file, "wb") as mp3_file: mp3_file.write(r.content) logger.info("finished") return True @@ -160,9 +168,7 @@ class Musify(AudioSource): @classmethod def fetch_audio(cls, song: song_objects.Song, src: song_objects.Source): super().fetch_audio(song, src) - - file_ = song['file'] - return cls.download_from_musify(file_, src.url) + return cls.download_from_musify(song.target, src.url) if __name__ == "__main__":