Skip to content

Commit 1dff812

Browse files
committed
smartplaylist: Fix duplicate entries and count bug
Deduplicate playlist output by URI instead of comparing bytes to PlaylistItem objects. Increment matched/pretend counts only when a new entry is actually added. This prevents duplicate lines when the same track is reached via multiple query paths.
1 parent 9dd9cfc commit 1dff812

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

beetsplug/smartplaylist.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ def update_playlists(
301301
if relative_to:
302302
relative_to = normpath(relative_to)
303303

304-
# Maps playlist filenames to lists of track filenames.
304+
# Maps playlist filenames to lists of track entries and URI sets used
305+
# to deduplicate output lines.
305306
m3us: dict[str, list[PlaylistItem]] = {}
307+
m3u_uris: dict[str, set[Any]] = {}
306308

307309
for playlist in self._matched_playlists:
308310
pretend_count = 0
@@ -345,6 +347,7 @@ def update_playlists(
345347
m3u_name = sanitize_path(m3u_name, lib.replacements)
346348
if m3u_name not in m3us:
347349
m3us[m3u_name] = []
350+
m3u_uris[m3u_name] = set()
348351
item_uri = item.path
349352
if tpl:
350353
item_uri = tpl.replace("$id", str(item.id)).encode("utf-8")
@@ -361,7 +364,8 @@ def update_playlists(
361364
)
362365
item_uri = prefix + item_uri
363366

364-
if item_uri not in m3us[m3u_name]:
367+
if item_uri not in m3u_uris[m3u_name]:
368+
m3u_uris[m3u_name].add(item_uri)
365369
m3us[m3u_name].append(PlaylistItem(item, item_uri))
366370
if (
367371
pretend
@@ -371,7 +375,7 @@ def update_playlists(
371375
print(displayable_path(item_uri))
372376
elif pretend and not quiet:
373377
print(item)
374-
pretend_count += 1
378+
pretend_count += 1
375379
if quiet:
376380
self._log.info("{}: {} items matched.", name, pretend_count)
377381

0 commit comments

Comments
 (0)