Skip to content

Commit a23cf58

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 c6c8414 commit a23cf58

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

beetsplug/smartplaylist.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,10 @@ def update_playlists(self, lib: Library, pretend: bool = False) -> None:
299299
if relative_to:
300300
relative_to = normpath(relative_to)
301301

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

305307
for playlist in self._matched_playlists:
306308
pretend_count = 0
@@ -343,6 +345,7 @@ def update_playlists(self, lib: Library, pretend: bool = False) -> None:
343345
m3u_name = sanitize_path(m3u_name, lib.replacements)
344346
if m3u_name not in m3us:
345347
m3us[m3u_name] = []
348+
m3u_uris[m3u_name] = set()
346349
item_uri = item.path
347350
if tpl:
348351
item_uri = tpl.replace("$id", str(item.id)).encode("utf-8")
@@ -359,7 +362,8 @@ def update_playlists(self, lib: Library, pretend: bool = False) -> None:
359362
)
360363
item_uri = prefix + item_uri
361364

362-
if item_uri not in m3us[m3u_name]:
365+
if item_uri not in m3u_uris[m3u_name]:
366+
m3u_uris[m3u_name].add(item_uri)
363367
m3us[m3u_name].append(PlaylistItem(item, item_uri))
364368
if (
365369
pretend
@@ -369,7 +373,7 @@ def update_playlists(self, lib: Library, pretend: bool = False) -> None:
369373
print(displayable_path(item_uri))
370374
elif pretend and not quiet:
371375
print(item)
372-
pretend_count += 1
376+
pretend_count += 1
373377
if quiet:
374378
self._log.info("{}: {} items matched.", name, pretend_count)
375379

0 commit comments

Comments
 (0)