Skip to content

Commit

Permalink
serializer/library: serialize AlbumModel and more rpc API
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Mar 5, 2025
1 parent 22167b8 commit f19a0c5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
24 changes: 23 additions & 1 deletion feeluown/library/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
from feeluown.library.models import (
ModelFlags as MF, BaseModel, SimpleSearchResult,
BriefVideoModel, BriefSongModel, SongModel,
LyricModel, VideoModel, BriefAlbumModel, BriefArtistModel
LyricModel, VideoModel, BriefAlbumModel, BriefArtistModel,
AlbumModel,
)
from feeluown.library.model_state import ModelState
from feeluown.library.provider_protocol import (
check_flag as check_flag_impl,
SupportsSongLyric, SupportsSongMV, SupportsSongMultiQuality,
SupportsVideoMultiQuality, SupportsSongWebUrl, SupportsVideoWebUrl,
SupportsAlbumSongsReader,
)
from feeluown.library.standby import (
get_standby_score,
Expand Down Expand Up @@ -385,6 +387,26 @@ def song_get_lyric(self, song: BriefSongModel) -> Optional[LyricModel]:
def album_upgrade(self, album: BriefAlbumModel):
return self._model_upgrade(album)

def album_list_songs(self, album: BriefAlbumModel):
"""
:raises ResourceNotFound:
:raises ProviderIOError:
.. versionadded:: 4.1.10
"""
if not isinstance(album, AlbumModel):
ualbum = self.album_upgrade(album)
else:
ualbum = album
if ualbum.song_count == 0:
return []
if ualbum.songs:
return ualbum.songs
provider = self.get(ualbum.source)
if isinstance(provider, SupportsAlbumSongsReader):
return list(provider.album_create_songs_rd(ualbum))
raise ResourceNotFound(reason=ResourceNotFound.Reason.not_supported)

# --------
# Artist
# --------
Expand Down
8 changes: 5 additions & 3 deletions feeluown/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

from pydantic import (
ConfigDict, BaseModel as _BaseModel, PrivateAttr,
model_validator, model_serializer,
model_validator, model_serializer, Field,
)

try:
Expand Down Expand Up @@ -149,7 +149,9 @@ class BaseModel(_BaseModel):

# For pydantic v2.
if pydantic_version == 2:
model_config = ConfigDict(from_attributes=False, extra='forbid')
model_config = ConfigDict(from_attributes=False,
extra='forbid',
use_enum_values=True)
else:
# For pydantic v1.
class Config:
Expand Down Expand Up @@ -382,7 +384,7 @@ class AlbumModel(BriefAlbumModel, BaseNormalModel):
meta: Any = ModelMeta.create(ModelType.album, is_normal=True)
name: str
cover: str
type_: AlbumType = AlbumType.standard
type_: AlbumType = Field(default=AlbumType.standard, validate_default=True)
artists: List[BriefArtistModel]
# One album usually has limited songs, and many providers' album_detail API
# can return songs list. UPDATE(3.8.12): However, we found that albums
Expand Down
12 changes: 10 additions & 2 deletions feeluown/serializers/python.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from feeluown.library import BaseModel
from feeluown.library import BaseModel, AlbumType

from .typename import attach_typename, get_type_by_name, model_cls_list
from .base import Serializer, SerializerMeta, DeserializerError
Expand Down Expand Up @@ -61,7 +61,7 @@ class ModelSerializer(PythonSerializer, metaclass=SerializerMeta):
class Meta:
types = (BaseModel, )

def serialize(self, model):
def serialize(self, model: BaseModel):
return model.model_dump()


Expand Down Expand Up @@ -101,3 +101,11 @@ class Meta:

def serialize(self, object):
return object


class EnumSerializer(PythonSerializer, metaclass=SerializerMeta):
class Meta:
types = (AlbumType, )

def serialize(self, obj):
return obj.value
2 changes: 2 additions & 0 deletions feeluown/serializers/typename.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
BriefAlbumModel,
BriefPlaylistModel,
BriefUserModel,
AlbumType,
)

model_cls_list = [
Expand All @@ -38,6 +39,7 @@
_typenames = {
'player.Metadata': Metadata,
'app.App': App, # TODO: remove this
'library.AlbumType': AlbumType,
}
for model_cls in model_cls_list:
_typenames[f'library.{model_cls.__name__}'] = model_cls
Expand Down
1 change: 1 addition & 0 deletions tests/serializers/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_serialize_model():
assert song_js['__type__'] == 'feeluown.library.SongModel'
assert song_js['album']['__type__'] == 'feeluown.library.AlbumModel'
assert song_js['album']['uri'] == 'fuo://dummy/albums/1'
assert song_js['album']['type_'] == 'standard'


def test_serialize_search_result():
Expand Down

0 comments on commit f19a0c5

Please sign in to comment.