From 2cdda43cea0c5a4a46c719a97ced5491b3d7e904 Mon Sep 17 00:00:00 2001 From: Puqns67 Date: Tue, 15 Oct 2024 20:30:38 +0800 Subject: [PATCH] chore: remove dead code, fix typos --- src/ncmlyrics/__main__.py | 6 ++---- src/ncmlyrics/api.py | 1 - src/ncmlyrics/enum.py | 2 +- src/ncmlyrics/error.py | 2 +- src/ncmlyrics/lrc.py | 28 +++++++++++++++------------- src/ncmlyrics/util.py | 6 +++--- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/ncmlyrics/__main__.py b/src/ncmlyrics/__main__.py index fed7811..5ea74f2 100644 --- a/src/ncmlyrics/__main__.py +++ b/src/ncmlyrics/__main__.py @@ -11,7 +11,7 @@ from .enum import LinkType from .error import UnsupportedLinkError from .lrc import Lrc from .object import NCMTrack -from .util import Link, parseLink, pickOutput +from .util import parseLink, pickOutput 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=[]) for link in links: - parsed: Link | None = None - try: parsed = parseLink(link) - except UnsupportLinkError: + except UnsupportedLinkError: continue match parsed.type: diff --git a/src/ncmlyrics/api.py b/src/ncmlyrics/api.py index 17b50d3..5dda5b7 100644 --- a/src/ncmlyrics/api.py +++ b/src/ncmlyrics/api.py @@ -114,4 +114,3 @@ class NCMApi: request = self._httpClient.build_request("GET", "/song/lyric/v1", params=params) return NCMLyrics.fromApi(self._fetch(request)).withId(trackId) - return NCMLyrics.fromApi(response.json()) diff --git a/src/ncmlyrics/enum.py b/src/ncmlyrics/enum.py index 6966d9e..dec0b44 100644 --- a/src/ncmlyrics/enum.py +++ b/src/ncmlyrics/enum.py @@ -8,7 +8,7 @@ class LrcType(Enum): Translation = auto() Romaji = auto() - def preety(self) -> str: + def pretty(self) -> str: match self: case LrcType.Origin: return "源" diff --git a/src/ncmlyrics/error.py b/src/ncmlyrics/error.py index eeadeaa..1d7788c 100644 --- a/src/ncmlyrics/error.py +++ b/src/ncmlyrics/error.py @@ -25,7 +25,7 @@ class ParseLinkError(NCMLyricsAppError): """无法解析此分享链接""" -class UnsupportLinkError(NCMLyricsAppError): +class UnsupportedLinkError(NCMLyricsAppError): """不支持的分享链接""" diff --git a/src/ncmlyrics/lrc.py b/src/ncmlyrics/lrc.py index 24be0c7..bda9b29 100644 --- a/src/ncmlyrics/lrc.py +++ b/src/ncmlyrics/lrc.py @@ -14,8 +14,8 @@ __all__ = ["LrcType", "LrcMetaType", "Lrc"] LRC_RE_COMMIT = reCompile(r"^\s*#") LRC_RE_META = reCompile(r"^\s*\[(?Pti|ar|al|au|length|by|offset):\s*(?P.+?)\s*\]\s*$") -LRC_RE_META_NCM_SPICAL = reCompile(r"^\s*\{.*\}\s*$") -LRC_RE_LYRIC = reCompile(r"^\s*(?P(?:\s*\[\d{1,2}:\d{1,2}(?:\.\d{1,3})?\])+)\s*(?P.+?)\s*$") +LRC_RE_META_NCM_SPECIAL = reCompile(r"^\s*\{.*\}\s*$") +LRC_RE_LYRIC = reCompile(r"^\s*(?P(?:\s*\[\d{1,2}:\d{1,2}(?:\.\d{1,3})?\])+)\s*(?P.+?)\s*$") LRC_RE_LYRIC_TIMELABEL = reCompile(r"\[(?P\d{1,2}):(?P\d{1,2}(?:\.\d{1,3})?)\]") @@ -53,8 +53,8 @@ class Lrc: if LRC_RE_COMMIT.match(lrcRow) is not None: return - # Skip NCM spical metadata lines - if LRC_RE_META_NCM_SPICAL.match(lrcRow) is not None: + # Skip NCM special metadata lines + if LRC_RE_META_NCM_SPECIAL.match(lrcRow) is not None: return matchedMetaDataRow = LRC_RE_META.match(lrcRow) @@ -88,11 +88,11 @@ class Lrc: self.metadata[metaType] = {lrcType: metaContent} def appendMatchedLyricRow(self, lrcType: LrcType, matchedLine: Match[str]) -> None: - timelabels, lyric = matchedLine.groups() + timeLabels, lyric = matchedLine.groups() timestamps: list[int] = [] - for timelabel in LRC_RE_LYRIC_TIMELABEL.finditer(timelabels): - timestamps.append(self._timelabel2timestamp(timelabel)) + for timeLabel in LRC_RE_LYRIC_TIMELABEL.finditer(timeLabels): + timestamps.append(self._timeLabel2Timestamp(timeLabel)) if CONFIG_LRC_AUTO_MERGE: mergedTimestamps: list[int] = [] @@ -120,11 +120,11 @@ class Lrc: for type in LrcMetaType: if type in self.metadata: 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]: 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: with path.open("w+") as fs: @@ -132,11 +132,13 @@ class Lrc: fs.write(row) fs.write("\n") - def _timelabel2timestamp(self, timelabel: Match[str]) -> int: - minutes, seconds = timelabel.groups() + @staticmethod + def _timeLabel2Timestamp(timeLabel: Match[str]) -> int: + minutes, seconds = timeLabel.groups() return round((int(minutes) * 60 + float(seconds)) * 1000) - def _timestamp2timelabel(self, timestamp: int) -> str: + @staticmethod + def _timestamp2TimeLabel(timestamp: int) -> str: seconds = timestamp / 1000 return f"[{seconds//60:02.0f}:{seconds%60:06.3f}]" @@ -147,7 +149,7 @@ class Lrc: timestampMax = timestamp + CONFIG_LRC_AUTO_MERGE_OFFSET for existLyric in self.lyrics.keys(): - if timestampMin <= existLyric and existLyric <= timestampMax: + if timestampMin <= existLyric <= timestampMax: result = existLyric break diff --git a/src/ncmlyrics/util.py b/src/ncmlyrics/util.py index 08a651a..240a79b 100644 --- a/src/ncmlyrics/util.py +++ b/src/ncmlyrics/util.py @@ -42,7 +42,7 @@ def parseLink(url: str) -> Link: contentType = LinkType.Album contentId = int(matchedPath["id"]) else: - raise UnsupportLinkError(parsedUrl) + raise UnsupportedLinkError(parsedUrl) case "y.music.163.com": match parsedUrl.path: case "/m/playlist": @@ -50,7 +50,7 @@ def parseLink(url: str) -> Link: case "/m/song": contentType = LinkType.Song case _: - raise UnsupportLinkError(parsedUrl) + raise UnsupportedLinkError(parsedUrl) case "163cn.tv": response = httpGet(url) if response.status_code != 302: @@ -60,7 +60,7 @@ def parseLink(url: str) -> Link: raise ParseLinkError("Api 未返回重定向结果") return parseLink(newUrl) case _: - raise UnsupportLinkError(parsedUrl) + raise UnsupportedLinkError(parsedUrl) if contentId is None: try: