chore: remove dead code, fix typos
This commit is contained in:
parent
3c8a46d039
commit
2cdda43cea
@ -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:
|
||||
|
@ -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())
|
||||
|
@ -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 "源"
|
||||
|
@ -25,7 +25,7 @@ class ParseLinkError(NCMLyricsAppError):
|
||||
"""无法解析此分享链接"""
|
||||
|
||||
|
||||
class UnsupportLinkError(NCMLyricsAppError):
|
||||
class UnsupportedLinkError(NCMLyricsAppError):
|
||||
"""不支持的分享链接"""
|
||||
|
||||
|
||||
|
@ -14,8 +14,8 @@ __all__ = ["LrcType", "LrcMetaType", "Lrc"]
|
||||
|
||||
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_NCM_SPICAL = 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_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_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:
|
||||
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
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user