mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 13:03:02 +00:00
add routes to get all albums and artists with sort
+ rewrite load all albums + artist logic with itertools.groupby + add a function to convert seconds to string
This commit is contained in:
+28
-1
@@ -4,14 +4,41 @@ Contains methods relating to albums.
|
||||
|
||||
from dataclasses import asdict
|
||||
from typing import Any
|
||||
from itertools import groupby
|
||||
|
||||
|
||||
from app.logger import log
|
||||
from app.models.track import Track
|
||||
from app.store.albums import AlbumStore
|
||||
from app.store.tracks import TrackStore
|
||||
|
||||
|
||||
def create_albums():
|
||||
"""
|
||||
Creates albums from the tracks in the store.
|
||||
"""
|
||||
|
||||
# group all tracks by albumhash
|
||||
tracks = TrackStore.tracks
|
||||
tracks = sorted(tracks, key=lambda t: t.albumhash)
|
||||
grouped = groupby(tracks, lambda t: t.albumhash)
|
||||
|
||||
# create albums from the groups
|
||||
albums: list[Track] = []
|
||||
for albumhash, tracks in grouped:
|
||||
count = len(list(tracks))
|
||||
duration = sum(t.duration for t in tracks)
|
||||
created_date = min(t.created_date for t in tracks)
|
||||
|
||||
album = AlbumStore.create_album(list(tracks)[0])
|
||||
album.set_count(count)
|
||||
album.set_duration(duration)
|
||||
album.set_created_date(created_date)
|
||||
|
||||
albums.append(album)
|
||||
|
||||
return albums
|
||||
|
||||
|
||||
def validate_albums():
|
||||
"""
|
||||
Removes albums that have no tracks.
|
||||
|
||||
Reference in New Issue
Block a user