From f6d4c339d0c2207dabc75c5ee3afad79923fe01c Mon Sep 17 00:00:00 2001 From: cosven Date: Tue, 11 Dec 2018 09:23:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E7=BC=93=E5=AD=98=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E7=BB=99=E6=8E=92=E5=BA=8F=E5=AE=B9=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fuocore/local/provider.py | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/fuocore/local/provider.py b/fuocore/local/provider.py index 213943b..1d54676 100644 --- a/fuocore/local/provider.py +++ b/fuocore/local/provider.py @@ -20,8 +20,6 @@ logger = logging.getLogger(__name__) -MUSIC_LIBRARY_PATH = os.path.expanduser('~') + '/Music' -CACHE_DIR = os.path.expanduser('~') + '/.FeelUOwn/data' def scan_directory(directory, exts=None, depth=2): @@ -54,8 +52,7 @@ def create_song(fpath): elif fpath.endswith('m4a'): metadata = EasyMP4(fpath) except MutagenError as e: - logger.error('Mutagen parse metadata failed, ignore.') - logger.debug(str(e)) + logger.exception('Mutagen parse metadata failed, ignore.') return None schema = EasyMP3MetadataSongSchema(strict=True) @@ -77,12 +74,14 @@ def create_song(fpath): class Scanner: + """本地歌曲扫描器""" + + DEFAULT_MUSIC_FOLDER = os.path.expanduser('~') + '/Music' + def __init__(self, paths=None, depth=2): self._songs = [] - - #: music resource paths to be scanned, list self.depth = depth - self.paths = paths or [MUSIC_LIBRARY_PATH] + self.paths = paths or [Scanner.DEFAULT_MUSIC_FOLDER] @property def songs(self): @@ -100,16 +99,6 @@ def run(self): logger.debug('正在扫描目录(%s)...', directory) media_files.extend(scan_directory(directory, exts, depth)) - db_name = CACHE_DIR + '/local_song_info.db' - try: - with open(db_name, 'rb') as file_object: - historty_media_files, songs = pickle.load(file_object) - if (set(historty_media_files) == set(media_files)): - self._songs = songs - return - except Exception as e: - logger.warning(str(e)) - self._songs = [] for fpath in media_files: song = create_song(fpath) @@ -119,10 +108,6 @@ def run(self): logger.warning('%s can not be recognized', fpath) logger.debug('扫描到 %d 首歌曲', len(self._songs)) - with open(db_name, 'wb') as file_object: - if media_files: - pickle.dump((media_files, self._songs), file_object) - class DataBase: def __init__(self): @@ -183,7 +168,11 @@ def setup_library(self, scanner_songs): def analyze_library(self): for album in self._albums.values(): - album.songs.sort(key=lambda x: (int(x.disc.split('/')[0]), int(x.track.split('/')[0]))) + try: + album.songs.sort(key=lambda x: (int(x.disc.split('/')[0]), int(x.track.split('/')[0]))) + except Exception as e: + logger.exception('Sort album songs failed.') + if album.artists is not None: album_artist = album.artists[0] if album_artist.identifier not in self._artists: @@ -195,9 +184,9 @@ def analyze_library(self): self._artists[album_artist.identifier].albums.append(album) for artist in self._artists.values(): - if artist.albums is not []: + if artist.albums: artist.albums.sort(key=lambda x: (x.songs[0].date is None, x.songs[0].date), reverse=True) - if artist.songs is not []: + if artist.songs: artist.songs.sort(key=lambda x: x.title)