mirror of
https://github.com/BsaLee/up163cloud.git
synced 2026-01-17 03:10:42 +08:00
Merge f33bf31420 into 5a7bcc49f2
This commit is contained in:
commit
95c232d72e
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
__pycache__/
|
||||||
|
.history/
|
||||||
|
neteasecloudmusicapi/
|
||||||
|
cookies.txt
|
||||||
|
failed_ids.txt
|
||||||
|
*.json
|
||||||
93
main.py
93
main.py
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
from typing import Union
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -66,6 +67,21 @@ def get_song_details(song_ids):
|
|||||||
print("响应内容无法解析为JSON:", response.text)
|
print("响应内容无法解析为JSON:", response.text)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# 判断歌曲是否已上传云盘
|
||||||
|
def has_uploaded(song_id, cookie):
|
||||||
|
url = f"http://localhost:3000/user/cloud/detail?id={song_id}&cookie={cookie}"
|
||||||
|
response = requests.get(url)
|
||||||
|
try:
|
||||||
|
response_data = response.json()
|
||||||
|
if response_data.get('code') == 200 and len(response_data['data']) != 0:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("获取云盘歌曲详情失败:", response_data.get("message"))
|
||||||
|
return False
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("获取云盘歌曲信息失败,响应内容无法解析为JSON:", response.text)
|
||||||
|
return False
|
||||||
|
|
||||||
# 执行 import 请求
|
# 执行 import 请求
|
||||||
def import_song(song_info, cookie):
|
def import_song(song_info, cookie):
|
||||||
song_id = song_info['id']
|
song_id = song_info['id']
|
||||||
@ -93,6 +109,14 @@ def import_song(song_info, cookie):
|
|||||||
def save_failed_id(song_id):
|
def save_failed_id(song_id):
|
||||||
with open("failed_ids.txt", "a") as f:
|
with open("failed_ids.txt", "a") as f:
|
||||||
f.write(f"{song_id}\n")
|
f.write(f"{song_id}\n")
|
||||||
|
|
||||||
|
# 获取最后一个上传的异常 id
|
||||||
|
def get_last_failed_id() -> Union[int, None]:
|
||||||
|
if not os.path.exists("failed_ids.txt"):
|
||||||
|
return None
|
||||||
|
with open("failed_ids.txt", "r") as f:
|
||||||
|
ids = [line.strip() for line in f]
|
||||||
|
return int(ids[-1])
|
||||||
|
|
||||||
# 处理歌曲导入请求
|
# 处理歌曲导入请求
|
||||||
def process_songs(song_info_list, cookie):
|
def process_songs(song_info_list, cookie):
|
||||||
@ -100,7 +124,12 @@ def process_songs(song_info_list, cookie):
|
|||||||
for song_info in song_info_list:
|
for song_info in song_info_list:
|
||||||
song_id = song_info['id']
|
song_id = song_info['id']
|
||||||
print(f"正在导入歌曲ID: {song_id}")
|
print(f"正在导入歌曲ID: {song_id}")
|
||||||
|
|
||||||
|
# 已上传则跳过
|
||||||
|
if has_uploaded(song_id, cookie):
|
||||||
|
print('该歌曲已上传,跳过!')
|
||||||
|
continue
|
||||||
|
|
||||||
# 查询歌曲的详细信息
|
# 查询歌曲的详细信息
|
||||||
song_details = get_song_details([song_id])
|
song_details = get_song_details([song_id])
|
||||||
if song_details:
|
if song_details:
|
||||||
@ -111,30 +140,47 @@ def process_songs(song_info_list, cookie):
|
|||||||
# 更新 song_info 添加 artist 和 album 信息
|
# 更新 song_info 添加 artist 和 album 信息
|
||||||
song_info['artist'] = song_artist
|
song_info['artist'] = song_artist
|
||||||
song_info['album'] = song_album
|
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
|
if result:
|
||||||
while attempts < 3:
|
success_songs = result.get('data', {}).get('successSongs', [])
|
||||||
result = import_song(song_info, cookie)
|
failed = result.get('data', {}).get('failed', [])
|
||||||
|
|
||||||
if result:
|
if success_songs:
|
||||||
success_songs = result.get('data', {}).get('successSongs', [])
|
print(f"歌曲 {song_id} 导入成功!")
|
||||||
failed = result.get('data', {}).get('failed', [])
|
break # 成功则跳出循环
|
||||||
|
else:
|
||||||
if success_songs:
|
print(f"歌曲 {song_id} 导入失败,失败原因:{failed}")
|
||||||
print(f"歌曲 {song_id} 导入成功!")
|
if all(f['code'] == -100 for f in failed): # 文件已存在的错误码
|
||||||
break # 成功则跳出循环
|
print(f"歌曲 {song_id} 文件已存在,跳过")
|
||||||
else:
|
save_failed_id(song_id) # 保存失败的 ID
|
||||||
print(f"歌曲 {song_id} 导入失败,失败原因:{failed}")
|
break
|
||||||
if all(f['code'] == -100 for f in failed): # 文件已存在的错误码
|
time.sleep(5) # 请求失败后等待 5 秒重新请求
|
||||||
print(f"歌曲 {song_id} 文件已存在,跳过")
|
attempts += 1
|
||||||
save_failed_id(song_id) # 保存失败的 ID
|
|
||||||
break
|
if attempts == 3: # 如果失败三次,则跳过此 ID
|
||||||
time.sleep(5) # 请求失败后等待 5 秒重新请求
|
print(f"歌曲 {song_id} 失败三次,跳过该歌曲。")
|
||||||
attempts += 1
|
save_failed_id(song_id) # 保存失败的 ID
|
||||||
|
|
||||||
if attempts == 3: # 如果失败三次,则跳过此 ID
|
def get_resume_song_info_list(song_info_list) -> list:
|
||||||
print(f"歌曲 {song_id} 失败三次,跳过该歌曲。")
|
last_failed_id = get_last_failed_id()
|
||||||
save_failed_id(song_id) # 保存失败的 ID
|
if last_failed_id is None:
|
||||||
|
print("暂无上传失败记录,从头开始上传")
|
||||||
|
return
|
||||||
|
for index, song_info in enumerate(song_info_list):
|
||||||
|
if int(song_info['id']) == last_failed_id:
|
||||||
|
print(f"当前已上传: {index + 1},最后上传失败的 id: {song_info['id']}")
|
||||||
|
return song_info_list[index + 1:]
|
||||||
|
|
||||||
# 主函数
|
# 主函数
|
||||||
def main():
|
def main():
|
||||||
@ -165,6 +211,9 @@ def main():
|
|||||||
song_info_list = get_all_song_info(songs_data)
|
song_info_list = get_all_song_info(songs_data)
|
||||||
#print(f"所有歌曲信息: {song_info_list}")
|
#print(f"所有歌曲信息: {song_info_list}")
|
||||||
|
|
||||||
|
# 从上次失败的歌曲开始上传
|
||||||
|
song_info_list = get_resume_song_info_list(song_info_list)
|
||||||
|
|
||||||
# 执行歌曲导入请求
|
# 执行歌曲导入请求
|
||||||
process_songs(song_info_list, cookie)
|
process_songs(song_info_list, cookie)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user