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
+7 -3
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}
+6 -1
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"])
-13
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.
+5 -5
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)