diff --git a/server/app/api/album.py b/server/app/api/album.py index 7889112..0b00fe6 100644 --- a/server/app/api/album.py +++ b/server/app/api/album.py @@ -60,12 +60,16 @@ def get_album(): album.count = len(tracks) try: - album.duration = albumslib.get_album_duration(tracks) + album.duration = sum([t.length for t in tracks]) except AttributeError: album.duration = 0 - if (album.count == 1 and tracks[0].title == album.title - and tracks[0].tracknumber == 1 and tracks[0].disknumber == 1): + if ( + album.count == 1 + and tracks[0].title == album.title + and tracks[0].tracknumber == 1 + and tracks[0].disknumber == 1 + ): album.is_single = True return {"tracks": tracks, "info": album} diff --git a/server/app/api/playlist.py b/server/app/api/playlist.py index b956e80..f7d5a0a 100644 --- a/server/app/api/playlist.py +++ b/server/app/api/playlist.py @@ -84,7 +84,12 @@ def get_playlist(playlistid: str): playlist = models.Playlist(p) tracks = playlistlib.create_playlist_tracks(playlist.pretracks) - return {"info": serializer.Playlist(playlist), "tracks": tracks} + + duration = sum([t.length for t in tracks]) + playlist = serializer.Playlist(playlist) + playlist.duration = duration + + return {"info": playlist, "tracks": tracks} @playlist_bp.route("/playlist//update", methods=["PUT"]) diff --git a/server/app/lib/albumslib.py b/server/app/lib/albumslib.py index f8c8b92..3f949a6 100644 --- a/server/app/lib/albumslib.py +++ b/server/app/lib/albumslib.py @@ -85,19 +85,6 @@ class ValidateAlbumThumbs: self.find_lost_thumbnails() -def get_album_duration(album: List[models.Track]) -> int: - """ - Gets the duration of an album. - """ - - album_duration = 0 - - for track in album: - album_duration += track.length - - return album_duration - - def use_defaults() -> str: """ Returns a path to a random image in the defaults directory. diff --git a/server/app/serializer.py b/server/app/serializer.py index cfae04d..96a0aa6 100644 --- a/server/app/serializer.py +++ b/server/app/serializer.py @@ -59,10 +59,9 @@ class Playlist: lastUpdated: int description: str count: int = 0 + duration: int = 0 - def __init__(self, - p: models.Playlist, - construct_last_updated: bool = True) -> None: + def __init__(self, p: models.Playlist, construct_last_updated: bool = True) -> None: self.playlistid = p.playlistid self.name = p.name self.image = p.image @@ -72,7 +71,8 @@ class Playlist: self.count = p.count if construct_last_updated: - self.lastUpdated = self.l_updated(p.lastUpdated) + self.lastUpdated = self.get_l_updated(p.lastUpdated) - def l_updated(self, date: str) -> str: + @staticmethod + def get_l_updated(date: str) -> str: return date_string_to_time_passed(date) diff --git a/src/components/PlaylistView/Header.vue b/src/components/PlaylistView/Header.vue index 5c39236..a53e756 100644 --- a/src/components/PlaylistView/Header.vue +++ b/src/components/PlaylistView/Header.vue @@ -27,7 +27,8 @@ {{ props.info.count }} Track - {{ props.info.count }} Tracks • 3 Hours + {{ props.info.count }} Tracks • + {{ formatSeconds(props.info.duration, true) }}
{{ props.info.description }} @@ -58,6 +59,7 @@ import useContextStore from "../../stores/context"; import useModalStore from "../../stores/modal"; import Option from "../shared/Option.vue"; import PlayBtnRect from "../shared/PlayBtnRect.vue"; +import { formatSeconds } from "@/composables/perks"; const imguri = paths.images.playlist; const context = useContextStore(); diff --git a/src/composables/perks.ts b/src/composables/perks.ts index 79b9555..56a5971 100644 --- a/src/composables/perks.ts +++ b/src/composables/perks.ts @@ -37,9 +37,12 @@ function getElem(id: string, type: string) { } } +/** + * Converts seconds into minutes and hours. + * @param seconds The seconds to convert + * @param long Whether to provide the time in the long format + */ function formatSeconds(seconds: number, long?: boolean) { - // check if there are arguments - const date = new Date(seconds * 1000); const hh = date.getUTCHours(); diff --git a/src/interfaces.ts b/src/interfaces.ts index cc4e3af..bea31fc 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -64,6 +64,7 @@ export interface Playlist { count?: number; lastUpdated?: string; thumb?: string; + duration?: number } export interface Notif {