chore: remove dead code, fix typos

This commit is contained in:
Puqns67 2024-10-15 20:30:38 +08:00
parent 3c8a46d039
commit 2cdda43cea
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
6 changed files with 22 additions and 23 deletions

View File

@ -11,7 +11,7 @@ from .enum import LinkType
from .error import UnsupportedLinkError from .error import UnsupportedLinkError
from .lrc import Lrc from .lrc import Lrc
from .object import NCMTrack from .object import NCMTrack
from .util import Link, parseLink, pickOutput from .util import parseLink, pickOutput
NCMLyricsAppTheme = Theme( NCMLyricsAppTheme = Theme(
{ {
@ -87,11 +87,9 @@ def main(outputs: list[Path], exist: bool, overwrite: bool, quiet: bool, links:
app = NCMLyricsApp(console=console, outputs=outputs, exist=exist, overwrite=overwrite, quiet=quiet, tracks=[]) app = NCMLyricsApp(console=console, outputs=outputs, exist=exist, overwrite=overwrite, quiet=quiet, tracks=[])
for link in links: for link in links:
parsed: Link | None = None
try: try:
parsed = parseLink(link) parsed = parseLink(link)
except UnsupportLinkError: except UnsupportedLinkError:
continue continue
match parsed.type: match parsed.type:

View File

@ -114,4 +114,3 @@ class NCMApi:
request = self._httpClient.build_request("GET", "/song/lyric/v1", params=params) request = self._httpClient.build_request("GET", "/song/lyric/v1", params=params)
return NCMLyrics.fromApi(self._fetch(request)).withId(trackId) return NCMLyrics.fromApi(self._fetch(request)).withId(trackId)
return NCMLyrics.fromApi(response.json())

View File

@ -8,7 +8,7 @@ class LrcType(Enum):
Translation = auto() Translation = auto()
Romaji = auto() Romaji = auto()
def preety(self) -> str: def pretty(self) -> str:
match self: match self:
case LrcType.Origin: case LrcType.Origin:
return "" return ""

View File

@ -25,7 +25,7 @@ class ParseLinkError(NCMLyricsAppError):
"""无法解析此分享链接""" """无法解析此分享链接"""
class UnsupportLinkError(NCMLyricsAppError): class UnsupportedLinkError(NCMLyricsAppError):
"""不支持的分享链接""" """不支持的分享链接"""

View File

@ -14,8 +14,8 @@ __all__ = ["LrcType", "LrcMetaType", "Lrc"]
LRC_RE_COMMIT = reCompile(r"^\s*#") LRC_RE_COMMIT = reCompile(r"^\s*#")
LRC_RE_META = reCompile(r"^\s*\[(?P<type>ti|ar|al|au|length|by|offset):\s*(?P<content>.+?)\s*\]\s*$") LRC_RE_META = reCompile(r"^\s*\[(?P<type>ti|ar|al|au|length|by|offset):\s*(?P<content>.+?)\s*\]\s*$")
LRC_RE_META_NCM_SPICAL = reCompile(r"^\s*\{.*\}\s*$") LRC_RE_META_NCM_SPECIAL = reCompile(r"^\s*\{.*\}\s*$")
LRC_RE_LYRIC = reCompile(r"^\s*(?P<timelabels>(?:\s*\[\d{1,2}:\d{1,2}(?:\.\d{1,3})?\])+)\s*(?P<lyric>.+?)\s*$") LRC_RE_LYRIC = reCompile(r"^\s*(?P<timeLabels>(?:\s*\[\d{1,2}:\d{1,2}(?:\.\d{1,3})?\])+)\s*(?P<lyric>.+?)\s*$")
LRC_RE_LYRIC_TIMELABEL = reCompile(r"\[(?P<minutes>\d{1,2}):(?P<seconds>\d{1,2}(?:\.\d{1,3})?)\]") LRC_RE_LYRIC_TIMELABEL = reCompile(r"\[(?P<minutes>\d{1,2}):(?P<seconds>\d{1,2}(?:\.\d{1,3})?)\]")
@ -53,8 +53,8 @@ class Lrc:
if LRC_RE_COMMIT.match(lrcRow) is not None: if LRC_RE_COMMIT.match(lrcRow) is not None:
return return
# Skip NCM spical metadata lines # Skip NCM special metadata lines
if LRC_RE_META_NCM_SPICAL.match(lrcRow) is not None: if LRC_RE_META_NCM_SPECIAL.match(lrcRow) is not None:
return return
matchedMetaDataRow = LRC_RE_META.match(lrcRow) matchedMetaDataRow = LRC_RE_META.match(lrcRow)
@ -88,11 +88,11 @@ class Lrc:
self.metadata[metaType] = {lrcType: metaContent} self.metadata[metaType] = {lrcType: metaContent}
def appendMatchedLyricRow(self, lrcType: LrcType, matchedLine: Match[str]) -> None: def appendMatchedLyricRow(self, lrcType: LrcType, matchedLine: Match[str]) -> None:
timelabels, lyric = matchedLine.groups() timeLabels, lyric = matchedLine.groups()
timestamps: list[int] = [] timestamps: list[int] = []
for timelabel in LRC_RE_LYRIC_TIMELABEL.finditer(timelabels): for timeLabel in LRC_RE_LYRIC_TIMELABEL.finditer(timeLabels):
timestamps.append(self._timelabel2timestamp(timelabel)) timestamps.append(self._timeLabel2Timestamp(timeLabel))
if CONFIG_LRC_AUTO_MERGE: if CONFIG_LRC_AUTO_MERGE:
mergedTimestamps: list[int] = [] mergedTimestamps: list[int] = []
@ -120,11 +120,11 @@ class Lrc:
for type in LrcMetaType: for type in LrcMetaType:
if type in self.metadata: if type in self.metadata:
for lrcType in self.metadata[type].keys(): for lrcType in self.metadata[type].keys():
yield f"[{type.value}: {lrcType.preety()}/{self.metadata[type][lrcType]}]" yield f"[{type.value}: {lrcType.pretty()}/{self.metadata[type][lrcType]}]"
def generateLyricRows(self, timestamp: int) -> Generator[str, None, None]: def generateLyricRows(self, timestamp: int) -> Generator[str, None, None]:
for lrcType in self.lyrics[timestamp].keys(): for lrcType in self.lyrics[timestamp].keys():
yield self._timestamp2timelabel(timestamp) + self.lyrics[timestamp][lrcType] yield self._timestamp2TimeLabel(timestamp) + self.lyrics[timestamp][lrcType]
def saveAs(self, path: Path) -> None: def saveAs(self, path: Path) -> None:
with path.open("w+") as fs: with path.open("w+") as fs:
@ -132,11 +132,13 @@ class Lrc:
fs.write(row) fs.write(row)
fs.write("\n") fs.write("\n")
def _timelabel2timestamp(self, timelabel: Match[str]) -> int: @staticmethod
minutes, seconds = timelabel.groups() def _timeLabel2Timestamp(timeLabel: Match[str]) -> int:
minutes, seconds = timeLabel.groups()
return round((int(minutes) * 60 + float(seconds)) * 1000) return round((int(minutes) * 60 + float(seconds)) * 1000)
def _timestamp2timelabel(self, timestamp: int) -> str: @staticmethod
def _timestamp2TimeLabel(timestamp: int) -> str:
seconds = timestamp / 1000 seconds = timestamp / 1000
return f"[{seconds//60:02.0f}:{seconds%60:06.3f}]" return f"[{seconds//60:02.0f}:{seconds%60:06.3f}]"
@ -147,7 +149,7 @@ class Lrc:
timestampMax = timestamp + CONFIG_LRC_AUTO_MERGE_OFFSET timestampMax = timestamp + CONFIG_LRC_AUTO_MERGE_OFFSET
for existLyric in self.lyrics.keys(): for existLyric in self.lyrics.keys():
if timestampMin <= existLyric and existLyric <= timestampMax: if timestampMin <= existLyric <= timestampMax:
result = existLyric result = existLyric
break break

View File

@ -42,7 +42,7 @@ def parseLink(url: str) -> Link:
contentType = LinkType.Album contentType = LinkType.Album
contentId = int(matchedPath["id"]) contentId = int(matchedPath["id"])
else: else:
raise UnsupportLinkError(parsedUrl) raise UnsupportedLinkError(parsedUrl)
case "y.music.163.com": case "y.music.163.com":
match parsedUrl.path: match parsedUrl.path:
case "/m/playlist": case "/m/playlist":
@ -50,7 +50,7 @@ def parseLink(url: str) -> Link:
case "/m/song": case "/m/song":
contentType = LinkType.Song contentType = LinkType.Song
case _: case _:
raise UnsupportLinkError(parsedUrl) raise UnsupportedLinkError(parsedUrl)
case "163cn.tv": case "163cn.tv":
response = httpGet(url) response = httpGet(url)
if response.status_code != 302: if response.status_code != 302:
@ -60,7 +60,7 @@ def parseLink(url: str) -> Link:
raise ParseLinkError("Api 未返回重定向结果") raise ParseLinkError("Api 未返回重定向结果")
return parseLink(newUrl) return parseLink(newUrl)
case _: case _:
raise UnsupportLinkError(parsedUrl) raise UnsupportedLinkError(parsedUrl)
if contentId is None: if contentId is None:
try: try: