def get_local_songs( filepaths, *, filters=None, max_depth=math.inf, exclude_paths=None, exclude_regexes=None, exclude_globs=None ): def _exclude_paths(path, exclude_paths): return any( str(Path(exclude_path)) in str(path) for exclude_path in exclude_paths ) def _exclude_regexes(path, exclude_regexes): return any( re.search(regex, str(path)) for regex in exclude_regexes ) local_songs = [] for filepath in filepaths: if ( _exclude_paths(filepath, exclude_paths) or _exclude_regexes(filepath, exclude_regexes) ): continue if filepath.is_dir(): start_level = len(filepath.parts) exclude_files = set() for exclude_glob in exclude_globs: exclude_files |= set(filepath.rglob(exclude_glob)) for path in filepath.glob('**/*'): if ( path.is_file() and len(path.parent.parts) - start_level <= max_depth and path not in exclude_files and not _exclude_paths(path, exclude_paths) and not _exclude_regexes(path, exclude_regexes) ): with path.open('rb') as f: if audio_metadata.determine_format( f.read(4), extension=path.suffix ) is not None: local_songs.append(path) elif filepath.is_file(): with filepath.open('rb') as f: if audio_metadata.determine_format( f.read(4), extension=filepath.suffix ) is not None: local_songs.append(filepath) matched_songs = filter_metadata(local_songs, filters) return matched_songs
def get_local_songs(paths, *, filters=None, max_depth=math.inf, exclude_paths=None, exclude_regexes=None, exclude_globs=None): logger.log('NORMAL', "Loading local songs") local_songs = [ filepath for filepath in get_filepaths(paths, max_depth=max_depth, exclude_paths=exclude_paths, exclude_regexes=exclude_regexes, exclude_globs=exclude_globs) if audio_metadata.determine_format(filepath) in [ audio_metadata.FLAC, audio_metadata.MP3, audio_metadata.OggOpus, audio_metadata.OggVorbis, audio_metadata.WAVE, ] ] logger.info("Found {} local songs", len(local_songs)) matched_songs = filter_metadata(local_songs, filters) return matched_songs
def test_determine_format_non_audio(): assert audio_metadata.determine_format(__file__) is None
def test_determine_format_pathobj_with_wrong_extension(): for fp in test_filepaths: assert audio_metadata.determine_format(fp, extension='.py') is None
def test_determine_format_pathobj_with_extension(): for fp in test_filepaths: assert issubclass( audio_metadata.determine_format(fp, extension=fp.suffix), audio_metadata.Format)
def test_determine_format_pathobj_no_extension(): for fp in test_filepaths: assert issubclass(audio_metadata.determine_format(fp), audio_metadata.Format)
def test_determine_format_bytes(): for fp in test_filepaths: assert issubclass(audio_metadata.determine_format(fp.read_bytes()), audio_metadata.Format)
def test_determine_format_fileobj(): for fp in test_filepaths: assert issubclass(audio_metadata.determine_format(fp.open('rb')), audio_metadata.Format)
def test_determine_format_invalid_value(): assert audio_metadata.determine_format([__file__]) is None
def test_determine_format_id3v2_no_mp3_frames(): assert audio_metadata.determine_format( b'ID3\x04\x00\x80\x00\x00\x00\x00\x00\xff\xfb') is None