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

Commit

Permalink
优化 netease 和 qq 音乐 Model 实现
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Jan 12, 2019
1 parent c4e6e3d commit 103b194
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
41 changes: 16 additions & 25 deletions fuocore/netease/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ class NeteaseSongSchema(Schema):

@post_load
def create_model(self, data):
album = data['album']
artists = data['artists']

# 在所有的接口中,song.album.songs 要么是一个空列表,
# 要么是 null,这里统一置为 None。
album.songs = None

# 在有的接口中(比如歌单列表接口),album cover 的值是不对的,
# 它会指向的是一个网易云默认的灰色图片,我们将其设置为 None,
# artist cover 也有类似的问题。
album.cover = None

if artists:
for artist in artists:
artist.cover = None

return NSongModel(**data)


Expand All @@ -23,11 +39,6 @@ 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)


Expand All @@ -39,11 +50,6 @@ class NeteaseArtistSchema(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 NArtistModel(**data)


Expand All @@ -60,21 +66,6 @@ class NeteasePlaylistSchema(Schema):

@post_load
def create_model(self, data):
if data.get('songs') is None:
data.pop('songs')
else:
# 这里 Artist 和 Album 的 picUrl 链接不对,
# 这个链接指向的是一个网易云默认的灰色图片,
# 我们将其设置为 None
#
# 另外,song.album.songs 这里会返回空列表,设置为 None
for song in data['songs']:
if song.artists:
for artist in song.artists:
artist.cover = None
if song.album:
song.album.cover = None
song.album.songs = None
if data.get('desc') is None:
data['desc'] = ''
return NPlaylistModel(**data)
Expand Down
8 changes: 8 additions & 0 deletions fuocore/qqmusic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def __init__(self, timeout=1):
'Chrome/66.0.3359.181 Mobile Safari/537.36',
}

def get_cover(self, mid, type_):
"""获取专辑、歌手封面
:param type_: 专辑: 2,歌手:1
"""
return 'http://y.gtimg.cn/music/photo_new/T00{}R800x800M000{}.jpg' \
.format(type_, mid)

def get_song_detail(self, song_id):
song_id = int(song_id)
url = 'http://u.y.qq.com/cgi-bin/musicu.fcg'
Expand Down
11 changes: 7 additions & 4 deletions fuocore/qqmusic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
AlbumModel,
ArtistModel,
SearchModel,
ModelStage,
)

from .provider import provider


class QQBaseModel(BaseModel):
_api = provider.api
_detail_fields = ()

class Meta:
allow_get = True
provider = provider
fields = ('mid', )

@classmethod
def get(cls, identifier):
Expand All @@ -26,14 +27,14 @@ def get(cls, identifier):
def _deserialize(data, schema_cls):
schema = schema_cls(strict=True)
obj, _ = schema.load(data)
# XXX: 将 model 设置为 gotten,减少代码编写时的心智负担,
# 避免在调用 get 方法时进入无限递归。
obj.stage = ModelStage.gotten
return obj


class QQSongModel(SongModel, QQBaseModel):

class Meta:
fields = ('mid', )

@classmethod
def get(cls, identifier):
data = cls._api.get_song_detail(identifier)
Expand Down Expand Up @@ -61,6 +62,7 @@ class QQAlbumModel(AlbumModel, QQBaseModel):
def get(cls, identifier):
data_album = cls._api.album_detail(identifier)
album = _deserialize(data_album, QQAlbumSchema)
album.cover = cls._api.get_cover(album.mid, 2)
return album


Expand All @@ -69,6 +71,7 @@ class QQArtistModel(ArtistModel, QQBaseModel):
def get(cls, identifier):
data_artist = cls._api.artist_detail(identifier)
artist = _deserialize(data_artist, QQArtistSchema)
artist.cover = cls._api.get_cover(artist.mid, 1)
return artist


Expand Down
3 changes: 3 additions & 0 deletions fuocore/qqmusic/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class QQArtistSchema(Schema):
"""歌手详情 Schema、歌曲歌手简要信息 Schema"""

identifier = fields.Int(load_from='singer_id', required=True)
mid = fields.Str(load_from='singer_mid', required=True)
name = fields.Str(load_from='singer_name', required=True)

desc = fields.Str(load_from='SingerDesc')
Expand All @@ -64,6 +65,7 @@ def create_model(self, data):

class QQAlbumSchema(Schema):
identifier = fields.Int(load_from='id', required=True)
mid = fields.Str(load_from='mid', required=True)
name = fields.Str(required=True)
desc = fields.Str(required=True)

Expand All @@ -78,6 +80,7 @@ def create_model(self, data):
artist = QQArtistModel(identifier=data['artist_id'],
name=data['artist_name'])
album = QQAlbumModel(identifier=data['identifier'],
mid=data['mid'],
name=data['name'],
desc=data['desc'],
songs=data['songs'] or [],
Expand Down

0 comments on commit 103b194

Please sign in to comment.