calculate playlist duration

- use python's `sum()` method to add durations instead of for loop
This commit is contained in:
geoffrey45 2022-07-02 13:19:09 +03:00 committed by Mungai Geoffrey
parent 5bc0eaf8e6
commit b318c0d324
7 changed files with 27 additions and 25 deletions

View File

@ -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}

View File

@ -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/<playlistid>/update", methods=["PUT"])

View File

@ -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.

View File

@ -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)

View File

@ -27,7 +27,8 @@
<span v-else-if="props.info.count == 1"
>{{ props.info.count }} Track</span
>
<span v-else>{{ props.info.count }} Tracks</span> 3 Hours
<span v-else>{{ props.info.count }} Tracks</span>
{{ formatSeconds(props.info.duration, true) }}
</div>
<div class="desc">
{{ 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();

View File

@ -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();

View File

@ -64,6 +64,7 @@ export interface Playlist {
count?: number;
lastUpdated?: string;
thumb?: string;
duration?: number
}
export interface Notif {