perf: rewrite testExistTrackSource()
using glob
* Also fix more source file name matching issues
This commit is contained in:
parent
1265958067
commit
34f16dbd0f
@ -11,6 +11,8 @@ from .api import NCMTrack
|
|||||||
from .error import ParseLinkError, UnsupportLinkError
|
from .error import ParseLinkError, UnsupportLinkError
|
||||||
|
|
||||||
RE_ANDROID_ALBUM_SHARE_LINK_PATH = reCompile(r"^/album/(?P<id>\d*)/?$")
|
RE_ANDROID_ALBUM_SHARE_LINK_PATH = reCompile(r"^/album/(?P<id>\d*)/?$")
|
||||||
|
RE_SAFE_FILENAME = reCompile(r"\*{2,}")
|
||||||
|
TRANSLATER_SAFE_FILENAME = str.maketrans({i: 0x2A for i in ("<", ">", ":", '"', "/", "\\", "|", "?")})
|
||||||
|
|
||||||
|
|
||||||
class LinkType(Enum):
|
class LinkType(Enum):
|
||||||
@ -76,27 +78,18 @@ def parseLink(url: str) -> Link:
|
|||||||
|
|
||||||
|
|
||||||
def testExistTrackSource(track: NCMTrack, path: Path) -> Path | None:
|
def testExistTrackSource(track: NCMTrack, path: Path) -> Path | None:
|
||||||
# 暂不考虑特殊字符的问题,目前于 PC 端已知的转换规则有:(":" => "", "\" => "\")
|
safeFileName = RE_SAFE_FILENAME.sub(
|
||||||
|
"*", f"{"?".join(track.artists[:3])} - {track.name.rstrip(".")}.*".translate(TRANSLATER_SAFE_FILENAME)
|
||||||
|
)
|
||||||
|
|
||||||
filenameList: list[str] = []
|
globing = path.glob(safeFileName, case_sensitive=False)
|
||||||
filenameListWithExtension: list[str] = []
|
|
||||||
|
|
||||||
if len(track.artists) == 1:
|
try:
|
||||||
filenameList.append(f"{track.artists[0]} - {track.name}")
|
return next(globing)
|
||||||
else:
|
except StopIteration:
|
||||||
for delimiter in (",", " "):
|
return None
|
||||||
filenameList.append(f"{delimiter.join(track.artists)} - {track.name}")
|
finally:
|
||||||
|
globing.close()
|
||||||
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:
|
def pickOutput(track: NCMTrack, outputs: list[Path], forceSourceExists: bool = False) -> Path | None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user