Skip to content
This repository has been archived by the owner on Jun 30, 2019. It is now read-only.

Commit

Permalink
去掉缓存逻辑,给排序容错
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Dec 11, 2018
1 parent 4819d34 commit f6d4c33
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions fuocore/local/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)


Expand Down

0 comments on commit f6d4c33

Please sign in to comment.