-
Notifications
You must be signed in to change notification settings - Fork 25
/
commands.py
299 lines (252 loc) · 9.51 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import logging
import pathlib
import time
from mopidy import commands
from mopidy.audio import scan, tags
from mopidy_local import mtimes, storage, translator
logger = logging.getLogger(__name__)
MIN_DURATION_MS = 100 # Shortest length of track to include.
class LocalCommand(commands.Command):
def __init__(self):
super().__init__()
self.add_child("scan", ScanCommand())
self.add_child("clear", ClearCommand())
class ClearCommand(commands.Command):
help = "Clear local media files from the local library."
def run(self, args, config):
library = storage.LocalStorageProvider(config)
prompt = "Are you sure you want to clear the library? [y/N] "
if input(prompt).lower() != "y":
print("Clearing library aborted")
return 0
if library.clear():
print("Library successfully cleared")
return 0
print("Unable to clear library")
return 1
class ScanCommand(commands.Command):
help = "Scan local media files and populate the local library."
def __init__(self):
super().__init__()
self.add_argument(
"--limit",
action="store",
type=int,
dest="limit",
default=None,
help="Maximum number of tracks to scan",
)
self.add_argument(
"--force",
action="store_true",
dest="force",
default=False,
help="Force rescan of all media files",
)
def run(self, args, config):
media_dir = pathlib.Path(config["local"]["media_dir"]).resolve()
library = storage.LocalStorageProvider(config)
file_mtimes = self._find_files(
media_dir=media_dir,
follow_symlinks=config["local"]["scan_follow_symlinks"],
)
files_to_update, files_in_library = self._check_tracks_in_library(
media_dir=media_dir,
file_mtimes=file_mtimes,
library=library,
force_rescan=args.force,
)
files_to_update.update(
self._find_files_to_scan(
media_dir=media_dir,
file_mtimes=file_mtimes,
files_in_library=files_in_library,
included_file_exts=[
file_ext.lower()
for file_ext in config["local"]["included_file_extensions"]
],
excluded_file_exts=[
file_ext.lower()
for file_ext in config["local"]["excluded_file_extensions"]
],
)
)
self._scan_metadata(
media_dir=media_dir,
file_mtimes=file_mtimes,
files=files_to_update,
library=library,
timeout=config["local"]["scan_timeout"],
flush_threshold=config["local"]["scan_flush_threshold"],
limit=args.limit,
)
library.close()
return 0
def _find_files(self, *, media_dir, follow_symlinks):
logger.info(f"Finding files in {media_dir.as_uri()} ...")
file_mtimes, file_errors = mtimes.find_mtimes(
media_dir, follow=follow_symlinks
)
logger.info(f"Found {len(file_mtimes)} files in {media_dir.as_uri()}")
if file_errors:
logger.warning(
f"Encountered {len(file_errors)} errors "
f"while finding files in {media_dir.as_uri()}"
)
for path in file_errors:
logger.warning(f"Error for {path.as_uri()}: {file_errors[path]}")
return file_mtimes
def _check_tracks_in_library(
self, *, media_dir, file_mtimes, library, force_rescan
):
num_tracks = library.load()
logger.info(f"Checking {num_tracks} tracks from library")
uris_to_remove = set()
files_to_update = set()
files_in_library = set()
for track in library.begin():
absolute_path = translator.local_uri_to_path(track.uri, media_dir)
mtime = file_mtimes.get(absolute_path)
if mtime is None:
logger.debug(f"Removing {track.uri}: File not found")
uris_to_remove.add(track.uri)
elif mtime > track.last_modified or force_rescan:
files_to_update.add(absolute_path)
files_in_library.add(absolute_path)
logger.info(f"Removing {len(uris_to_remove)} missing tracks")
for local_uri in uris_to_remove:
library.remove(local_uri)
return files_to_update, files_in_library
def _find_files_to_scan(
self,
*,
media_dir,
file_mtimes,
files_in_library,
included_file_exts,
excluded_file_exts,
):
files_to_update = set()
def _is_hidden_file(relative_path, file_uri):
if any(p.startswith(".") for p in relative_path.parts):
logger.debug(f"Skipped {file_uri}: Hidden directory/file")
return True
else:
return False
def _extension_filters(
relative_path, file_uri, included_file_exts, excluded_file_exts
):
if included_file_exts:
if relative_path.suffix.lower() in included_file_exts:
logger.debug(
f"Added {file_uri}: File extension on included list"
)
return True
else:
logger.debug(
f"Skipped {file_uri}: File extension not on included list"
)
return False
else:
if relative_path.suffix.lower() in excluded_file_exts:
logger.debug(
f"Skipped {file_uri}: File extension on excluded list"
)
return False
else:
logger.debug(
f"Included {file_uri}: File extension not on excluded list"
)
return True
for absolute_path in file_mtimes:
relative_path = absolute_path.relative_to(media_dir)
file_uri = absolute_path.as_uri()
if (
not _is_hidden_file(relative_path, file_uri)
and _extension_filters(
relative_path,
file_uri,
included_file_exts,
excluded_file_exts,
)
and absolute_path not in files_in_library
):
files_to_update.add(absolute_path)
logger.info(
f"Found {len(files_to_update)} tracks which need to be updated"
)
return files_to_update
def _scan_metadata(
self,
*,
media_dir,
file_mtimes,
files,
library,
timeout,
flush_threshold,
limit,
):
logger.info("Scanning...")
files = sorted(files)[:limit]
scanner = scan.Scanner(timeout)
progress = _ScanProgress(batch_size=flush_threshold, total=len(files))
for absolute_path in files:
try:
file_uri = absolute_path.as_uri()
result = scanner.scan(file_uri)
if not result.playable:
logger.warning(
f"Failed scanning {file_uri}: No audio found in file"
)
elif result.duration is None:
logger.warning(
f"Failed scanning {file_uri}: "
"No duration information found in file"
)
elif result.duration < MIN_DURATION_MS:
logger.warning(
f"Failed scanning {file_uri}: "
f"Track shorter than {MIN_DURATION_MS}ms"
)
else:
local_uri = translator.path_to_local_track_uri(
absolute_path, media_dir
)
mtime = file_mtimes.get(absolute_path)
track = tags.convert_tags_to_track(result.tags).replace(
uri=local_uri,
length=result.duration,
last_modified=mtime,
)
library.add(track, result.tags, result.duration)
logger.debug(f"Added {track.uri}")
except Exception as error:
logger.warning(f"Failed scanning {file_uri}: {error}")
if progress.increment():
progress.log()
if library.flush():
logger.debug("Progress flushed")
progress.log()
logger.info("Done scanning")
class _ScanProgress:
def __init__(self, *, batch_size, total):
self.count = 0
self.batch_size = batch_size
self.total = total
self.start = time.time()
def increment(self):
self.count += 1
return self.batch_size and self.count % self.batch_size == 0
def log(self):
duration = time.time() - self.start
if self.count >= self.total or not self.count:
logger.info(
f"Scanned {self.count} of {self.total} files in {duration:.3f}s."
)
else:
remainder = duration / self.count * (self.total - self.count)
logger.info(
f"Scanned {self.count} of {self.total} files "
f"in {duration:.3f}s, ~{remainder:.0f}s left"
)