fix: cookie load and save errors, pure music track save error, lrc class creating error

This commit is contained in:
Puqns67 2024-09-26 20:21:15 +08:00
parent 34f16dbd0f
commit 097e541003
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
4 changed files with 32 additions and 11 deletions

View File

@ -132,7 +132,14 @@ def main(outputs: list[Path], exist: bool, overwrite: bool, quiet: bool, links:
confirm("继续操作?", default=True, abort=True) confirm("继续操作?", default=True, abort=True)
for track, path in app.tracks: 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__": if __name__ == "__main__":

View File

@ -1,12 +1,12 @@
from dataclasses import dataclass from dataclasses import dataclass
from http.cookiejar import MozillaCookieJar from http.cookiejar import LoadError, MozillaCookieJar
from json import dumps as dumpJson from json import dumps as dumpJson
from typing import Any, Self from typing import Any, Self
from httpx import Client as HttpClient from httpx import Client as HttpClient
from .constant import CONFIG_API_DETAIL_TRACK_PER_REQUEST, NCM_API_BASE_URL, PLATFORM 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 from .lrc import Lrc, LrcType
REQUEST_HEADERS = { REQUEST_HEADERS = {
@ -135,6 +135,9 @@ class NCMLyrics:
return NCMLyrics(isPureMusic=False, data=data) return NCMLyrics(isPureMusic=False, data=data)
def lrc(self) -> Lrc: def lrc(self) -> Lrc:
if self.isPureMusic:
raise UnsupportedPureMusicTrackError
result = Lrc() result = Lrc()
for lrcType in LrcType: for lrcType in LrcType:
@ -158,7 +161,7 @@ class NCMApi:
try: try:
self._cookieJar.load(str(PLATFORM.user_config_path / "cookies.txt")) self._cookieJar.load(str(PLATFORM.user_config_path / "cookies.txt"))
except FileNotFoundError: except FileNotFoundError | LoadError:
pass pass
self._httpClient = HttpClient( self._httpClient = HttpClient(
@ -168,9 +171,8 @@ class NCMApi:
http2=http2, http2=http2,
) )
def __del__(self) -> None: def saveCookies(self) -> None:
self._cookieJar.save(str(PLATFORM.user_config_path / "cookies.txt")) self._cookieJar.save(str(PLATFORM.user_config_path / "cookies.txt"))
self._httpClient.close()
def getDetailsForTrack(self, trackId: int) -> NCMTrack: def getDetailsForTrack(self, trackId: int) -> NCMTrack:
params = {"c": f"[{{'id':{trackId}}}]"} params = {"c": f"[{{'id':{trackId}}}]"}

View File

@ -1,7 +1,11 @@
from httpx import RequestError from httpx import RequestError
class NCMApiError(Exception): class NCMLyricsAppError(Exception):
"""NCMLyrics 错误"""
class NCMApiError(NCMLyricsAppError):
"""使用网易云音乐 API 时出现错误""" """使用网易云音乐 API 时出现错误"""
@ -13,9 +17,13 @@ class NCMApiParseError(NCMApiError):
"""解析网易云音乐 API 返回的数据时出现错误""" """解析网易云音乐 API 返回的数据时出现错误"""
class ParseLinkError(Exception): class ParseLinkError(NCMLyricsAppError):
"""无法解析此分享链接""" """无法解析此分享链接"""
class UnsupportLinkError(Exception): class UnsupportLinkError(NCMLyricsAppError):
"""不支持的分享链接""" """不支持的分享链接"""
class UnsupportedPureMusicTrackError(NCMLyricsAppError):
"""不支持纯音乐单曲"""

View File

@ -38,8 +38,12 @@ class LrcMetaType(Enum):
class Lrc: class Lrc:
metadata: dict[LrcMetaType, dict[LrcType, str]] = {} # metaType: lrcType: metaContent def __init__(self) -> None:
lyrics: dict[int, dict[LrcType, str]] = {} # timestamp: lrcType: lrcContent # 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: def serializeLyricString(self, lrcType: LrcType, lrcStr: str) -> None:
for line in lrcStr.splitlines(): for line in lrcStr.splitlines():