Coverage for downtify/deemix_provider.py: 76%

209 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 22:18 +0000

1"""Optional Deemix server audio source; credentials stay in its session.""" 

2 

3from __future__ import annotations 

4 

5import os 

6import re 

7import time 

8import unicodedata 

9from pathlib import Path 

10from typing import Any, Callable, Optional 

11from urllib.parse import urlsplit 

12 

13import httpx 

14import mutagen 

15from loguru import logger 

16 

17from .deezer import _get_json 

18 

19DEFAULT_SETTINGS = { 

20 'enabled': False, 

21 'base_url': '', 

22 'remote_dir': '/downloads', 

23 'source_dir': '/deemix', 

24 'bitrate': '3', 

25 'download_timeout_seconds': 600, 

26} 

27_AUDIO = {'.mp3', '.flac', '.m4a', '.opus', '.ogg'} 

28 

29 

30class DeemixError(RuntimeError): 

31 """A stable, credential-free reason an integration cannot proceed.""" 

32 

33 

34def normalize_settings(raw: Any) -> dict[str, Any]: 

35 """Normalize only known settings. Never accept a stored ARL.""" 

36 raw = raw if isinstance(raw, dict) else {} 

37 cfg = { 

38 **DEFAULT_SETTINGS, 

39 **{k: v for k, v in raw.items() if k in DEFAULT_SETTINGS}, 

40 } 

41 cfg['enabled'] = bool(cfg['enabled']) 

42 for key in ('base_url', 'remote_dir', 'source_dir'): 

43 cfg[key] = str(cfg[key] or '').strip().rstrip('/') 

44 cfg['bitrate'] = str(cfg['bitrate']) 

45 if cfg['bitrate'] not in {'1', '3', '9'}: 

46 cfg['bitrate'] = '3' 

47 try: 

48 cfg['download_timeout_seconds'] = min( 

49 3600, max(30, int(cfg['download_timeout_seconds'])) 

50 ) 

51 except (TypeError, ValueError): 

52 cfg['download_timeout_seconds'] = 600 

53 return cfg 

54 

55 

56def validate_settings(cfg: dict[str, Any]) -> None: 

57 """Reject embedded credentials and ambiguous path mappings.""" 

58 try: 

59 url = urlsplit(cfg['base_url']) 

60 except ValueError: 

61 raise DeemixError('connection_bad_url') from None 

62 if ( 

63 url.scheme not in {'http', 'https'} 

64 or not url.hostname 

65 or url.username 

66 or url.password 

67 or url.query 

68 or url.fragment 

69 ): 

70 raise DeemixError('connection_bad_url') 

71 if not all( 

72 Path(cfg[k]).is_absolute() for k in ('remote_dir', 'source_dir') 

73 ): 

74 raise DeemixError('folder_missing') 

75 

76 

77class DeemixClient: 

78 """One short-lived cookie session; no global queue mutation.""" 

79 

80 def __init__(self, cfg: dict[str, Any]) -> None: 

81 validate_settings(cfg) 

82 self.cfg = cfg 

83 self.http = httpx.Client( 

84 base_url=cfg['base_url'] + '/', 

85 timeout=15, 

86 follow_redirects=False, 

87 ) 

88 

89 def close(self) -> None: 

90 self.http.close() 

91 

92 def request(self, method: str, route: str, **kwargs: Any) -> Any: 

93 try: 

94 res = self.http.request(method, 'api/' + route, **kwargs) 

95 res.raise_for_status() 

96 return res.json() 

97 except (httpx.HTTPError, ValueError): 

98 raise DeemixError('connection_unreachable') from None 

99 

100 def connect(self) -> dict[str, Any]: 

101 info = self.request('GET', 'connect') 

102 if not isinstance(info, dict) or 'autologin' not in info: 

103 raise DeemixError('connection_not_deemix') 

104 if info.get('autologin'): 

105 filename = os.environ.get('DOWNTIFY_DEEMIX_ARL_FILE', '') 

106 try: 

107 arl = ( 

108 Path(filename).read_text(encoding='utf-8').strip() 

109 if filename 

110 else '' 

111 ) 

112 except OSError: 

113 raise DeemixError('auth_bad_credentials') from None 

114 if not arl: 

115 raise DeemixError('auth_bad_credentials') 

116 # Only read the private credential when opening a session. 

117 # Never expose it through settings, queue data or error messages. 

118 self.request('POST', 'loginArl', json={'arl': arl}) 

119 info = self.request('GET', 'connect') 

120 if info.get('autologin'): 

121 raise DeemixError('auth_bad_credentials') 

122 user = info.get('currentUser') or {} 

123 needed = {'3': 'can_stream_hq', '9': 'can_stream_lossless'}.get( 

124 self.cfg['bitrate'] 

125 ) 

126 if needed and not user.get(needed): 

127 raise DeemixError('quality_unavailable') 

128 return info 

129 

130 def local_file(self, remote: str) -> Optional[Path]: 

131 """Confine a queue path to the explicitly mapped shared folder.""" 

132 remote_path = Path(remote) 

133 try: 

134 rel = remote_path.relative_to(Path(self.cfg['remote_dir'])) 

135 root = Path(self.cfg['source_dir']).resolve(strict=True) 

136 path = (root / rel).resolve(strict=True) 

137 path.relative_to(root) 

138 if path.is_file() and path.suffix.lower() in _AUDIO: 

139 return path 

140 except (OSError, ValueError): 

141 pass 

142 return None 

143 

144 

145def _key(value: Any) -> str: 

146 text = unicodedata.normalize('NFKD', str(value or '').casefold()) 

147 return re.sub( 

148 r'[^\w]+', 

149 '', 

150 ''.join(c for c in text if not unicodedata.combining(c)), 

151 ) 

152 

153 

154def _artists(song: dict[str, Any]) -> str: 

155 artists = song.get('artists') or [] 

156 if isinstance(artists, str): 

157 return artists.split(',')[0] 

158 if artists: 

159 first = artists[0] 

160 return str(first.get('name', '') if isinstance(first, dict) else first) 

161 return str(song.get('artist') or '') 

162 

163 

164def candidate_matches(song: dict[str, Any], row: dict[str, Any]) -> bool: 

165 """Exact recording metadata only; never a fuzzy title-only match.""" 

166 if not _key(song.get('name')) or not _key(_artists(song)): 

167 return False 

168 if _key(song['name']) != _key(row.get('title')) or _key( 

169 _artists(song) 

170 ) != _key((row.get('artist') or {}).get('name')): 

171 return False 

172 album = song.get('album_name') 

173 if album and _key(album) != _key((row.get('album') or {}).get('title')): 

174 return False 

175 if isinstance(song.get('explicit'), bool) and ( 

176 song['explicit'] != bool(row.get('explicit_lyrics')) 

177 ): 

178 return False 

179 # For catalog downloads duration is the requested recording's length. 

180 # A later library replacement must supply catalog metadata, not the 

181 # duration of an existing YouTube clip. 

182 duration = song.get('duration') 

183 return ( 

184 not duration 

185 or abs(float(duration) - float(row.get('duration') or 0)) <= 3 

186 ) 

187 

188 

189def _catalog_track( 

190 client: DeemixClient, song: dict[str, Any] 

191) -> Optional[dict]: 

192 ident = str(song.get('song_id') or '') 

193 url = str(song.get('url') or '') 

194 match = re.fullmatch(r'deezer-(\d+)', ident) or re.search( 

195 r'https://(?:www\.)?deezer\.com/(?:[a-z]{2}/)?track/(\d+)(?:$|[?/#])', 

196 url, 

197 ) 

198 if match: 

199 # Existing catalog adapter avoids putting a Deemix session cookie 

200 # on a request to a different origin. 

201 row = _get_json('https://api.deezer.com/track/' + match[1]) 

202 return row if isinstance(row, dict) and row.get('id') else None 

203 result = client.request( 

204 'GET', 

205 'search', 

206 params={ 

207 'term': f'{_artists(song)} {song.get("name", "")}', 

208 'type': 'track', 

209 'start': 0, 

210 'nb': 25, 

211 }, 

212 ) 

213 rows = result.get('data', []) if isinstance(result, dict) else [] 

214 if isinstance(rows, dict): 

215 rows = rows.get('data', []) 

216 matches = [ 

217 row 

218 for row in rows 

219 if isinstance(row, dict) and candidate_matches(song, row) 

220 ] 

221 ids = {str(row.get('id')) for row in matches if row.get('id')} 

222 return matches[0] if len(ids) == 1 else None 

223 

224 

225def verified_audio(path: Path, row: dict[str, Any], bitrate: str) -> bool: 

226 """A queue completion alone does not prove a readable full recording.""" 

227 try: 

228 audio = mutagen.File(path, easy=True) 

229 if audio is None or not audio.info or path.stat().st_size == 0: 

230 return False 

231 tags = audio.tags or {} 

232 if _key((tags.get('title') or [''])[0]) != _key( 

233 row.get('title') 

234 ) or _key((tags.get('artist') or [''])[0]) != _key( 

235 (row.get('artist') or {}).get('name') 

236 ): 

237 return False 

238 album = _key((row.get('album') or {}).get('title')) 

239 if album and _key((tags.get('album') or [''])[0]) != album: 

240 return False 

241 expected = float(row.get('duration') or 0) 

242 if expected <= 0 or abs(audio.info.length - expected) > 3: 

243 return False 

244 lossless = 'audio/flac' in (getattr(audio, 'mime', None) or []) 

245 if bitrate == '9': 

246 return lossless 

247 if lossless: 

248 return True 

249 minimum = 300000 if bitrate == '3' else 115000 

250 return getattr(audio.info, 'bitrate', 0) >= minimum 

251 except (OSError, ValueError, mutagen.MutagenError): 

252 return False 

253 

254 

255def _completed_file( 

256 client: DeemixClient, 

257 job: dict[str, Any], 

258 row: dict[str, Any], 

259 bitrate: str, 

260) -> Path: 

261 for entry in job.get('files') or []: 

262 remote = str(entry.get('path') or '') 

263 path = client.local_file(remote) 

264 if path is None and remote and entry.get('filename'): 

265 path = client.local_file( 

266 str(Path(remote) / str(entry['filename'])) 

267 ) 

268 if path and verified_audio(path, row, bitrate): 

269 return path 

270 raise DeemixError('download_file_unverified') 

271 

272 

273def download_from_deemix( 

274 song: dict[str, Any], 

275 cfg: dict[str, Any], 

276 progress_cb: Optional[Callable[..., None]] = None, 

277 cancel_check: Optional[Callable[[], None]] = None, 

278) -> Optional[Path]: 

279 """Download one matched recording; keep all Deemix files in place.""" 

280 client: Optional[DeemixClient] = None 

281 try: 

282 if cancel_check: 

283 cancel_check() 

284 client = DeemixClient(cfg) 

285 client.connect() 

286 row = _catalog_track(client, song) 

287 if cancel_check: 

288 cancel_check() 

289 if row is None: 

290 return None 

291 ident = str(row['id']) 

292 uuid = f'track_{ident}_{cfg["bitrate"]}' 

293 result = client.request( 

294 'POST', 

295 'addToQueue', 

296 json={ 

297 'url': 'https://www.deezer.com/track/' + ident, 

298 'bitrate': cfg['bitrate'], 

299 }, 

300 ) 

301 obj = (result.get('data') or {}).get('obj') 

302 if isinstance(obj, list): 

303 obj = obj[0] if len(obj) == 1 else None 

304 if isinstance(obj, dict): 

305 uuid = str(obj.get('uuid') or uuid) 

306 deadline = time.monotonic() + cfg['download_timeout_seconds'] 

307 while time.monotonic() < deadline: 

308 if cancel_check: 

309 cancel_check() 

310 queue = client.request('GET', 'getQueue') 

311 job = (queue.get('queue') or {}).get(uuid) 

312 if job: 

313 if str(job.get('id')) != ident: 

314 raise DeemixError('download_identity_mismatch') 

315 if job.get('status') in {'completed', 'withErrors'}: 

316 return _completed_file(client, job, row, cfg['bitrate']) 

317 if job.get('status') in {'failed', 'cancelled'}: 

318 raise DeemixError('download_failed') 

319 if progress_cb: 

320 progress_cb( 

321 min(99.0, float(job.get('progress') or 0)), 

322 'Downloading', 

323 'deemix', 

324 ) 

325 time.sleep(2) 

326 raise DeemixError('download_timeout') 

327 except DeemixError as exc: 

328 logger.warning('Deemix source unavailable: {}', str(exc)) 

329 return None 

330 finally: 

331 if client: 

332 client.close() 

333 

334 

335def check_deemix(cfg: dict[str, Any]) -> dict[str, Any]: 

336 """Test a session and read-only shared folder, without queue changes.""" 

337 client: Optional[DeemixClient] = None 

338 try: 

339 client = DeemixClient(cfg) 

340 client.connect() 

341 if not Path(cfg['source_dir']).is_dir(): 

342 raise DeemixError('folder_missing') 

343 return { 

344 'ok': True, 

345 'server': 'Deemix', 

346 'checks': [ 

347 { 

348 'id': 'connection', 

349 'status': 'ok', 

350 'code': 'ok', 

351 'detail': '', 

352 }, 

353 { 

354 'id': 'folder', 

355 'status': 'ok', 

356 'code': 'ok', 

357 'detail': cfg['source_dir'], 

358 }, 

359 ], 

360 } 

361 except DeemixError as exc: 

362 ident, _, code = str(exc).partition('_') 

363 return { 

364 'ok': False, 

365 'server': '', 

366 'checks': [ 

367 {'id': ident, 'status': 'fail', 'code': code, 'detail': ''}, 

368 ], 

369 } 

370 finally: 

371 if client: 

372 client.close()