From 097e5410036cb69f6eae8c75073c0ef14de00b48 Mon Sep 17 00:00:00 2001 From: Puqns67 Date: Thu, 26 Sep 2024 20:21:15 +0800 Subject: [PATCH] fix: cookie load and save errors, pure music track save error, lrc class creating error --- src/ncmlyrics/__main__.py | 9 ++++++++- src/ncmlyrics/api.py | 12 +++++++----- src/ncmlyrics/error.py | 14 +++++++++++--- src/ncmlyrics/lrc.py | 8 ++++++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/ncmlyrics/__main__.py b/src/ncmlyrics/__main__.py index 3894d38..1c83dd9 100644 --- a/src/ncmlyrics/__main__.py +++ b/src/ncmlyrics/__main__.py @@ -132,7 +132,14 @@ def main(outputs: list[Path], exist: bool, overwrite: bool, quiet: bool, links: confirm("继续操作?", default=True, abort=True) for track, path in app.tracks: - api.getLyricsByTrack(track.id).lrc().saveAs(path) + ncmlyrics = api.getLyricsByTrack(track.id) + if ncmlyrics.isPureMusic: + console.print(f"曲目 {track.name} 为纯音乐, 跳过此曲目") + else: + ncmlyrics.lrc().saveAs(path) + console.print(f"--> {str(path)}") + + api.saveCookies() if __name__ == "__main__": diff --git a/src/ncmlyrics/api.py b/src/ncmlyrics/api.py index b115d1e..00791a9 100644 --- a/src/ncmlyrics/api.py +++ b/src/ncmlyrics/api.py @@ -1,12 +1,12 @@ from dataclasses import dataclass -from http.cookiejar import MozillaCookieJar +from http.cookiejar import LoadError, MozillaCookieJar from json import dumps as dumpJson from typing import Any, Self from httpx import Client as HttpClient from .constant import CONFIG_API_DETAIL_TRACK_PER_REQUEST, NCM_API_BASE_URL, PLATFORM -from .error import NCMApiParseError, NCMApiRequestError +from .error import NCMApiParseError, NCMApiRequestError, UnsupportedPureMusicTrackError from .lrc import Lrc, LrcType REQUEST_HEADERS = { @@ -135,6 +135,9 @@ class NCMLyrics: return NCMLyrics(isPureMusic=False, data=data) def lrc(self) -> Lrc: + if self.isPureMusic: + raise UnsupportedPureMusicTrackError + result = Lrc() for lrcType in LrcType: @@ -158,7 +161,7 @@ class NCMApi: try: self._cookieJar.load(str(PLATFORM.user_config_path / "cookies.txt")) - except FileNotFoundError: + except FileNotFoundError | LoadError: pass self._httpClient = HttpClient( @@ -168,9 +171,8 @@ class NCMApi: http2=http2, ) - def __del__(self) -> None: + def saveCookies(self) -> None: self._cookieJar.save(str(PLATFORM.user_config_path / "cookies.txt")) - self._httpClient.close() def getDetailsForTrack(self, trackId: int) -> NCMTrack: params = {"c": f"[{{'id':{trackId}}}]"} diff --git a/src/ncmlyrics/error.py b/src/ncmlyrics/error.py index 486f0f1..4925bf5 100644 --- a/src/ncmlyrics/error.py +++ b/src/ncmlyrics/error.py @@ -1,7 +1,11 @@ from httpx import RequestError -class NCMApiError(Exception): +class NCMLyricsAppError(Exception): + """NCMLyrics 错误""" + + +class NCMApiError(NCMLyricsAppError): """使用网易云音乐 API 时出现错误""" @@ -13,9 +17,13 @@ class NCMApiParseError(NCMApiError): """解析网易云音乐 API 返回的数据时出现错误""" -class ParseLinkError(Exception): +class ParseLinkError(NCMLyricsAppError): """无法解析此分享链接""" -class UnsupportLinkError(Exception): +class UnsupportLinkError(NCMLyricsAppError): """不支持的分享链接""" + + +class UnsupportedPureMusicTrackError(NCMLyricsAppError): + """不支持纯音乐单曲""" diff --git a/src/ncmlyrics/lrc.py b/src/ncmlyrics/lrc.py index 03c9f61..ff635b3 100644 --- a/src/ncmlyrics/lrc.py +++ b/src/ncmlyrics/lrc.py @@ -38,8 +38,12 @@ class LrcMetaType(Enum): class Lrc: - metadata: dict[LrcMetaType, dict[LrcType, str]] = {} # metaType: lrcType: metaContent - lyrics: dict[int, dict[LrcType, str]] = {} # timestamp: lrcType: lrcContent + def __init__(self) -> None: + # metaType: lrcType: metaContent + self.metadata: dict[LrcMetaType, dict[LrcType, str]] = {} + + # timestamp: lrcType: lrcContent + self.lyrics: dict[int, dict[LrcType, str]] = {} def serializeLyricString(self, lrcType: LrcType, lrcStr: str) -> None: for line in lrcStr.splitlines():