refactor: convert Enum to StrEnum and reduce some duplicated code

This commit is contained in:
Puqns67 2024-12-04 20:20:32 +08:00
parent 72a5448976
commit 4c01aa7467
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
2 changed files with 22 additions and 22 deletions

View File

@ -1,9 +1,9 @@
from enum import Enum, auto
from enum import StrEnum, auto
__all__ = ["LrcType", "LrcMetaType", "LinkType"]
class LrcType(Enum):
class LrcType(StrEnum):
Origin = auto()
Translation = auto()
Romaji = auto()
@ -17,18 +17,27 @@ class LrcType(Enum):
case LrcType.Romaji:
return ""
def ncmAPIString(self) -> str:
match self:
case LrcType.Origin:
return "lrc"
case LrcType.Translation:
return "tlyric"
case LrcType.Romaji:
return "romalrc"
class LrcMetaType(Enum):
class LrcMetaType(StrEnum):
Title = "ti"
Artist = "ar"
Album = "al"
Author = "au"
Length = "length"
LrcAuthor = "by"
Author = "by"
Offset = "offset"
class LinkType(Enum):
Track = "track"
Album = "album"
Playlist = "playlist"
class LinkType(StrEnum):
Track = auto()
Album = auto()
Playlist = auto()

View File

@ -157,20 +157,11 @@ class NCMLyrics:
lyrics: dict[LrcType, str] = {}
try:
lyrics[LrcType.Origin] = data["lrc"]["lyric"]
except KeyError:
pass
try:
lyrics[LrcType.Translation] = data["tlyric"]["lyric"]
except KeyError:
pass
try:
lyrics[LrcType.Romaji] = data["romalrc"]["lyric"]
except KeyError:
pass
for lrctype in LrcType:
try:
lyrics[lrctype] = data[lrctype.ncmAPIString()]["lyric"]
except KeyError:
pass
return cls(
id=None,