From 5b77e00e089b8591ae156f3b1088f08cac53f052 Mon Sep 17 00:00:00 2001 From: cyliuu Date: Thu, 6 Dec 2018 22:53:08 +0800 Subject: [PATCH] =?UTF-8?q?Local=20Schema=E4=BB=A3=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E8=A7=84=E8=8C=83=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fuocore/local/models.py | 2 +- fuocore/local/provider.py | 17 ++++---- fuocore/local/schemas.py | 85 ++++++++++++++++++++------------------ fuocore/netease/schemas.py | 5 +++ 4 files changed, 60 insertions(+), 49 deletions(-) diff --git a/fuocore/local/models.py b/fuocore/local/models.py index 6394e1f..0c44a8e 100644 --- a/fuocore/local/models.py +++ b/fuocore/local/models.py @@ -30,7 +30,7 @@ def __getattribute__(self, name): setattr(self, field, getattr(obj, field)) value = object.__getattribute__(self, name) elif name in cls._detail_fields and not value: - logger.warning('Field %s value is not None, but is %s' % (name, value)) + logger.debug('Field %s value is not None, but is %s' % (name, value)) return value diff --git a/fuocore/local/provider.py b/fuocore/local/provider.py index 4b726de..3a032d3 100644 --- a/fuocore/local/provider.py +++ b/fuocore/local/provider.py @@ -60,8 +60,10 @@ def create_song(fpath): schema = EasyMP3MetadataSongSchema(strict=True) metadata_dict = dict(metadata) + for key in metadata.keys(): + metadata_dict[key] = metadata_dict[key][0] if 'title' not in metadata_dict: - title = [fpath.rsplit('/')[-1].split('.')[0], ] + title = fpath.rsplit('/')[-1].split('.')[0] metadata_dict['title'] = title metadata_dict.update(dict( url=fpath, @@ -108,7 +110,6 @@ def run(self): # except Exception as e: # logger.warning(str(e)) - self._songs = [] for fpath in media_files: song = create_song(fpath) if song is not None: @@ -164,10 +165,8 @@ def setup_library(self, scanner_songs): if album.identifier not in self._albums: album_data = {'identifier': album.identifier, 'name': album.name, + 'artists_name': album.artists[0].name if album.artists else '', 'songs': []} - if album.artists: - album_data['artists'] = [{'identifier': album.artists[0].identifier, - 'name': album.artists[0].name}] self._albums[album.identifier], _ = LocalAlbumSchema(strict=True).load(album_data) self._albums[album.identifier].songs.append(song) @@ -183,7 +182,8 @@ def setup_library(self, scanner_songs): def analyze_library(self): for album in self._albums.values(): - if album.artists: + # album.songs.sort(key=lambda x: (int(x.disc.split('/')[0]), int(x.track.split('/')[0]))) + if album.artists is not None: album_artist = album.artists[0] if album_artist.identifier not in self._artists: album_artist_data = {'identifier': album_artist.identifier, @@ -192,10 +192,11 @@ def analyze_library(self): 'albums': []} self._artists[album_artist.identifier], _ = LocalArtistSchema(strict=True).load(album_artist_data) self._artists[album_artist.identifier].albums.append(album) + for artist in self._artists.values(): - # if artist.albums: + # if artist.albums is not []: # artist.albums.sort(key=lambda x: (x.songs[0].date is None, x.songs[0].date), reverse=True) - if artist.songs: + if artist.songs is not []: artist.songs.sort(key=lambda x: x.title) diff --git a/fuocore/local/schemas.py b/fuocore/local/schemas.py index 2269a64..c358530 100644 --- a/fuocore/local/schemas.py +++ b/fuocore/local/schemas.py @@ -8,7 +8,7 @@ class BaseSchema(Schema): - identifier = fields.Field(required=True) + identifier = fields.Field(required=True, missing=None) desc = fields.Str() @@ -21,6 +21,8 @@ class LocalArtistSchema(BaseSchema): @post_load def create_model(self, data): + if data['identifier'] is None: + data['identifier'] = str(elfhash(base64.b64encode(bytes(data['name'], 'utf-8')))) return LArtistModel(**data) @@ -28,11 +30,20 @@ class LocalAlbumSchema(BaseSchema): name = fields.Str(required=True) img = fields.Str() songs = fields.List(fields.Nested('LocalSongSchema'), missing=None) - artists = fields.List(fields.Nested(LocalArtistSchema), missing=[]) + artists = fields.List(fields.Nested(LocalArtistSchema), missing=None) + + artists_name = fields.Str() @post_load def create_model(self, data): - return LAlbumModel(**data) + if data['identifier'] is None: + identifier_str = '{} - {}'.format(data['name'], data['artists_name']) + data['identifier'] = str(elfhash(base64.b64encode(bytes(identifier_str, 'utf-8')))) + album = LAlbumModel(**data) + if album.artists is None and data['artists_name']: + album_artist, _ = LocalArtistSchema(strict=True).load({'name': data['artists_name']}) + album.artists = [album_artist] + return album class LocalSongSchema(BaseSchema): @@ -40,7 +51,7 @@ class LocalSongSchema(BaseSchema): url = fields.Str(required=True) duration = fields.Float(required=True) # mileseconds album = fields.Nested(LocalAlbumSchema, missing=None) - artists = fields.List(fields.Nested(LocalArtistSchema), missing=[]) + artists = fields.List(fields.Nested(LocalArtistSchema), missing=None) @post_load def create_model(self, data): @@ -49,49 +60,43 @@ def create_model(self, data): class EasyMP3MetadataSongSchema(Schema): """EasyMP3 metadata""" url = fields.Str(required=True) - title_list = fields.List(fields.Str(), load_from='title', required=True) duration = fields.Float(required=True) - artist_list = fields.List(fields.Str(), load_from='artist') - # genre_list = fields.List(fields.Str(), load_from='genre') - album_list = fields.List(fields.Str(), load_from='album') - album_artist_list = fields.List(fields.Str(), load_from='albumartist') + title = fields.Str(required=True, missing='Unknown') + artists_name = fields.Str(required=True, load_from='artist', missing='') + album_name = fields.Str(required=True, load_from='album', missing='') + album_artist_name = fields.Str(required=True, load_from='albumartist', missing='') + # track = fields.Str(load_from='tracknumber') + # disc = fields.Str(load_from='discnumber') + # date = fields.Str() + # genre = fields.Str() @post_load def create_model(self, data): # FIXME: 逻辑太多,请重构我,重构之前这里不应该添加新功能 - title = data['title_list'][0] if data.get('title_list') else 'Unknown' - artists_name = data['artist_list'][0] if data.get('artist_list') else '' - album_name = data['album_list'][0] if data.get('album_list') else '' # NOTE: use {title}-{artists_name}-{album_name} as song identifier - song_identifier_str = '{} - {} - {} - {}'.format(title, artists_name, album_name, data['duration']) - song_identifier = str(elfhash(base64.b64encode(bytes(song_identifier_str, 'utf-8')))) - song_data = { - 'identifier': song_identifier, - 'title': title, - 'duration': data['duration'], - 'url': data['url'] - } - - if album_name: - album_artist_name = data['album_artist_list'][0] if data.get('album_artist_list') else '' - album_identifier_str = '{} - {}'.format(album_name, album_artist_name) - album_identifier = str(elfhash(base64.b64encode(bytes(album_identifier_str, 'utf-8')))) - song_data['album'] = {'identifier': album_identifier, - 'name': album_name} - if album_artist_name: - album_artist_identifier = str(elfhash(base64.b64encode(bytes(album_artist_name, 'utf-8')))) - song_data['album']['artists'] = [{'identifier': album_artist_identifier, - 'name': album_artist_name}] - - if artists_name: - song_data['artists'] = [] - artist_names = [artist.strip() for artist in re.split(r'[,&]', artists_name)] + identifier_str = '{} - {} - {} - {}'.format(data['title'], data['artists_name'], data['album_name'], + data['duration']) + data['identifier'] = str(elfhash(base64.b64encode(bytes(identifier_str, 'utf-8')))) + song, _ = LocalSongSchema(strict=True).load(data) + + if song.album is None and data['album_name']: + album_data = {'name': data['album_name'], + 'artists_name': data['album_artist_name']} + song.album, _ = LocalAlbumSchema(strict=True).load(album_data) + + if song.artists is None and data['artists_name']: + song.artists = [] + artist_names = [artist.strip() for artist in re.split(r'[,&]', data['artists_name'])] for artist_name in artist_names: - artist_identifier = str(elfhash(base64.b64encode(bytes(artist_name, 'utf-8')))) - song_data['artists'].append({'identifier': artist_identifier, - 'name': artist_name}) - - song, _ = LocalSongSchema(strict=True).load(song_data) + artist_data = {'name': artist_name} + artist, _ = LocalArtistSchema(strict=True).load(artist_data) + song.artists.append(artist) + + # song.genre = data.get('genre', None) + # if song.album is not None: + # song.disc = data.get('disc', '1/1') + # song.track = data.get('track', '1/1') + # song.date = data.get('date', None) return song diff --git a/fuocore/netease/schemas.py b/fuocore/netease/schemas.py index 1b97a73..5a8a0e1 100644 --- a/fuocore/netease/schemas.py +++ b/fuocore/netease/schemas.py @@ -23,6 +23,11 @@ class NeteaseAlbumSchema(Schema): @post_load def create_model(self, data): + for song in data.get('songs', ()): + # song.album.songs 这里会返回空列表,设置为 None + if song.album: + song.album.cover = None + song.album.songs = None return NAlbumModel(**data)