This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__init__.py
368 lines (338 loc) · 16.1 KB
/
__init__.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
'''
163ListDownloader by CooooldWind_
Version 1.3.0-23020a
Sourcecode follows GPL-3.0 licence.
Updates:
1. 修复漏洞:选择“属性编辑”而不选择“下载歌曲”时的反馈
2. 删除对pprint的冗余库需求
'''
import random,time,os,eyed3,requests,threading,shutil
from mutagen.id3 import ID3,APIC
from PIL import Image
from .params_encSecKey import *
playlist_api = "https://music.163.com/weapi/v6/playlist/detail?"
music_info_api = "https://music.163.com/weapi/v3/song/detail"
lyric_api = "https://music.163.com/weapi/song/lyric?csrf_token="
def clean(s):
dirty = ["/","\\",":","*","\"","?","|","<",">"]
for i in dirty:
s = s.replace(i,"")
return s
class playlist():
def __init__(self, id):
self.id = id
self.longlist = "[Error] Didn't run data_get function."
self.shortlist = "[Error] Didn't run data_get function."
self.creater = "[Error] Didn't run data_get function."
self.playlist_name = "[Error] Didn't run data_get function."
def data_get(self):
'全局一下API'
global playlist_api
global music_info_api
'爬歌曲ID'
try: get_list = Netease_params(
{'csrf_token': "", 'id': str(self.id), 'n': "0"}
).run(playlist_api)['playlist']
except KeyError: pass
else: pass
'存创建者ID待会儿仿ta本人获取详细信息,顺便测下爬没爬成'
try: self.creater = str(get_list['userId'])
except:
raise Exception("Playlist-Get Error: Server refused the request, this playlist may not be accessible.")
self.playlist_name = get_list['name']
'存下所有歌ID'
self.shortlist = [{'id':i['id']} for i in get_list['trackIds']]
'爬歌曲详细信息'
try: get_music = Netease_params(
{'c': str(self.shortlist), 'csrf_token': '', 'userId': self.creater}
).run(music_info_api)['songs']
except TimeoutError:
raise Exception("Playlist-Get Error: Connect time is too long to wait.")
else: pass
'longlist就是包含各种信息的大表'
self.longlist = []
'开始填写信息'
now = 0
for i in range(len(get_music)):
'伪随机种子,基于歌曲id'
random.seed(get_music[i]['id'])
'歌曲名'
name = get_music[i]['name']
'艺人'
artists = ""
for j in get_music[i]['ar']:
artists += j['name'] + ","
artists = artists[0: len(artists) - 1]
'专辑'
album = get_music[i]['al']['name']
'歌名、艺人、专辑检测违规字符并去除'
name = clean(name)
artists = clean(artists)
album = clean(album)
self.playlist_name = clean(self.playlist_name)
'歌曲、歌词、专辑封面文件名'
music_filename = name + " - " + artists + ".mp3"
lyric_filename = name + " - " + artists + ".lrc"
cover_filename = name + " - " + artists + ".jpg"
'封面和音乐的链接'
cover_link = get_music[i]['al']['picUrl']
music_link = "https://music.163.com/song/media/outer/url?id=" + str(get_music[i]['id']) + ".mp3"
'uid获取,为25位包含26个大写字母和数字的字符串,每五个用横杠隔开'
uid = ""
for j in range(5):
uid += "".join(random.sample('1234567890QWERTYUIOPASDFGHJKLZXCVBNM',5))
uid += "-"
uid = uid[0: len(uid) - 1]
'最后把以上信息汇总到一起'
appending = {
'name': name,
'artists': artists,
'album': album,
'music_filename': music_filename,
'lyric_filename': lyric_filename,
'cover_filename': cover_filename,
'cover_link': cover_link,
'music_link': music_link,
'uid': uid,
'id': now,
'id_show': now + 1,
'music_id': get_music[i]['id']
}
self.longlist.append(appending)
now += 1
def download_info_add(self, path, args, filename):
'针对每首歌都添加参数和地址'
for now in self.longlist:
'补充斜杠'
if path[len(path) - 1] != "/": path += "/"
now['path'] = path
'参数第一项是是否分类文件夹,为true自动添加一个uid子文件夹'
if args[0]:
now['path'] += self.playlist_name + "/"
'参数第2-5项'
now['path'] += now['uid'] + "/"
now['path'] = now['path'].replace("/","\\")
now['args'] = args[1: len(args)]
'文件重命名'
music_id = now['music_id']
filename_copy = str(filename)
filename_copy = filename_copy.replace("$title$",now['name'])
filename_copy = filename_copy.replace("$artists$",now['artists'])
filename_copy = filename_copy.replace("$album$",now['album'])
filename_copy = filename_copy.replace("$music_id$",str(now['music_id']))
filename_copy = filename_copy.replace("$list_id$",str(now['id_show']))
filename_copy = clean(filename_copy)
now['music_filename'] = filename_copy + ".mp3"
now['lyric_filename'] = filename_copy + ".lrc"
now['cover_filename'] = filename_copy + ".jpg"
print(filename_copy)
def download_main(self, thread_sum):
self.download_status = []
for i in self.longlist:
self.download_status.append({
'state': 0,
'value': "Waiting",
'name': i['name'] + " - " + i['artists']
})
self.total_size = len(self.longlist)
self.now_size = 0
self.failure_size = 0
self.success_size = 0
thread_controller = threading.Semaphore(thread_sum)
for i in self.longlist:
'向music内参传入playlist外参就能让它修改文件了(吧?)'
thread = music(i, thread_controller, self)
thread.start()
time.sleep(0)
class music(threading.Thread):
def __init__(self, info, thread_controller, father):
threading.Thread.__init__(self)
self.info = info
self.thread_controller = thread_controller
self.father = father
self.music_download = self.info['args'][0]
self.lyric_download = self.info['args'][1]
self.cover_download = self.info['args'][2]
self.attribute_write = self.info['args'][3]
self.id = self.info['id']
def run(self):
with self.thread_controller:
try: os.makedirs(self.info['path'])
except: pass
'下载音乐'
if self.music_download:
self.father.download_status[self.id]['state'] = 1
music_file = open(self.info['path'] + self.info['music_filename'],'wb+')
'获取请求头'
while True:
try: music_source = requests.get(self.info['music_link'], allow_redirects = True, stream = True)
except: pass
else: break
'计数器'
rate = int(0)
if music_source.headers['Content-Type'] == 'text/html;charset=utf8':
music_file.close()
time.sleep(0.05)
os.remove(self.info['path'] + self.info['music_filename'])
os.removedirs(self.info['path'])
self.father.download_status[self.id]['value'] = "Error:It's a VIP song, so we can't download it."
self.father.download_status[self.id]['state'] = 6
self.father.failure_size += 1
self.father.now_size += 1
return None
else: totalsize = int(music_source.headers['Content-Length'])
'开始下载'
for data in music_source.iter_content(chunk_size = 1024):
music_file.write(data)
rate += len(data)
self.father.download_status[self.id]['value'] = round(float(rate / totalsize), 2)
'别一直干活,人总是要休息的,服务器也一样,别累坏人家了'
if random.randint(0,1000) % 200 == 0:
time.sleep(0.01)
music_file.close()
time.sleep(0.5)
'歌词下载'
if self.lyric_download:
self.father.download_status[self.id]['state'] = 2
self.father.download_status[self.id]['value'] = 0
lyric_file = open(self.info['path'] + self.info['lyric_filename'],'w+',encoding = 'utf-8')
'调用api'
global lyric_api
'请求歌词'
while True:
try: data = Netease_params({
'csrf_token':"",
'id':self.info['music_id'],
'lv':'-1',
'tv':'-1'}).run(lyric_api)
except: pass
else: break
'写入并替换换行符'
lyric_file.write(data['lrc']['lyric'].replace("\n",'\n'))
lyric_file.close()
self.father.download_status[self.id]['value'] = 1
time.sleep(0.5)
'封面下载'
if self.cover_download:
self.father.download_status[self.id]['state'] = 3
self.father.download_status[self.id]['value'] = 0
cover_file = open(self.info['path'] + self.info['cover_filename'],'wb+')
'获取请求头'
while True:
try: cover_source = requests.get(self.info['cover_link'], allow_redirects = True, stream = True)
except: pass
else: break
'计数器'
rate = int(0)
totalsize = int(cover_source.headers['Content-Length'])
'开始下载'
for data in cover_source.iter_content(chunk_size = 1024):
cover_file.write(data)
rate += len(data)
self.father.download_status[self.id]['value'] = round(float(rate / totalsize * 0.8), 2)
'别一直干活,人总是要休息的,服务器也一样,别累坏人家了'
if random.randint(0,1000) % 200 == 0:
time.sleep(0.01)
cover_file.close()
'换一种打开方式'
cover_file = Image.open(self.info['path'] + self.info['cover_filename'])
'更换通道'
cover_file = cover_file.convert("RGB")
cover_file_type = cover_file.format
'压缩边长'
cover_file_out = cover_file.resize((800,800), Image.NEAREST)
'保存'
cover_file_out.save(self.info['path'] + self.info['cover_filename'], cover_file_type)
self.father.download_status[self.id]['value'] = 1
time.sleep(0.5)
'属性填写'
if self.attribute_write and self.music_download:
'利用eyed3完成第一步骤'
self.father.download_status[self.id]['state'] = 4
self.father.download_status[self.id]['value'] = 0
music_file = eyed3.load(self.info['path'] + self.info['music_filename'])
music_file.tag.artist = self.info['artists']
self.father.download_status[self.id]['value'] = 0.25
music_file.tag.artist = self.info['album']
self.father.download_status[self.id]['value'] = 0.5
music_file.tag.save()
'改用ID3完成第二步骤,需要图像文件支持'
if self.cover_download:
cover_file = open(self.info['path'] + self.info['cover_filename'],'rb+')
music_file = ID3(self.info['path'] + self.info['music_filename'])
music_file.add(APIC(encoding = 3,mime = 'image/jpeg',type = 3,desc = u'Cover',data = cover_file.read()))
music_file.save(v2_version = 3)
cover_file.close()
'过 河 拆 桥'
os.remove(self.info['path'] + self.info['cover_filename'])
self.father.download_status[self.id]['value'] = 1
time.sleep(0.5)
'最后的修改:剪切并删除文件夹'
self.father.download_status[self.id]['state'] = 5
self.father.download_status[self.id]['value'] = "Finishing"
self.copy_to_path = self.info['path'][0: len(self.info['path']) - 31]
if self.music_download:
try: shutil.move(self.info['path'] + self.info['music_filename'], self.copy_to_path)
except:
os.remove(self.copy_to_path + "\\" + self.info['music_filename'])
shutil.move(self.info['path'] + self.info['music_filename'], self.copy_to_path)
if self.lyric_download:
try: shutil.move(self.info['path'] + self.info['lyric_filename'], self.copy_to_path)
except:
os.remove(self.copy_to_path + "\\" + self.info['lyric_filename'])
shutil.move(self.info['path'] + self.info['lyric_filename'], self.copy_to_path)
shutil.rmtree(self.info['path'])
self.father.download_status[self.id]['state'] = 6
self.father.download_status[self.id]['value'] = "Successful"
self.father.now_size += 1
self.father.success_size += 1
return None
'''----------入门教学----------'''
'''定义类 '''
# p = playlist("YOUR ID HERE")
'''定义完不会自动获取数据,请对它下读取命令'''
# p.data_get()
'''
下载启动前请存入下载信息,分别是
("存储路径",[歌单文件夹分类(T/F),下载歌曲(T/F),下载歌词(T/F),下载专辑封面(T/F),属性编辑(T/F)],文件名)
(T/F)代表该部分填入True/False。
文件名这么填:
打一个双引号,里面是字,注意:
$title$是歌曲名
$artists$是歌手
$album$是专辑
$music_id$是歌曲的网易云ID
$list_id$是歌曲在歌单中的序号
例:
"[$music_id$]$artists$ - $title$ - MyFavorite"
下载下来的歌,名字会成为
"[543987451]Vicetone,Cozi Zuehlsdorff - Way Back - MyFavorit.mp3"
'''
# p.download_info_add("C:/Users/Administrator/Desktop/Test",[True,True,True,True,True],"[$music_id$]$artists$ - $title$ - MyFavorite")
'''下载开始时请传入线程数量'''
# p.download_main(4)
'''
其他信息
p.longlist - 歌曲全部信息
p.now_size - 下载进度
p.success_size - 下载成功的个数
p.failure_size - 下载失败的个数
p.total_size - 歌曲总数
download_status - 下载详情,以[{'state':数字,'value':数值或报错,'name':歌曲名称},...]组成
以下是例子
它的输出:
进度: 0.0% (0/0/10)
进度: 0.0% (0/0/10)
......
进度: 70.0% (7/0/10)
进度: 70.0% (7/0/10)
进度: 90.0% (9/0/10)
成功: 10; 失败: 0
'''
'''
while p.now_size != p.total_size:
now = round(p.now_size / p.total_size * 100, 3)
print("进度: " + str(now) + "% (" + str(p.success_size) + "/" + str(p.failure_size) + "/" + str(p.total_size) + ")")
time.sleep(1)
print("成功: " + str(p.success_size) + "; 失败: " + str(p.failure_size))
'''