1
This commit is contained in:
78
songUpdate.py
Normal file
78
songUpdate.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import requests
|
||||
import time
|
||||
|
||||
# 源API和目标API地址
|
||||
SOURCE_API = "https://union.godserver.cn/api/union/uni"
|
||||
TARGET_API = "http://100.80.156.98:58329/api/songs/insert"
|
||||
BATCH_SIZE = 50 # 每批上传数量
|
||||
|
||||
|
||||
def fetch_songs():
|
||||
"""从源API获取歌曲数据"""
|
||||
try:
|
||||
response = requests.get(SOURCE_API, timeout=30)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取歌曲数据失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def upload_batch(batch):
|
||||
"""上传一批歌曲数据"""
|
||||
try:
|
||||
# 转换数据格式,只保留需要的字段
|
||||
formatted_data = [{"id": item["id"], "title": item["title"]} for item in batch]
|
||||
|
||||
response = requests.post(
|
||||
TARGET_API,
|
||||
json=formatted_data,
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=30
|
||||
)
|
||||
response.raise_for_status()
|
||||
return True, response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"上传失败: {e}")
|
||||
return False, None
|
||||
|
||||
|
||||
def main():
|
||||
# 获取所有歌曲数据
|
||||
songs = fetch_songs()
|
||||
if not songs or not isinstance(songs, list):
|
||||
print("没有获取到有效的歌曲数据")
|
||||
return
|
||||
|
||||
total = len(songs)
|
||||
print(f"共获取到 {total} 首歌曲,开始分批次上传...")
|
||||
|
||||
# 分批处理
|
||||
for i in range(0, total, BATCH_SIZE):
|
||||
batch = songs[i:i + BATCH_SIZE]
|
||||
batch_num = i // BATCH_SIZE + 1
|
||||
batch_count = (total + BATCH_SIZE - 1) // BATCH_SIZE
|
||||
|
||||
print(f"正在上传第 {batch_num}/{batch_count} 批,共 {len(batch)} 条数据")
|
||||
|
||||
success, result = upload_batch(batch)
|
||||
if success:
|
||||
print(f"第 {batch_num} 批上传成功")
|
||||
else:
|
||||
print(f"第 {batch_num} 批上传失败,将重试...")
|
||||
# 失败重试一次
|
||||
time.sleep(2)
|
||||
success, result = upload_batch(batch)
|
||||
if success:
|
||||
print(f"第 {batch_num} 批重试成功")
|
||||
else:
|
||||
print(f"第 {batch_num} 批重试仍失败,请后续手动处理")
|
||||
|
||||
# 避免请求过于频繁
|
||||
time.sleep(1)
|
||||
|
||||
print("所有批次处理完毕")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user