refactor: use prefix to speed up resolvePath, and let regular expression more reasonable
This commit is contained in:
parent
d16d14e20d
commit
d0349a7a4d
@ -57,7 +57,8 @@ class NCMLyricsApp:
|
|||||||
self.tracks: list[NCMTrack] = []
|
self.tracks: list[NCMTrack] = []
|
||||||
self.trackPairs: list[tuple[NCMTrack, Path | None]] = []
|
self.trackPairs: list[tuple[NCMTrack, Path | None]] = []
|
||||||
|
|
||||||
self.existedTargets: list[Path] = []
|
self.existingFiles: list[Path] = []
|
||||||
|
self.existingFilesByPrefix: dict[str, list[Path]] = {}
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
if len(self.links) == 0:
|
if len(self.links) == 0:
|
||||||
@ -130,7 +131,12 @@ class NCMLyricsApp:
|
|||||||
if not content.is_file():
|
if not content.is_file():
|
||||||
continue
|
continue
|
||||||
if content.suffix in (".ncm", ".mp3", ".flac"):
|
if content.suffix in (".ncm", ".mp3", ".flac"):
|
||||||
self.existedTargets.append(content)
|
self.existingFiles.append(content)
|
||||||
|
prefix = content.name[0]
|
||||||
|
if prefix in self.existingFilesByPrefix:
|
||||||
|
self.existingFilesByPrefix[prefix].append(content)
|
||||||
|
else:
|
||||||
|
self.existingFilesByPrefix[prefix] = [content]
|
||||||
|
|
||||||
def resolveLink(self, progress: TaskID, link: str) -> NCMTrack | NCMAlbum | NCMPlaylist:
|
def resolveLink(self, progress: TaskID, link: str) -> NCMTrack | NCMAlbum | NCMPlaylist:
|
||||||
try:
|
try:
|
||||||
@ -157,35 +163,44 @@ class NCMLyricsApp:
|
|||||||
|
|
||||||
def resolvePath(self, progress: TaskID, track: NCMTrack) -> tuple[NCMTrack, Path | None]:
|
def resolvePath(self, progress: TaskID, track: NCMTrack) -> tuple[NCMTrack, Path | None]:
|
||||||
regex: Pattern[str] | None = None
|
regex: Pattern[str] | None = None
|
||||||
targetLrc: Path | None = None
|
targetPath: Path | None = None
|
||||||
|
|
||||||
for target in self.existedTargets:
|
# If not in prefix then search all existing files
|
||||||
|
existingFiles = self.existingFilesByPrefix.get(track.artists[0][0], self.existingFiles)
|
||||||
|
|
||||||
|
for existingFile in existingFiles:
|
||||||
if regex is None:
|
if regex is None:
|
||||||
# "(?:," + ")?(?:,".join((escapeRegex(artist) for artist in track.artists[3:])) + ")?"
|
escapedArtists = "(,| )".join((escapeRegex(artist) for artist in track.artists[:3]))
|
||||||
regex = compileRegex(
|
if len(track.artists) > 3:
|
||||||
rf"^{escapeRegex(",".join(track.artists[:3])).replace(",", "(?:,| )")} - {escapeRegex(track.name.rstrip("."))}.+(ncm|mp3|flac)$"
|
escapedArtists += (
|
||||||
)
|
rf"((,| ){")?((,| )".join((escapeRegex(artist) for artist in track.artists[3:]))})?"
|
||||||
matched = regex.match(target.name)
|
)
|
||||||
|
regex = compileRegex(rf"^{escapedArtists} - {escapeRegex(track.name.rstrip("."))}\.+(ncm|mp3|flac)$")
|
||||||
|
matched = regex.match(existingFile.name)
|
||||||
if matched is not None:
|
if matched is not None:
|
||||||
targetLrc = target.with_suffix(".lrc")
|
targetPath = existingFile.with_suffix(".lrc")
|
||||||
break
|
break
|
||||||
|
|
||||||
self.progress.advance(progress)
|
self.progress.advance(progress)
|
||||||
|
|
||||||
if targetLrc is None:
|
if targetPath is None:
|
||||||
if self.exist:
|
if self.exist:
|
||||||
return (track, None)
|
return (track, None)
|
||||||
else:
|
else:
|
||||||
targetLrc = self.outputs[-1] / safeFileName(f"{",".join(track.artists)} - {track.name}.lrc")
|
targetPath = self.outputs[-1] / safeFileName(f"{",".join(track.artists)} - {track.name}.lrc")
|
||||||
|
|
||||||
if not self.overwrite and targetLrc.exists():
|
return (track, targetPath)
|
||||||
return (track, None)
|
|
||||||
return (track, targetLrc)
|
|
||||||
|
|
||||||
def exportLrc(self, progress: TaskID, track: NCMTrack, path: Path | None) -> None:
|
def exportLrc(self, progress: TaskID, track: NCMTrack, path: Path | None) -> None:
|
||||||
if path is None:
|
if path is None:
|
||||||
self.console.print(
|
self.console.print(
|
||||||
f"[trackarrow]-->[/trackarrow] {track.prettyString()} [dark_turquoise]==>[dark_turquoise] [warning]对应的歌词文件已存在, 跳过此曲目。[/warning]"
|
f"[trackarrow]-->[/trackarrow] {track.prettyString()} [dark_turquoise]==>[dark_turquoise] [warning]找不到对应的源文件, 跳过此曲目。[/warning]"
|
||||||
|
)
|
||||||
|
self.progress.advance(progress)
|
||||||
|
return
|
||||||
|
elif not self.overwrite and path.exists():
|
||||||
|
self.console.print(
|
||||||
|
f"[trackarrow]-->[/trackarrow] {track.prettyString()} [dark_turquoise]==>[/dark_turquoise] [warning]对应的歌词文件已存在, 跳过此曲目。[/warning]"
|
||||||
)
|
)
|
||||||
self.progress.advance(progress)
|
self.progress.advance(progress)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user