Coverage for downtify/file_providers.py: 100%
102 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"""Which audio provider fetched each library file.
3A file carries no mark of where its audio came from - YouTube Music,
4YouTube, Soulseek (slskd) or Deemix all end up as a tagged file in the
5library - so the playlist page couldn't tell them apart. From now on the
6provider is remembered when a download finishes (``Downloader.on_downloaded``
7in ``main.py``), by the file's content key (name + size, which survives a
8folder move) and its library path (which survives a rewrite in place).
9Flows that rewrite or delete a file re-point or drop the row
10(``api.rekey_file_owners`` / ``api.drop_file_owners``).
12Files downloaded before this was kept have no row; :func:`guess_provider`
13still recognises the ones whose path says it - slskd files left in
14place (``slskd/...``) and files from extra music folders (``ext/...``).
16A row also keeps when the file was downloaded (``recorded_at``) and,
17for an slskd transfer copied into the library instead of left in place,
18the absolute path of the transfer it was copied from (``source``): that
19copy in slskd's folder is no library content of its own, which the
20library usage page (``downtify/library_usage.py``) tells apart.
21"""
23from __future__ import annotations
25from datetime import datetime, timezone
26from pathlib import Path
27from typing import Any
29from .db.config import Dialect
30from .db.core import Database, as_database
31from .library_paths import EXTERNAL_LIBRARY_PREFIX, SLSKD_LIBRARY_PREFIX
33#: Not a download at all: a file from an extra music folder.
34FOLDER = 'folder'
37def _now_iso() -> str:
38 return datetime.now(timezone.utc).isoformat()
41def _norm_path(path: object) -> str:
42 return str(path or '').strip().replace('\\', '/')
45def guess_provider(path: object) -> str:
46 """The provider a library path gives away, or ``''``."""
48 normal = _norm_path(path)
49 if normal.startswith(SLSKD_LIBRARY_PREFIX):
50 return 'slskd'
51 if normal.startswith(EXTERNAL_LIBRARY_PREFIX):
52 return FOLDER
53 return ''
56def _chunked(values: list[str], size: int = 400):
57 for index in range(0, len(values), size):
58 yield values[index : index + size]
61def _details(row: Any) -> dict[str, str]:
62 return {'provider': row['provider'], 'recorded_at': row['recorded_at']}
65class FileProviders:
66 """``content key -> provider`` (plus the path), in the library database."""
68 def __init__(self, path_or_db: str | Path | Database) -> None:
69 self.db = as_database(path_or_db, 'library')
70 if isinstance(path_or_db, Database):
71 self._path = str(self.db.path) if self.db.path else None
72 else:
73 self._path = str(path_or_db)
74 self._init_db()
76 def _connect(self):
77 return self.db.connect()
79 def _init_db(self) -> None:
80 # The Postgres schema is created by migrations; only SQLite needs DDL.
81 if self.db.dialect is not Dialect.SQLITE:
82 return
83 with self._connect() as conn:
84 conn.execute("""
85 CREATE TABLE IF NOT EXISTS file_providers (
86 content_key TEXT PRIMARY KEY,
87 path TEXT NOT NULL DEFAULT '',
88 provider TEXT NOT NULL,
89 recorded_at TEXT NOT NULL,
90 source TEXT NOT NULL DEFAULT ''
91 )
92 """)
93 columns = {
94 row['name']
95 for row in conn.execute('PRAGMA table_info(file_providers)')
96 }
97 if 'source' not in columns:
98 conn.execute(
99 'ALTER TABLE file_providers '
100 "ADD COLUMN source TEXT NOT NULL DEFAULT ''"
101 )
102 conn.execute("""
103 CREATE INDEX IF NOT EXISTS idx_file_providers_path
104 ON file_providers (path)
105 """)
107 def record(
108 self, content_key: str, path: str, provider: str, source: str = ''
109 ) -> None:
110 """Remember that *provider* fetched the file (the latest wins: a
111 file downloaded again replaced the old one) and, when it is a
112 copy, the absolute path of the transfer it was copied from."""
114 key = str(content_key or '').strip()
115 name = str(provider or '').strip()
116 if not key or not name:
117 return
118 with self._connect() as conn:
119 conn.execute(
120 """INSERT INTO file_providers
121 (content_key, path, provider, recorded_at, source)
122 VALUES (?, ?, ?, ?, ?)
123 ON CONFLICT(content_key) DO UPDATE SET
124 path = excluded.path,
125 provider = excluded.provider,
126 recorded_at = excluded.recorded_at,
127 source = excluded.source""",
128 (
129 key,
130 _norm_path(path),
131 name,
132 _now_iso(),
133 str(source or '').strip(),
134 ),
135 )
137 def lookup(self, keys_by_path: dict[str, str]) -> dict[str, str]:
138 """``{path: provider}`` for the library files ``{path: content
139 key}`` - by content key, else by path; files nobody recorded are
140 left out."""
142 return {
143 path: row['provider']
144 for path, row in self.recorded(keys_by_path).items()
145 }
147 def recorded(
148 self, keys_by_path: dict[str, str]
149 ) -> dict[str, dict[str, str]]:
150 """``{path: {provider, recorded_at}}`` for the library files
151 ``{path: content key}`` - by content key, else by path; files
152 nobody recorded are left out."""
154 wanted = {_norm_path(p): str(k or '') for p, k in keys_by_path.items()}
155 wanted.pop('', None)
156 if not wanted:
157 return {}
158 by_key: dict[str, dict[str, str]] = {}
159 by_path: dict[str, dict[str, str]] = {}
160 keys = sorted({k for k in wanted.values() if k})
161 paths = sorted(wanted)
162 with self._connect() as conn:
163 for chunk in _chunked(keys):
164 marks = ','.join('?' * len(chunk))
165 for row in conn.execute(
166 'SELECT content_key, provider, recorded_at '
167 f'FROM file_providers WHERE content_key IN ({marks})',
168 chunk,
169 ):
170 by_key[row['content_key']] = _details(row)
171 for chunk in _chunked(paths):
172 marks = ','.join('?' * len(chunk))
173 for row in conn.execute(
174 'SELECT path, provider, recorded_at FROM file_providers '
175 f'WHERE path IN ({marks})',
176 chunk,
177 ):
178 by_path.setdefault(row['path'], _details(row))
179 found: dict[str, dict[str, str]] = {}
180 for path, key in wanted.items():
181 details = by_key.get(key) or by_path.get(path)
182 if details:
183 found[path] = details
184 return found
186 def sources(self) -> list[str]:
187 """The absolute paths library files were copied from (slskd
188 transfers not left in place), whether or not still on disk."""
190 with self._connect() as conn:
191 rows = conn.execute(
192 "SELECT DISTINCT source FROM file_providers WHERE source != ''"
193 ).fetchall()
194 return [str(row['source']) for row in rows]
196 def rekey(self, old_key: str, new_key: str, stored_path: str = '') -> int:
197 """Follow a file rewritten in place (upgrade, replaced audio)."""
199 old = str(old_key or '').strip()
200 new = str(new_key or '').strip()
201 if not old or not new or old == new:
202 return 0
203 with self._connect() as conn:
204 # SQLite's ``UPDATE OR REPLACE``, spelled portably: drop the row
205 # already under the new key, but only when the old one exists.
206 conn.execute(
207 'DELETE FROM file_providers WHERE content_key = ? AND '
208 'EXISTS (SELECT 1 FROM file_providers WHERE content_key = ?)',
209 (new, old),
210 )
211 cur = conn.execute(
212 'UPDATE file_providers SET content_key = ?, '
213 "path = COALESCE(NULLIF(?, ''), path) WHERE content_key = ?",
214 (new, _norm_path(stored_path), old),
215 )
216 return cur.rowcount
218 def drop(self, content_key: str) -> None:
219 """Forget a deleted file."""
221 key = str(content_key or '').strip()
222 if not key:
223 return
224 with self._connect() as conn:
225 conn.execute(
226 'DELETE FROM file_providers WHERE content_key = ?', (key,)
227 )
230def providers_for(
231 store: FileProviders | None, keys_by_path: dict[str, str]
232) -> dict[str, str]:
233 """``{path: provider}`` for every file whose provider is known:
234 recorded, else guessed from its path (see :func:`guess_provider`)."""
236 recorded = store.lookup(keys_by_path) if store is not None else {}
237 found: dict[str, str] = {}
238 for path in keys_by_path:
239 provider = recorded.get(_norm_path(path)) or guess_provider(path)
240 if provider:
241 found[path] = provider
242 return found