|
5 | 5 | "strings" |
6 | 6 | "time" |
7 | 7 | "log/slog" |
| 8 | + "errors" |
8 | 9 |
|
9 | 10 | cfg "explo/src/config" |
10 | 11 | "explo/src/models" |
@@ -255,36 +256,46 @@ func (c *ListenBrainz) getImportPlaylist(user string) (string, error) { // Get u |
255 | 256 |
|
256 | 257 | offset += playlists.Count |
257 | 258 | } |
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) |
259 | 260 | } |
260 | 261 |
|
261 | | -func (c ListenBrainz) parseCreatedFor(playlists CreatedFor) (string, error) { |
| 262 | +func (c *ListenBrainz) parseCreatedFor(playlists CreatedFor) (string, error) { |
262 | 263 | var currentWeek, currentDay int |
263 | 264 | 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 { |
267 | 267 | currentDay = now.YearDay() |
| 268 | + } else { |
| 269 | + _, currentWeek = now.ISOWeek() |
268 | 270 | } |
269 | 271 |
|
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() |
271 | 280 | 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 |
276 | 284 | } else { |
277 | | - creationDay := playlist.Playlist.Date.Local().YearDay() |
278 | | - timeMatch = currentDay == creationDay |
| 285 | + _, w := created.ISOWeek() |
| 286 | + timeMatch = w == currentWeek |
279 | 287 | } |
280 | 288 |
|
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 |
284 | 291 | } |
| 292 | + |
| 293 | + parts := strings.Split(p.Playlist.Identifier, "/") |
| 294 | + return parts[len(parts)-1], nil |
285 | 295 | } |
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") |
288 | 299 | } |
289 | 300 |
|
290 | 301 | func (c *ListenBrainz) parsePlaylist(identifier string, singleArtist bool) ([]*models.Track, error) { |
|
0 commit comments