fixed author and album images

This commit is contained in:
Alexander Karpov 2024-01-12 00:52:28 +03:00
parent 017efcb1cc
commit fb2b611dd8
2 changed files with 27 additions and 26 deletions

View File

@ -166,28 +166,34 @@ def get_yandex_artist_info(artist_name: str, client: Client):
def download_image(url, save_path): def download_image(url, save_path):
image_path = os.path.join(save_path, f"tmp_{randint(10000, 99999)}.png")
if type(url) is Cover: if type(url) is Cover:
url = url["uri"] url.download(image_path)
if not str(url).startswith("http"): else:
url = "https://" + str(url) if not url.startswith("http"):
response = requests.get(url) url = "https://" + url
if response.status_code == 200: response = requests.get(url)
image_path = os.path.join(save_path, f"tmp_{randint(10000, 99999)}.png") if response.status_code == 200:
with open(image_path, "wb") as f: with open(image_path, "wb") as f:
f.write(response.content) f.write(response.content)
return image_path return image_path
return "" return ""
def update_album_info(album: AlbumModel, track_name: str) -> None: def update_album_info(album: AlbumModel, author_name: str = None) -> None:
client = yandex_login() client = yandex_login()
spotify_session = create_spotify_session() spotify_session = create_spotify_session()
# Retrieve info from both services if author_name:
yandex_album_info = get_yandex_album_info(album.name + " - " + track_name, client) yandex_album_info = get_yandex_album_info(
spotify_album_info = get_spotify_album_info( album.name + " - " + author_name, client
album.name + " - " + track_name, spotify_session )
) spotify_album_info = get_spotify_album_info(
album.name + " - " + author_name, spotify_session
)
else:
yandex_album_info = get_yandex_album_info(album.name, client)
spotify_album_info = get_spotify_album_info(album.name, spotify_session)
# Combine and prioritize Spotify data # Combine and prioritize Spotify data
album_data = {} album_data = {}
@ -271,17 +277,13 @@ def update_album_info(album: AlbumModel, track_name: str) -> None:
album.save() album.save()
def update_author_info(author: Author, track_name: str) -> None: def update_author_info(author: Author) -> None:
client = yandex_login() client = yandex_login()
spotify_session = create_spotify_session() spotify_session = create_spotify_session()
# Retrieve info from both services # Retrieve info from both services
yandex_artist_info = get_yandex_artist_info( yandex_artist_info = get_yandex_artist_info(author.name, client)
author.name + " - " + track_name, client spotify_artist_info = get_spotify_artist_info(author.name, spotify_session)
)
spotify_artist_info = get_spotify_artist_info(
author.name + " - " + track_name, spotify_session
)
# Combine and prioritize Spotify data # Combine and prioritize Spotify data
author_data = {} author_data = {}

View File

@ -17,15 +17,14 @@ def auto_delete_file_on_delete(sender, instance, **kwargs):
@receiver(post_save, sender=Author) @receiver(post_save, sender=Author)
def author_create(sender, instance, created, **kwargs): def author_create(sender, instance, created, **kwargs):
if created: if created:
songs = Song.objects.filter(authors=instance) update_author_info(instance)
update_author_info(instance, songs.first().name if songs.exists() else "")
@receiver(post_save, sender=Album) @receiver(post_save, sender=Album)
def album_create(sender, instance, created, **kwargs): def album_create(sender, instance, created, **kwargs):
if created: if created:
songs = Song.objects.filter(album=instance) authors = instance.authors.all()
update_album_info(instance, songs.first().name if songs.exists() else "") update_album_info(instance, authors.first().name if authors.exists() else None)
@receiver(post_save) @receiver(post_save)