From 43cfe4e85b8b22a59ab3ae292b6a8c8fc5b56a56 Mon Sep 17 00:00:00 2001 From: holwell Date: Fri, 14 Feb 2025 16:17:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E6=AD=8C=E6=9B=B2=E6=B2=A1?= =?UTF-8?q?=E6=9C=89artist=E4=BF=A1=E6=81=AF=E7=AD=89=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E7=A8=B3=E5=81=A5=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++++++ main.py | 51 +++++++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..203732c --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +__pycache__/ +.history/ +neteasecloudmusicapi/ +cookies.txt +failed_ids.txt +*.json \ No newline at end of file diff --git a/main.py b/main.py index 2d4c7ed..07425eb 100644 --- a/main.py +++ b/main.py @@ -131,30 +131,37 @@ def process_songs(song_info_list, cookie): # 更新 song_info 添加 artist 和 album 信息 song_info['artist'] = song_artist song_info['album'] = song_album + try: + try_to_upload_song(song_info, cookie) + except Exception as e: + print(f'上传过程异常,跳过该歌曲:{e}') + save_failed_id(song_id) + +def try_to_upload_song(song_info, cookie): + song_id = song_info['id'] + attempts = 0 + while attempts < 3: + result = import_song(song_info, cookie) - attempts = 0 - while attempts < 3: - result = import_song(song_info, cookie) + if result: + success_songs = result.get('data', {}).get('successSongs', []) + failed = result.get('data', {}).get('failed', []) - if result: - success_songs = result.get('data', {}).get('successSongs', []) - failed = result.get('data', {}).get('failed', []) - - if success_songs: - print(f"歌曲 {song_id} 导入成功!") - break # 成功则跳出循环 - else: - print(f"歌曲 {song_id} 导入失败,失败原因:{failed}") - if all(f['code'] == -100 for f in failed): # 文件已存在的错误码 - print(f"歌曲 {song_id} 文件已存在,跳过") - save_failed_id(song_id) # 保存失败的 ID - break - time.sleep(5) # 请求失败后等待 5 秒重新请求 - attempts += 1 - - if attempts == 3: # 如果失败三次,则跳过此 ID - print(f"歌曲 {song_id} 失败三次,跳过该歌曲。") - save_failed_id(song_id) # 保存失败的 ID + if success_songs: + print(f"歌曲 {song_id} 导入成功!") + break # 成功则跳出循环 + else: + print(f"歌曲 {song_id} 导入失败,失败原因:{failed}") + if all(f['code'] == -100 for f in failed): # 文件已存在的错误码 + print(f"歌曲 {song_id} 文件已存在,跳过") + save_failed_id(song_id) # 保存失败的 ID + break + time.sleep(5) # 请求失败后等待 5 秒重新请求 + attempts += 1 + + if attempts == 3: # 如果失败三次,则跳过此 ID + print(f"歌曲 {song_id} 失败三次,跳过该歌曲。") + save_failed_id(song_id) # 保存失败的 ID # 主函数 def main():