-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathv.24-10-06.py
262 lines (221 loc) · 9.67 KB
/
v.24-10-06.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import requests
from mutagen.mp3 import MP3
import time
import signal
import sys
import re
from tabulate import tabulate
def download_file(url, file_path, file_type, index, total_files, timeout=10):
try:
response = requests.get(url, stream=True, timeout=timeout)
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
downloaded_size = 0
with open(file_path, "wb") as file:
for data in response.iter_content(chunk_size=4096):
downloaded_size += len(data)
file.write(data)
progress = downloaded_size / total_size * 100 if total_size > 0 else 0
print(f"正在下载 [{index}/{total_files}][{file_type}] {file_path},进度:{progress:.2f}%\r", end="")
print(f"下载完成 [{index}/{total_files}][{file_type}] {file_path}")
return True
except requests.exceptions.RequestException as e:
print(f"下载 [{index}/{total_files}][{file_type}] {file_path} 失败:{e}")
return False
def download_lyrics(url, lrc_path, song_index, total_songs):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
with open(lrc_path, "w", encoding="utf-8") as lrc_file:
lrc_file.write(response.content.decode('utf-8'))
print(f"下载完成 [{song_index}/{total_songs}][LRC] {lrc_path}")
return True
except requests.exceptions.RequestException as e:
print(f"下载歌词失败:{e}")
return False
def safe_filename(filename):
invalid_chars = '\\/:*?"<>|'
for char in invalid_chars:
filename = filename.replace(char, '_')
return filename
def delete_file(file_path):
if os.path.exists(file_path):
os.remove(file_path)
print(f"删除文件:{file_path}")
def song_table(data):
table_data = []
for idx, item in enumerate(data, start=1):
name = item['name']
artist = item['artist']
url_id = re.search(r'\d+', item['url']).group()
table_data.append([idx, name, artist, url_id])
table_headers = ["序号", "标题", "艺术家", "ID"]
table = tabulate(table_data, table_headers, tablefmt="pipe")
print(table)
def exit_ctrl_c(sig, frame):
print("\n退出程序...")
sys.exit(0)
def welcome():
print("welcome")
print("欢迎使用我开发的小程序")
print("Github Rope: https://github.com/God-2077/python-code/")
print("-------------------------")
print("网易云音乐歌单批量下载歌曲")
def download_playlist(playlist_id, download_path):
if not playlist_id.isdigit():
print("歌单ID必须为数字")
return
error_song_name = []
error_song_id = []
api_urls = [
"https://meting.qjqq.cn/?type=playlist&id=",
"https://api.injahow.cn/meting/?type=playlist&id=",
"https://meting-api.mnchen.cn/?type=playlist&id=",
"https://meting-api.mlj-dragon.cn/meting/?type=playlist&id="
]
selected_api = None
data = None
for api_url in api_urls:
try:
response = requests.get(f"{api_url}{playlist_id}", timeout=10)
# if requests.status_codes != 200:
# print("出错了...")
# print(f"状态码:{requests.status_codes}")
# return
response.raise_for_status()
data = response.json()
selected_api = api_url
if 'error' in data:
error = data.get("error", "")
print("出错了...")
print(f"错误详细:{error}")
return
break
except requests.exceptions.RequestException as e:
print(f"API {api_url} 请求失败:{e}")
continue
if not data:
print("所有API都无法获取数据")
return
os.makedirs(download_path, exist_ok=True)
print(f"Meting API: {selected_api}")
total_songs = len(data)
print(f"歌单共有 {total_songs} 首歌曲")
song_table(data)
envisage_size = total_songs * 7.7
print(f"歌单共有 {total_songs} 首歌曲,预计歌曲文件总大小为 {envisage_size} MB")
chose = str(input("是否继续下载?(yes): "))
if chose not in ["y", "", "yes"]:
print("退出程序...")
sys.exit(0)
chose = str(input("是否下载小于 60 秒的歌曲(可能为试听音乐)?(not): "))
if chose not in ["y", "", "yes"]:
downtrymusic = 1
else:
downtrymusic = 0
successful_downloads = 0
failed_downloads = []
def signal_handler(sig, frame):
print("\n检测到Ctrl+C, exiting gracefully...")
print(f"程序运行完成,共 {total_songs} 首歌曲,成功下载 {successful_downloads} 首歌曲")
if failed_downloads:
print(f"共有 {len(failed_downloads)} 首歌曲下载失败")
print("失败列表如下")
table_data = [[i + 1, error_song_name[i], error_song_id[i]] for i in range(len(error_song_name))]
print(tabulate(table_data, headers=["序号", "标题 - 艺术家", "ID"], tablefmt="grid"))
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
for index, song in enumerate(data, start=1):
name = song.get("name", "")
artist = song.get("artist", "")
url = song.get("url", "")
lrc = song.get("lrc", "")
pic = song.get("pic", "")
if not name or not artist or not url or not lrc:
print(f"歌曲信息不完整:{song}")
continue
song_filename = f"{safe_filename(name)} - {safe_filename(artist)}.mp3"
song_name = f"{safe_filename(name)} - {safe_filename(artist)}"
song_path = os.path.join(download_path, song_filename)
retry_count = 0
while retry_count < 5:
if download_file(url, song_path, "MP3", index, total_songs):
successful_downloads += 1
state = True
break
else:
retry_count += 1
print(f"重试下载 [{index}/{total_songs}][MP3] {song_path},次数:{retry_count}")
state = False
time.sleep(1)
if state == False:
print("")
match = re.search(r'\d+', url)
error_song_id.append(int(match.group()))
error_song_name.append(song_name)
failed_downloads.append(match)
if state == True:
try:
audio = MP3(song_path)
audio_duration = audio.info.length
if downtrymusic == 1:
if audio_duration < 60:
print(f"歌曲时长小于一分钟,删除歌曲和取消下载歌词和图片:{song_path}")
delete_file(song_path)
audio_duration_TF = False
successful_downloads -= 1
match = re.search(r'\d+', url)
error_song_id.append(int(match.group()))
error_song_name.append(song_name)
else:
print(f"歌曲时长为 {audio_duration} 秒")
audio_duration_TF = True
else:
print(f"歌曲时长为 {audio_duration} 秒")
audio_duration_TF = True
except Exception as e:
print(f"无法获取歌曲时长:{e}")
print("应该为 VIP 单曲,删除歌曲文件和取消下载歌词和图片")
delete_file(song_path)
audio_duration_TF = False
successful_downloads -= 1
match = re.search(r'\d+', url)
error_song_id.append(int(match.group()))
error_song_name.append(song_name)
failed_downloads.append(match)
if audio_duration_TF:
lrc_filename = f"{safe_filename(name)} - {safe_filename(artist)}.lrc"
lrc_path = os.path.join(download_path, lrc_filename)
retry_count = 0
while retry_count < 5:
if download_lyrics(lrc, lrc_path, index, total_songs):
break
else:
retry_count += 1
print(f"重试下载 [{index}/{total_songs}][LRC] {lrc_path},次数:{retry_count}")
time.sleep(1)
pic_filename = f"{safe_filename(name)} - {safe_filename(artist)}.jpg"
pic_path = os.path.join(download_path, pic_filename)
retry_count = 0
while retry_count < 5:
if download_file(pic, pic_path, "PIC", index, total_songs):
break
else:
retry_count += 1
print(f"重试下载 [{index}/{total_songs}][PIC] {pic_path},次数:{retry_count}")
time.sleep(1)
print(f"程序运行完成,共 {total_songs} 首歌曲,成功下载 {successful_downloads} 首歌曲")
if failed_downloads or error_song_name:
print(f"共有 {len(failed_downloads)} 首歌曲下载失败")
print("失败列表如下")
table_data = [[i + 1, error_song_name[i], error_song_id[i]] for i in range(len(error_song_name))]
print(tabulate(table_data, headers=["序号", "标题 - 艺术家", "ID"], tablefmt="grid"))
if __name__ == "__main__":
signal.signal(signal.SIGINT, exit_ctrl_c)
welcome()
playlist_id = input("歌单ID:")
download_path = input(r"下载路径(默认为 ./down):") or r"./down"
download_playlist(playlist_id, download_path)