refactored

This commit is contained in:
Lars Noack 2022-11-18 09:09:50 +01:00
parent d45b42ff7d
commit 2a846e5b7b
2 changed files with 25 additions and 2 deletions

View File

@ -132,13 +132,36 @@ class Musify(AudioSource):
return None return None
@classmethod
def download_from_musify(cls, file, url):
logger.info(f"downloading: '{url}'")
try:
r = session.get(url, timeout=15)
except requests.exceptions.ConnectionError or requests.exceptions.ReadTimeout:
return False
if r.status_code != 200:
if r.status_code == 404:
logger.warning(f"{r.url} was not found")
return False
if r.status_code == 503:
logger.warning(f"{r.url} raised an internal server error")
return False
logger.error(f"\"{url}\" returned {r.status_code}: {r.text}")
return False
# write to the file
with open(file, "wb") as mp3_file:
mp3_file.write(r.content)
logger.info("finished")
return True
@classmethod @classmethod
def fetch_audio(cls, row: dict): def fetch_audio(cls, row: dict):
super().fetch_audio(row) super().fetch_audio(row)
url = row['url'] url = row['url']
file_ = row['file'] file_ = row['file']
return download_from_musify(file_, url) return cls.download_from_musify(file_, url)
""" """

View File

@ -22,7 +22,7 @@ class UrlPath:
self.genre = genre self.genre = genre
for row in database.get_tracks_without_filepath(): for row in database.get_tracks_without_filepath():
print(row) # print(row)
file, path = self.get_path_from_row(row) file, path = self.get_path_from_row(row)
logger.info(f"setting target to {file}") logger.info(f"setting target to {file}")
database.set_filepath(row['id'], file, path, genre) database.set_filepath(row['id'], file, path, genre)