From 126595806785a507eeca1d09ca3bd0297d15b0cb Mon Sep 17 00:00:00 2001 From: Puqns67 Date: Wed, 25 Sep 2024 01:08:58 +0800 Subject: [PATCH] fix: partially fix source file name matching issues --- src/ncmlyrics/__main__.py | 4 ++- src/ncmlyrics/util.py | 34 ++++++++++++++----- .../FlacArtist1 FlacArtist2 - FlacName.flac | 0 .../Mp3Artist - Mp3Name.mp3 | 0 .../Mp3Artist1,Mp3Artist2 - Mp3Name.mp3 | 0 .../NcmArtistWith. - NcmNameWith..ncm} | 0 tests/test_utils.py | 16 ++++----- 7 files changed, 36 insertions(+), 18 deletions(-) rename tests/resource/util/{testTrackSourceExists => testExistTrackSource}/FlacArtist1 FlacArtist2 - FlacName.flac (100%) rename tests/resource/util/{testTrackSourceExists => testExistTrackSource}/Mp3Artist - Mp3Name.mp3 (100%) rename tests/resource/util/{testTrackSourceExists => testExistTrackSource}/Mp3Artist1,Mp3Artist2 - Mp3Name.mp3 (100%) rename tests/resource/util/{testTrackSourceExists/NcmArtist1,NcmArtist2 - NcmName.flac => testExistTrackSource/NcmArtistWith. - NcmNameWith..ncm} (100%) diff --git a/src/ncmlyrics/__main__.py b/src/ncmlyrics/__main__.py index 6eb72f5..3894d38 100644 --- a/src/ncmlyrics/__main__.py +++ b/src/ncmlyrics/__main__.py @@ -1,6 +1,8 @@ from dataclasses import dataclass from pathlib import Path -from click import argument, command, confirm, option, Path as clickPath + +from click import Path as clickPath +from click import argument, command, confirm, option from rich.console import Console from rich.theme import Theme diff --git a/src/ncmlyrics/util.py b/src/ncmlyrics/util.py index 5346e43..47ffb38 100644 --- a/src/ncmlyrics/util.py +++ b/src/ncmlyrics/util.py @@ -4,6 +4,7 @@ from pathlib import Path from re import compile as reCompile from urllib.parse import parse_qs as parseQuery from urllib.parse import urlparse as parseUrl + from httpx import get as httpGet from .api import NCMTrack @@ -74,30 +75,45 @@ def parseLink(url: str) -> Link: return Link(contentType, contentId) -def testTrackSourceExists(track: NCMTrack, path: Path) -> Path | None: - for name in (f"{",".join(track.artists)} - {track.name}", f"{" ".join(track.artists)} - {track.name}"): - for suffix in (".mp3", ".flac", ".ncm"): - result = path.joinpath(name).with_suffix(suffix) - if result.exists(): - return result +def testExistTrackSource(track: NCMTrack, path: Path) -> Path | None: + # 暂不考虑特殊字符的问题,目前于 PC 端已知的转换规则有:(":" => "", "\" => "\") + + filenameList: list[str] = [] + filenameListWithExtension: list[str] = [] + + if len(track.artists) == 1: + filenameList.append(f"{track.artists[0]} - {track.name}") + else: + for delimiter in (",", " "): + filenameList.append(f"{delimiter.join(track.artists)} - {track.name}") + + for filename in filenameList: + for extension in (".mp3", ".ncm", ".flac"): + filenameListWithExtension.append(filename + extension) + + for filename in filenameListWithExtension: + result = path / filename + if result.exists(): + return result + return None def pickOutput(track: NCMTrack, outputs: list[Path], forceSourceExists: bool = False) -> Path | None: match len(outputs): case 0: - result = testTrackSourceExists(track, Path()) + result = testExistTrackSource(track, Path()) if result is not None: return result.with_suffix(".lrc") return None if forceSourceExists else Path(f"{",".join(track.artists)} - {track.name}.lrc") case 1: - result = testTrackSourceExists(track, outputs[0]) + result = testExistTrackSource(track, outputs[0]) if result is not None: return result.with_suffix(".lrc") return None if forceSourceExists else outputs[0] / f"{",".join(track.artists)} - {track.name}.lrc" case _: for output in outputs: - result = testTrackSourceExists(track, output) + result = testExistTrackSource(track, output) if result is not None: return result.with_suffix(".lrc") return None if forceSourceExists else outputs[-1] / f"{",".join(track.artists)} - {track.name}.lrc" diff --git a/tests/resource/util/testTrackSourceExists/FlacArtist1 FlacArtist2 - FlacName.flac b/tests/resource/util/testExistTrackSource/FlacArtist1 FlacArtist2 - FlacName.flac similarity index 100% rename from tests/resource/util/testTrackSourceExists/FlacArtist1 FlacArtist2 - FlacName.flac rename to tests/resource/util/testExistTrackSource/FlacArtist1 FlacArtist2 - FlacName.flac diff --git a/tests/resource/util/testTrackSourceExists/Mp3Artist - Mp3Name.mp3 b/tests/resource/util/testExistTrackSource/Mp3Artist - Mp3Name.mp3 similarity index 100% rename from tests/resource/util/testTrackSourceExists/Mp3Artist - Mp3Name.mp3 rename to tests/resource/util/testExistTrackSource/Mp3Artist - Mp3Name.mp3 diff --git a/tests/resource/util/testTrackSourceExists/Mp3Artist1,Mp3Artist2 - Mp3Name.mp3 b/tests/resource/util/testExistTrackSource/Mp3Artist1,Mp3Artist2 - Mp3Name.mp3 similarity index 100% rename from tests/resource/util/testTrackSourceExists/Mp3Artist1,Mp3Artist2 - Mp3Name.mp3 rename to tests/resource/util/testExistTrackSource/Mp3Artist1,Mp3Artist2 - Mp3Name.mp3 diff --git a/tests/resource/util/testTrackSourceExists/NcmArtist1,NcmArtist2 - NcmName.flac b/tests/resource/util/testExistTrackSource/NcmArtistWith. - NcmNameWith..ncm similarity index 100% rename from tests/resource/util/testTrackSourceExists/NcmArtist1,NcmArtist2 - NcmName.flac rename to tests/resource/util/testExistTrackSource/NcmArtistWith. - NcmNameWith..ncm diff --git a/tests/test_utils.py b/tests/test_utils.py index 1449bfb..2ca8111 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -3,7 +3,7 @@ from unittest import TestCase from ncmlyrics.api import NCMTrack from ncmlyrics.error import ParseLinkError, UnsupportLinkError -from ncmlyrics.util import Link, LinkType, parseLink, testTrackSourceExists, pickOutput +from ncmlyrics.util import Link, LinkType, parseLink, pickOutput, testExistTrackSource class TestUtils(TestCase): @@ -97,27 +97,27 @@ class TestUtils(TestCase): "https://music.163.com/playlist?id=123a", ) - def test_testTrackSourceExists(self): - resources = Path("tests/resource/util/testTrackSourceExists") + def test_testExistTrackSource(self): + resources = Path("tests/resource/util/testExistTrackSource") self.assertEqual( - testTrackSourceExists(NCMTrack(0, "Mp3Name", ["Mp3Artist"]), resources), + testExistTrackSource(NCMTrack(0, "Mp3Name", ["Mp3Artist"]), resources), resources / "Mp3Artist - Mp3Name.mp3", ) self.assertEqual( - testTrackSourceExists(NCMTrack(0, "Mp3Name", ["Mp3Artist1", "Mp3Artist2"]), resources), + testExistTrackSource(NCMTrack(0, "Mp3Name", ["Mp3Artist1", "Mp3Artist2"]), resources), resources / "Mp3Artist1,Mp3Artist2 - Mp3Name.mp3", ) self.assertEqual( - testTrackSourceExists(NCMTrack(0, "FlacName", ["FlacArtist1", "FlacArtist2"]), resources), + testExistTrackSource(NCMTrack(0, "FlacName", ["FlacArtist1", "FlacArtist2"]), resources), resources / "FlacArtist1 FlacArtist2 - FlacName.flac", ) self.assertEqual( - testTrackSourceExists(NCMTrack(0, "NcmName", ["NcmArtist1", "NcmArtist2"]), resources), - resources / "NcmArtist1,NcmArtist2 - NcmName.flac", + testExistTrackSource(NCMTrack(0, "NcmNameWith.", ["NcmArtistWith."]), resources), + resources / "NcmArtistWith. - NcmNameWith..ncm", ) def test_pickOutput(self):