Coverage for downtify/external_playlists.py: 91%

143 statements  

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

1"""Discover external M3U files and import selected snapshots, never audio.""" 

2 

3from __future__ import annotations 

4 

5import hashlib 

6import os 

7import threading 

8from dataclasses import dataclass 

9from pathlib import Path 

10 

11from . import m3u 

12from .db.config import Dialect 

13from .db.core import Database, as_database 

14from .library_catalog import AUDIO_EXTENSIONS, LibraryContext 

15from .library_paths import library_stored_path 

16from .manual_playlists import copy_playlist, existing_playlist_names 

17from .playlist_catalog import ManualPlaylistOwners 

18from .playlist_shares import PlaylistShares 

19 

20MAX_BYTES = 2 * 1024 * 1024 

21MAX_LINES = 20_000 

22MAX_CANDIDATES = 500 

23MAX_SCAN_ENTRIES = 100_000 

24 

25 

26@dataclass(frozen=True) 

27class ExternalPlaylist: 

28 source_id: str 

29 name: str 

30 folder: str 

31 revision: str 

32 files: tuple[str, ...] 

33 missing: int 

34 duplicates: int 

35 

36 

37def _within(path: Path, roots: tuple[Path, ...]) -> bool: 

38 return any(path.is_relative_to(root) for root in roots) 

39 

40 

41def read_external_playlist( 

42 path: Path, root: Path, ctx: LibraryContext 

43) -> ExternalPlaylist: 

44 root = root.resolve() 

45 if path.is_symlink() or not path.resolve().is_relative_to(root): 

46 raise ValueError('Playlist must stay inside its configured folder') 

47 with path.open('rb') as stream: 

48 blob = stream.read(MAX_BYTES + 1) 

49 if len(blob) > MAX_BYTES: 

50 raise ValueError('Playlist exceeds the 2 MiB limit') 

51 try: 

52 text = blob.decode('utf-8-sig') 

53 except UnicodeDecodeError: 

54 if path.suffix.lower() == '.m3u8': 

55 raise ValueError('M3U8 must be UTF-8') from None 

56 text = blob.decode('cp1252') 

57 lines = text.splitlines() 

58 if len(lines) > MAX_LINES: 

59 raise ValueError('Playlist exceeds the entry limit') 

60 roots = tuple( 

61 folder.resolve() 

62 for folder in ( 

63 ctx.download_dir, 

64 *((ctx.slskd_dir,) if ctx.slskd_dir else ()), 

65 *(ctx.extra_dirs or ()), 

66 ) 

67 ) 

68 files: list[str] = [] 

69 seen: set[str] = set() 

70 missing = duplicates = 0 

71 for raw in lines: 

72 line = raw.strip() 

73 if not line or line.startswith('#'): 

74 continue 

75 if '\x00' in line or '://' in line: 

76 missing += 1 

77 continue 

78 entry = Path(line.replace('\\', '/')) 

79 # Check lexical containment before resolving: an absolute UNC path must 

80 # never initiate access to an unconfigured network share on Windows. 

81 target = Path( 

82 os.path.abspath( 

83 entry if entry.is_absolute() else path.parent / entry 

84 ) 

85 ) 

86 if not _within(target, roots): 

87 missing += 1 

88 continue 

89 try: 

90 full = target.resolve() 

91 if ( 

92 not _within(full, roots) 

93 or not full.is_file() 

94 or full.suffix.lower() not in AUDIO_EXTENSIONS 

95 ): 

96 missing += 1 

97 continue 

98 stored = library_stored_path( 

99 full, ctx.download_dir, ctx.slskd_dir, ctx.extra_dirs 

100 ) 

101 except OSError: 

102 missing += 1 

103 continue 

104 if stored in seen: 

105 duplicates += 1 

106 continue 

107 seen.add(stored) 

108 files.append(stored) 

109 identity = ( 

110 os.path.normcase(str(root)) + '\0' + path.relative_to(root).as_posix() 

111 ) 

112 return ExternalPlaylist( 

113 hashlib.sha256(identity.encode()).hexdigest(), 

114 path.parent.name if path.stem.casefold() == 'playlist' else path.stem, 

115 root.name, 

116 hashlib.sha256(blob).hexdigest(), 

117 tuple(files), 

118 missing, 

119 duplicates, 

120 ) 

121 

122 

123def discover_external_playlists( 

124 ctx: LibraryContext, 

125) -> tuple[list[ExternalPlaylist], list[str], bool]: 

126 found: list[ExternalPlaylist] = [] 

127 warnings: list[str] = [] 

128 visited = 0 

129 for raw_root in ctx.extra_dirs or (): 

130 root = raw_root.resolve() 

131 if not root.is_dir(): 

132 warnings.append(f'Folder unavailable: {root.name}') 

133 continue 

134 try: 

135 for path in root.rglob('*'): 

136 visited += 1 

137 if visited > MAX_SCAN_ENTRIES or len(found) >= MAX_CANDIDATES: 

138 return found, warnings, True 

139 if ( 

140 path.suffix.lower() not in {'.m3u', '.m3u8'} 

141 or not path.is_file() 

142 ): 

143 continue 

144 try: 

145 found.append(read_external_playlist(path, root, ctx)) 

146 except (OSError, ValueError, UnicodeError): 

147 warnings.append( 

148 f'Playlist unavailable or invalid: {path.name}' 

149 ) 

150 except OSError: 

151 warnings.append(f'Folder could not be fully scanned: {root.name}') 

152 return ( 

153 sorted(found, key=lambda item: (item.name.casefold(), item.source_id)), 

154 warnings, 

155 False, 

156 ) 

157 

158 

159class ExternalPlaylistImports: 

160 def __init__(self, db_path: str | Path | Database) -> None: 

161 self.db = as_database(db_path, 'library') 

162 if isinstance(db_path, Database): 

163 self.path = str(self.db.path) if self.db.path else None 

164 else: 

165 self.path = str(db_path) 

166 self.lock = threading.RLock() 

167 self._init_db() 

168 

169 def _init_db(self) -> None: 

170 # PostgreSQL gets this table from the migrations. 

171 if self.db.dialect is not Dialect.SQLITE: 

172 return 

173 with self.db.connect() as conn: 

174 conn.execute("""CREATE TABLE IF NOT EXISTS external_playlist_imports ( 

175 user_id INTEGER NOT NULL, source_id TEXT NOT NULL, 

176 playlist_name TEXT NOT NULL, revision TEXT NOT NULL, 

177 PRIMARY KEY(user_id, source_id))""") 

178 

179 def imported(self, user_id: int) -> dict[str, str]: 

180 with self.db.connect() as conn: 

181 rows = conn.execute( 

182 'SELECT source_id, playlist_name FROM external_playlist_imports WHERE user_id = ?', 

183 (user_id,), 

184 ).fetchall() 

185 return {row[0]: row[1] for row in rows} 

186 

187 def import_snapshot( 

188 self, 

189 ctx: LibraryContext, 

190 user_id: int, 

191 source: ExternalPlaylist, 

192 files: list[str], 

193 ) -> dict: 

194 with self.lock: 

195 previous = self.imported(user_id).get(source.source_id) 

196 owners = ManualPlaylistOwners(self.db) 

197 if ( 

198 previous 

199 and owners.owner_map().get(previous.casefold()) == user_id 

200 and m3u.find_m3u_for_name(ctx.download_dir, previous) 

201 ): 

202 return { 

203 'source_id': source.source_id, 

204 'status': 'already_present', 

205 'name': previous, 

206 } 

207 base = ( 

208 m3u.sanitize_playlist_name(source.name)[:110] 

209 or 'Imported playlist' 

210 ) 

211 if base.casefold() == 'downtify liked songs': 

212 base = 'Imported playlist' 

213 taken = existing_playlist_names(ctx.download_dir) 

214 name = base 

215 suffix = 2 

216 while name.casefold() in taken: 

217 name = f'{base} ({suffix})' 

218 suffix += 1 

219 result = copy_playlist( 

220 ctx, 

221 files, 

222 name, 

223 owners, 

224 user_id, 

225 PlaylistShares(self.db), 

226 ) 

227 with self.db.connect() as conn: 

228 conn.execute( 

229 'INSERT INTO external_playlist_imports VALUES (?, ?, ?, ?) ' 

230 'ON CONFLICT(user_id, source_id) DO UPDATE SET ' 

231 'playlist_name=excluded.playlist_name, revision=excluded.revision', 

232 ( 

233 user_id, 

234 source.source_id, 

235 result['name'], 

236 source.revision, 

237 ), 

238 ) 

239 return {'source_id': source.source_id, 'status': 'added', **result} 

240 

241 def rename(self, old: str, new: str) -> None: 

242 with self.db.connect() as conn: 

243 conn.execute( 

244 'UPDATE external_playlist_imports SET playlist_name = ? WHERE playlist_name = ?', 

245 (new, old), 

246 ) 

247 

248 def forget(self, name: str) -> None: 

249 with self.db.connect() as conn: 

250 conn.execute( 

251 'DELETE FROM external_playlist_imports WHERE playlist_name = ?', 

252 (name,), 

253 )