fix: partially fix source file name matching issues

This commit is contained in:
Puqns67 2024-09-25 01:08:58 +08:00
parent 4319c8b6d7
commit 1265958067
Signed by: Puqns67
GPG Key ID: 9669DF042554F536
7 changed files with 36 additions and 18 deletions

View File

@ -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

View File

@ -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"

View File

@ -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):