From d0349a7a4d9cf21fbf0fd663282c66c5c3c8b0bf Mon Sep 17 00:00:00 2001 From: Puqns67 Date: Wed, 4 Dec 2024 23:13:53 +0800 Subject: [PATCH] refactor: use prefix to speed up resolvePath, and let regular expression more reasonable --- ncmlyrics/app.py | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/ncmlyrics/app.py b/ncmlyrics/app.py index 4ae430e..83bedb4 100644 --- a/ncmlyrics/app.py +++ b/ncmlyrics/app.py @@ -57,7 +57,8 @@ class NCMLyricsApp: self.tracks: list[NCMTrack] = [] 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: if len(self.links) == 0: @@ -130,7 +131,12 @@ class NCMLyricsApp: if not content.is_file(): continue 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: try: @@ -157,35 +163,44 @@ class NCMLyricsApp: def resolvePath(self, progress: TaskID, track: NCMTrack) -> tuple[NCMTrack, Path | 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: - # "(?:," + ")?(?:,".join((escapeRegex(artist) for artist in track.artists[3:])) + ")?" - regex = compileRegex( - rf"^{escapeRegex(",".join(track.artists[:3])).replace(",", "(?:,| )")} - {escapeRegex(track.name.rstrip("."))}.+(ncm|mp3|flac)$" - ) - matched = regex.match(target.name) + escapedArtists = "(,| )".join((escapeRegex(artist) for artist in track.artists[:3])) + if len(track.artists) > 3: + escapedArtists += ( + rf"((,| ){")?((,| )".join((escapeRegex(artist) for artist in track.artists[3:]))})?" + ) + regex = compileRegex(rf"^{escapedArtists} - {escapeRegex(track.name.rstrip("."))}\.+(ncm|mp3|flac)$") + matched = regex.match(existingFile.name) if matched is not None: - targetLrc = target.with_suffix(".lrc") + targetPath = existingFile.with_suffix(".lrc") break self.progress.advance(progress) - if targetLrc is None: + if targetPath is None: if self.exist: return (track, None) 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, None) - return (track, targetLrc) + return (track, targetPath) def exportLrc(self, progress: TaskID, track: NCMTrack, path: Path | None) -> None: if path is None: 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) return