Skip to content

Commit 0d38b37

Browse files
committed
refactor createdfor parsing
1 parent 344ab7d commit 0d38b37

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

src/discovery/listenbrainz.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"time"
77
"log/slog"
8+
"errors"
89

910
cfg "explo/src/config"
1011
"explo/src/models"
@@ -255,36 +256,46 @@ func (c *ListenBrainz) getImportPlaylist(user string) (string, error) { // Get u
255256

256257
offset += playlists.Count
257258
}
258-
return "", fmt.Errorf("failed to get %s playlist, check if ListenBrainz has generated one", c.cfg.ImportPlaylist,)
259+
return "", fmt.Errorf("failed to get %s playlist, check if ListenBrainz has generated one", c.cfg.ImportPlaylist)
259260
}
260261

261-
func (c ListenBrainz) parseCreatedFor(playlists CreatedFor) (string, error) {
262+
func (c *ListenBrainz) parseCreatedFor(playlists CreatedFor) (string, error) {
262263
var currentWeek, currentDay int
263264
now := time.Now().Local()
264-
if c.cfg.ImportPlaylist != "daily-jams" {
265-
_, currentWeek = now.ISOWeek()
266-
} else {
265+
isDaily := c.cfg.ImportPlaylist == "daily-jams"
266+
if isDaily {
267267
currentDay = now.YearDay()
268+
} else {
269+
_, currentWeek = now.ISOWeek()
268270
}
269271

270-
for _, playlist := range playlists.Playlists {
272+
for _, p := range playlists.Playlists {
273+
meta := p.Playlist.Extension.HTTPSJspfPlaylist.AdditionalMetadata
274+
275+
if meta.AlgorithmMetadata.SourcePatch != c.cfg.ImportPlaylist {
276+
continue
277+
}
278+
279+
created := p.Playlist.Date.Local()
271280
var timeMatch bool
272-
273-
if c.cfg.ImportPlaylist != "daily-jams" {
274-
_, creationWeek := playlist.Playlist.Date.Local().ISOWeek()
275-
timeMatch = currentWeek == creationWeek
281+
282+
if isDaily {
283+
timeMatch = created.YearDay() == currentDay
276284
} else {
277-
creationDay := playlist.Playlist.Date.Local().YearDay()
278-
timeMatch = currentDay == creationDay
285+
_, w := created.ISOWeek()
286+
timeMatch = w == currentWeek
279287
}
280288

281-
if playlist.Playlist.Extension.HTTPSJspfPlaylist.AdditionalMetadata.AlgorithmMetadata.SourcePatch == c.cfg.ImportPlaylist && timeMatch {
282-
id := strings.Split(playlist.Playlist.Identifier, "/")
283-
return id[len(id)-1], nil
289+
if !timeMatch {
290+
continue
284291
}
292+
293+
parts := strings.Split(p.Playlist.Identifier, "/")
294+
return parts[len(parts)-1], nil
285295
}
286-
slog.Debug(fmt.Sprintf("playlist output: %v", playlists))
287-
return "", fmt.Errorf("failed to get %s playlist, check if ListenBrainz has generated one this week", c.cfg.ImportPlaylist)
296+
297+
slog.Debug("playlist output", "playlists", playlists)
298+
return "", errors.New("playlist not found in this page")
288299
}
289300

290301
func (c *ListenBrainz) parsePlaylist(identifier string, singleArtist bool) ([]*models.Track, error) {

0 commit comments

Comments
 (0)