Skip to content

Commit

Permalink
introduce Collection dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Apr 14, 2024
1 parent 1fb7d79 commit c4c62ea
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
13 changes: 4 additions & 9 deletions feeluown/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
from datetime import datetime
from enum import Enum
from pathlib import Path
from typing import Dict, Iterable, List

Expand All @@ -12,7 +11,7 @@
from feeluown.consts import COLLECTIONS_DIR
from feeluown.utils.dispatch import Signal
from feeluown.library import resolve, reverse, ResolverNotFound, \
ResolveFailed, ModelState
ResolveFailed, ModelState, CollectionType
from feeluown.utils.utils import elfhash

logger = logging.getLogger(__name__)
Expand All @@ -33,14 +32,10 @@ class CollectionAlreadyExists(Exception):
pass


class CollectionType(Enum):
sys_library = 16
sys_pool = 13

mixed = 8


class Collection:
"""
TODO: This collection should be moved into local provider.
"""

def __init__(self, fpath):
# TODO: 以后考虑添加 identifier 字段,identifier
Expand Down
4 changes: 2 additions & 2 deletions feeluown/gui/pages/homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from PyQt5.QtWidgets import QWidget, QVBoxLayout

from feeluown.library import SupportsRecListDailyPlaylists, SupportsRecACollection
from feeluown.library import SupportsRecListDailyPlaylists, SupportsRecACollectionOfSongs
from feeluown.utils.reader import create_reader
from feeluown.utils.aio import run_fn, as_completed
from feeluown.gui.widgets.header import LargeHeader
Expand Down Expand Up @@ -111,7 +111,7 @@ async def _get_rec_songs(self):
songs = []
for coro in as_completed([
run_fn(provider.rec_a_collection) for provider in providers
if isinstance(provider, SupportsRecACollection)
if isinstance(provider, SupportsRecACollectionOfSongs)
]):
try:
title, songs_ = await coro
Expand Down
1 change: 1 addition & 0 deletions feeluown/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
Resolver, reverse, resolve, ResolverNotFound, ResolveFailed,
parse_line, NS_TYPE_MAP,
)
from .collection import Collection, CollectionType
38 changes: 38 additions & 0 deletions feeluown/library/collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from enum import Enum
from dataclasses import dataclass
from typing import List

from .models import BaseModel


class CollectionType(Enum):
"""Collection type enumeration"""

# These two values are only used in local collection.
sys_library = 16
sys_pool = 13

only_songs = 1
only_artists = 2
only_albums = 3
only_playlists = 4
only_lyrics = 5
only_videos = 6

only_users = 17
only_comments = 18

mixed = 32


@dataclass
class Collection:
"""
Differences between a collection and a playlist
- A collection has no identifier in general.
- A collection may have songs, albums and artists.
"""
name: str
type_: CollectionType
models: List[BaseModel]
description: str = ''
5 changes: 3 additions & 2 deletions feeluown/library/provider_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PlaylistModel, UserModel, ModelType, BriefArtistModel, BriefSongModel,
LyricModel, BriefVideoModel,
)
from .collection import Collection

from .flags import Flags as PF

Expand Down Expand Up @@ -385,9 +386,9 @@ def rec_list_daily_songs(self) -> List[SongModel]:


@runtime_checkable
class SupportsRecACollection(Protocol):
class SupportsRecACollectionOfSongs(Protocol):
@abstractmethod
def rec_a_collection(self) -> Tuple[str, List[BriefSongModel]]:
def rec_a_collection_of_songs(self) -> Collection:
"""
For example, providers may provider a list of songs,
and the title looks like “大家都在听” / “红心歌曲”.
Expand Down
10 changes: 6 additions & 4 deletions feeluown/player/lyric.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ def parse_lyric_text(content: str) -> Dict[int, str]:
"""
Reference: https://github.com/osdlyrics/osdlyrics/blob/master/python/lrc.py
>>> parse_lyric_text("[00:00.00] 作曲 : 周杰伦\\n[00:01.00] 作词 : 周杰伦\\n")
OrderedDict([(0, ' 作曲 : 周杰伦'), (1000, ' 作词 : 周杰伦')])
>>> parse_lyric_text("[01:30][00:01:10][01:00]再等直至再吻到你")
OrderedDict([(60000, '再等直至再吻到你'), (70000, '再等直至再吻到你'), (90000, '再等直至再吻到你')])
>>> r = parse_lyric_text("[00:00.00] 作曲 : 周杰伦\\n[00:01.00] 作词 : 周杰伦\\n")
>>> list(r.items())[0]
(0, ' 作曲 : 周杰伦')
>>> r = parse_lyric_text("[01:30][00:01:10][01:00]再等直至再吻到你")
>>> list(r.items())[-1]
(90000, '再等直至再吻到你')
"""
def to_mileseconds(time_str):
mileseconds = 0
Expand Down

0 comments on commit c4c62ea

Please sign in to comment.