Coverage for downtify/playlist_identity.py: 100%
19 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
1"""Telling playlists apart: the same playlist under its Spotify name, its
2M3U file name and the Navidrome playlists made from either.
4A leaf module (no Downtify imports) so both :mod:`downtify.navidrome`
5and :mod:`downtify.doctor` can use it.
6"""
8from __future__ import annotations
10import re
11import unicodedata
12from typing import Any
14#: Playlists Downtify creates in Navidrome carry this comment.
15DOWNTIFY_COMMENT = 'Synced from Spotify via Downtify'
16#: Navidrome's comment on a playlist it imported from an M3U file.
17_IMPORTED_COMMENT = re.compile(r"^Auto-imported from '(?P<file>.+)'$")
20def playlist_key(name: str) -> str:
21 """A playlist name with case, accents, punctuation and the
22 characters a filename can't hold folded away - ``Chill: Vibes`` and
23 the ``Chill Vibes.m3u`` written for it compare equal."""
25 text = unicodedata.normalize('NFKD', str(name or ''))
26 text = ''.join(ch for ch in text if not unicodedata.combining(ch))
27 text = ''.join(ch if ch.isalnum() else ' ' for ch in text.casefold())
28 return ' '.join(text.split())
31def made_by_downtify(playlist: dict[str, Any]) -> bool:
32 """A Navidrome playlist Downtify created through the API."""
34 return str(playlist.get('comment') or '') == DOWNTIFY_COMMENT
37def imported_from(playlist: dict[str, Any]) -> str:
38 """The M3U a Navidrome playlist was imported from ('' when none):
39 the native API's ``path``, else the file named in its comment."""
41 path = str(playlist.get('path') or '').strip().replace('\\', '/')
42 if playlist.get('sync') or path:
43 return path
44 match = _IMPORTED_COMMENT.match(str(playlist.get('comment') or ''))
45 return match.group('file') if match else ''