Skip to content

Commit

Permalink
Fixed the problem when searching lyrics from Kugou Music
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmozhijin committed Mar 9, 2024
1 parent 9ab5b46 commit dc80bda
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion utils/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def download_normal_lyrics(self) -> tuple[str | None, LyricProcessingError | Non
return "没有获取到可用的歌词(orig=None and ts=None)", LyricProcessingError.NOT_FOUND
return None, None

def merge(self, lyrics_order: list) -> str:
def get_merge_lrc(self, lyrics_order: list) -> str:
"""
合并歌词
:param lyrics_order:歌词顺序,同时决定需要合并的类型
Expand Down
17 changes: 7 additions & 10 deletions utils/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ def run(self) -> None:

if self.source == Source.KG and isinstance(self.keyword, dict) and self.search_type == SearchType.LYRICS:
# 为歌词搜索结果添加歌曲信息(歌词保存需要)
result = []
for i, item in enumerate(search_return):
result.append(self.keyword)
result[i].update(item)
result = [dict(self.keyword, **item) for item in search_return]
else:
result = search_return
self.signals.result.emit(self.taskid, self.search_type.value, result)
Expand Down Expand Up @@ -198,9 +195,9 @@ def stop(self) -> None:
def get_lyrics(self, song_info: dict) -> tuple[None | Lyrics, bool]:
logging.debug(f"开始获取歌词:{song_info['id']}")
from_cache = False
if ("lyrics", song_info["source"], song_info['id']) in cache:
lyrics = cache[("lyrics", song_info["source"], song_info['id'])]
logging.info(f"从缓存中获取歌词:源:{song_info['source']}, id:{song_info['id']}")
if ("lyrics", song_info["source"], song_info['id'], song_info.get("accesskey", "")) in cache:
lyrics = cache[("lyrics", song_info["source"], song_info['id'], song_info.get("accesskey", ""))]
logging.info(f"从缓存中获取歌词:源:{song_info['source']}, id:{song_info['id']}, accesskey: {song_info.get('accesskey', '无')}")
from_cache = True
if not from_cache:
lyrics = Lyrics(song_info)
Expand Down Expand Up @@ -233,7 +230,7 @@ def get_lyrics(self, song_info: dict) -> tuple[None | Lyrics, bool]:
return None, False

if error1_type != LyricProcessingError.REQUEST and not from_cache: # 如果不是请求错误则缓存
cache[("lyrics", song_info["source"], song_info['id'])] = lyrics
cache[("lyrics", song_info["source"], song_info['id'], song_info.get("accesskey", ""))] = lyrics
return lyrics, from_cache

def get_merged_lyric(self, song_info: dict, lyric_type: list) -> bool:
Expand All @@ -247,7 +244,7 @@ def get_merged_lyric(self, song_info: dict, lyric_type: list) -> bool:
self.data_mutex.unlock()

try:
merged_lyric = lyrics.merge(lyrics_order)
merged_lyric = lyrics.get_merge_lrc(lyrics_order)
except Exception as e:
logging.exception("合并歌词失败")
self.signals.error.emit(f"合并歌词失败:{e}")
Expand Down Expand Up @@ -541,7 +538,7 @@ def search_and_get(self, info: dict) -> list:

# Step 4 合并歌词
if isinstance(lyrics, Lyrics):
merged_lyric = lyrics.merge(self.lyrics_order)
merged_lyric = lyrics.get_merge_lrc(self.lyrics_order)
return merged_lyric, song_info
if 'artist' in info:
if inst:
Expand Down
6 changes: 3 additions & 3 deletions view/encrypted_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def convert(self) -> None:
self.lyrics = Lyrics({"source": Source.QM})
self.lyrics.tags, lyric = qrc2list(lyrics)
self.lyrics["orig"] = lyric
lrc = self.lyrics.merge(["orig"])
lrc = self.lyrics.get_merge_lrc(["orig"])
elif self.lyrics_type == "krc":
self.data_mutex.lock()
type_mapping = {"原文": "orig", "译文": "ts", "罗马音": "roma"}
Expand All @@ -95,7 +95,7 @@ def convert(self) -> None:

self.lyrics.tags, lyric = krc2dict(lyrics)
self.lyrics.update(lyric)
lrc = self.lyrics.merge(lyrics_order)
lrc = self.lyrics.get_merge_lrc(lyrics_order)
except Exception as e:
logging.exception("转换失败")
QMessageBox.critical(self, "错误", f"转换失败:{e}")
Expand All @@ -109,7 +109,7 @@ def change_lyrics_type(self) -> None:
self.data_mutex.lock()
lyrics_order = [type_mapping[type_] for type_ in self.data.cfg["lyrics_order"] if type_mapping[type_] in self.get_lyric_type()]
self.data_mutex.unlock()
self.plainTextEdit.setPlainText(self.lyrics.merge(lyrics_order))
self.plainTextEdit.setPlainText(self.lyrics.get_merge_lrc(lyrics_order))

def save(self) -> None:
file_path, _ = QFileDialog.getSaveFileName(self, "保存文件", "", "LRC文件 (*.lrc)")
Expand Down

0 comments on commit dc80bda

Please sign in to comment.