Skip to content

Commit 6d10213

Browse files
committed
Sort playlist items by date added, before downloading
Merges PR zotify-dev#55 from upstream.
1 parent 2b33f65 commit 6d10213

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

zotify/playlist.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from zotify.podcast import download_episode
33
from zotify.termoutput import Printer
44
from zotify.track import download_track
5-
from zotify.utils import split_input
5+
from zotify.utils import split_input, strptime_utc
66
from zotify.zotify import Zotify
77

88
MY_PLAYLISTS_URL = 'https://api.spotify.com/v1/me/playlists'
@@ -30,14 +30,16 @@ def get_playlist_songs(playlist_id):
3030
songs = []
3131
offset = 0
3232
limit = 100
33-
33+
3434
while True:
3535
resp = Zotify.invoke_url_with_params(f'{PLAYLISTS_URL}/{playlist_id}/tracks', limit=limit, offset=offset)
3636
offset += limit
3737
songs.extend(resp[ITEMS])
3838
if len(resp[ITEMS]) < limit:
3939
break
40-
40+
41+
songs.sort(key=lambda s: strptime_utc(s['added_at']), reverse=True)
42+
4143
return songs
4244

4345

zotify/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,17 @@ def fix_filename(name):
339339

340340
def fmt_seconds(secs: float) -> str:
341341
val = math.floor(secs)
342-
342+
343343
s = math.floor(val % 60)
344344
val -= s
345345
val /= 60
346-
346+
347347
m = math.floor(val % 60)
348348
val -= m
349349
val /= 60
350-
350+
351351
h = math.floor(val)
352-
352+
353353
if h == 0 and m == 0 and s == 0:
354354
return "0s"
355355
elif h == 0 and m == 0:
@@ -358,3 +358,7 @@ def fmt_seconds(secs: float) -> str:
358358
return f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
359359
else:
360360
return f'{h}'.zfill(2) + ':' + f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
361+
362+
363+
def strptime_utc(dtstr):
364+
return datetime.datetime.strptime(dtstr[:-1], '%Y-%m-%dT%H:%M:%S').replace(tzinfo=datetime.timezone.utc)

0 commit comments

Comments
 (0)