-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake_lrc_music_m3u.py
393 lines (334 loc) · 13.4 KB
/
make_lrc_music_m3u.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*- encoding:utf-8 -*-
# 网抑云 lrc歌曲m3u生成器
# 版本: 13.1
import platform
import sys
import codecs
import hashlib
import json
import os
import urllib.request, urllib.parse, urllib.error
from http import cookiejar
import gzip
import signal
from sys import argv
from io import StringIO
import unicodedata, re
if len(argv) < 2:
print("请传入歌单id")
sys.exit()
# 歌单id设置 设置为 argv[1] 将使用 " python make_lrc_music_m3u.py 歌单id " 这种方式传入
playlistId = argv[1]
# 播放列表存放位置
m3udir = "./播放列表/"
# 相对于播放列表存放位置的 音乐存放位置
mp3dir_in_m3udir = "../网易云音乐/"
# 是否按m3u分类
if len(argv) > 2:
sortBym3u = True
else:
sortBym3u = False
# 是否下载歌词
downLrc = False
# 是否下载音乐
down128Music = False
# 账号cookie
# 由于不登录只会返回前10首歌 更多的需要登录 (歌单创建者 失效了可以打开一次输出的url再复制) 别人得到了这段cookie相当于能登录你的账号 请务必不要泄露
# Chrome打开网抑云 -> 登录 -> 按F12打开 开发者工具 -> 切换到 Console -> 输入 document.cookie 按回车 -> 复制输出内容替换下面的双引号
cookie = ""
translationTable = str.maketrans("àèéùâêîôûçë", "aeuaeeiouce")
# Ctrl + C 退出
def signal_handler(signal, frame):
print('Ctrl + C, exit now...')
sys.exit(1)
signal.signal(signal.SIGINT, signal_handler)
# 加载头部 防ban
cookieJar = cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookieJar))
opener.addheaders = [
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'),
('Accept-Encoding', 'gzip, deflate'),
('Accept-Language', 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6'),
('Connection', 'keep-alive'),
('Cookie', cookie),
('DNT', '1'),
('Host', 'music.163.com'),
('Upgrade-Insecure-Requests', '1'),
('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36')
]
urllib.request.install_opener(opener)
# 判断文件是否存在
def hasFile(fileName):
# 文件名不区分大小写
for i in listdir:
# 适配网易云的迷惑unicode行为 比如 '結束バンド' != '結束バンド' 文件名开始结束或歌手的全角空格
fn1 = unicodedata.normalize("NFC", re.sub(r'[()()]|[ ]', '', fileName))
fn2 = unicodedata.normalize("NFC", re.sub(r'[()()]|[ ]', '', i))
if fn1 == fn2:
return i
return ''
# 寻找存在的音频文件
fType = ''
def findHasMusicFileFullFileName(name):
global fType
fType = ''
fileTyps = ['.flac', '.ape', '.wav', '.mp3']
for t in fileTyps:
fullFileName = hasFile(name + t)
if fullFileName != '':
fType = t
return fullFileName
return ''
# 半角转全角
def half2full(ustring):
rstring = ""
for uchar in ustring:
inside_code = ord(uchar)
if inside_code == 32:
inside_code = 12288
elif inside_code >= 32 and inside_code <= 126:
inside_code += 65248
rstring += chr(inside_code)
return rstring
# 发送请求
def urlGetJsonLoad(url):
gzdata = ''
try:
gzdata = urllib.request.urlopen(url, timeout=10)
except Exception as e:
print('connect error: ', url)
return {'code': ''}
try:
if gzdata.info().get('Content-Encoding') == 'gzip':
gziper = gzip.GzipFile(fileobj=gzdata)
return json.loads(gziper.read().decode('utf-8'))
else:
return json.loads(gzdata.read().decode('utf-8'))
except Exception as e:
print('decode error: ', url)
return {'code': ''}
# 替换文件名不允许字符
def replaceName(name):
name = name.replace('?', half2full('?'))
name = name.replace('*', half2full('*'))
name = name.replace('/', half2full('/'))
name = name.replace('\\', half2full('\\'))
name = name.replace('<', half2full('<'))
name = name.replace('>', half2full('>'))
name = name.replace(':', half2full(':'))
name = name.replace('\"', half2full('\"'))
name = name.replace('|', half2full('|'))
name = name.replace('[', half2full('['))
name = name.replace(']', half2full(']'))
name = name.strip()
return name
# 获取歌词
def getLrc(tracksId):
url = 'http://music.163.com/api/song/lyric?lv=-1&tv=-1&id=' + tracksId
dataS = urlGetJsonLoad(url)
if dataS['code'] != 200:
ecode = str(dataS['code'])
print('errorCode: ' + ecode)
return ''
else:
if 'lrc' not in dataS or 'lyric' not in dataS['lrc'] or dataS['lrc']['lyric'] == None:
return ''
if 'tlyric' not in dataS or 'lyric' not in dataS['tlyric'] or dataS['tlyric']['lyric'] == None:
return dataS['lrc']['lyric']
# 按换行分割
lrcL = dataS['lrc']['lyric'].splitlines()
tlyricL = dataS['tlyric']['lyric'].splitlines()
tlyricD = {}
lrcStr = ''
# 分割翻译
for tlyric in tlyricL:
tl = tlyric.split(']', 1)
# 防止有时间但翻译为空
if len(tl) > 1:
tlyricD[tl[0]] = tl[1]
# 合并歌词
for lrc in lrcL:
l = lrc.split(']', 1)
if l[0] in tlyricD:
lrcStr += l[0] + ']' + l[1] + '\t' + tlyricD[l[0]] + '\n'
else:
lrcStr += lrc + '\n'
return lrcStr
# 下载歌曲
def downMusic(tracksId, fileName):
url = 'http://music.163.com/song/media/outer/url?id=' + tracksId + '.mp3'
try:
print('Download music: ' + fileName.replace(m3udir, '').replace(mp3dir, ''))
except:
print('Download music: ' + tracksId)
try:
urllib.request.urlretrieve(url, fileName)
if os.path.getsize(fileName) < 20000:
# 小于10k这音频肯定有问题 给它扬了
print('Need VIP')
os.remove(fileName)
return False
except:
print('error')
return False
return True
# 写出文件
def writeToFile(name, text):
try:
print('Write to file: ' + name.replace(m3udir, '').replace(mp3dir, ''))
except:
print('Write to file: ')
try:
file = codecs.open(name, "w", "utf-8")
file.write(text)
file.close()
except:
print('error')
# 生成播放列表
m3uText = "#EXTM3U"
def addPlaylist(mp3Title, mp3Name):
global m3uText
m3uText += "\n#EXTINF:" + mp3Title + "\n" + mp3dir.replace("/", "\\") + mp3Name
# 确保需要的文件夹
if not os.path.isdir(m3udir):
os.mkdir(m3udir)
if not os.path.isdir(m3udir + mp3dir_in_m3udir):
os.mkdir(m3udir + mp3dir_in_m3udir)
# 弄个文件来存对应关系
db = {}
if os.path.isfile(m3udir + 'db.json'):
with codecs.open(m3udir + 'db.json', "r", "utf-8") as dbf:
try:
db = json.load(dbf)
except:
print('Error DB')
else:
print('NO DB')
# 获取歌单
url = 'http://music.163.com/api/v6/playlist/detail?n=2147483647&id=' + playlistId
print(url)
dataL = urlGetJsonLoad(url)
if dataL['code'] != 200:
ecode = str(dataL['code'])
print('errorCode: ' + ecode)
else:
m3uName = replaceName(dataL['playlist']['name'])
allNum = len(dataL['playlist']['tracks'])
nowNum = 0
# 如按歌单分文件夹,不存在文件夹就创建
if sortBym3u:
mp3dir = mp3dir_in_m3udir + m3uName + "/"
if not os.path.isdir(m3udir + mp3dir):
os.mkdir(m3udir + mp3dir)
else:
mp3dir = mp3dir_in_m3udir
# 查找所有文件
listdir = os.listdir(m3udir + mp3dir)
noFileTxt = ''
# 循环歌单
for tracks in dataL['playlist']['tracks']:
nowNum += 1
print("\r" + str(nowNum) + '/ ' + str(allNum), end=' ')
fileName = ''
fileNameAndroid = ''
fileNameReverse = ''
fileNameReverseAndroid = ''
# 循环歌手
i = len(tracks['ar']) - 1
for artist in tracks['ar']:
if i > 0:
fileName += artist['name'] + ","
fileNameAndroid += artist['name'] + " "
fileNameReverse = "," + artist['name'] + fileNameReverse
fileNameReverseAndroid = " " + artist['name'] + fileNameReverse
i -= 1
elif artist['name']:
fileName += artist['name']
fileNameAndroid += artist['name']
fileNameReverse = artist['name'] + fileNameReverse
fileNameReverseAndroid = artist['name'] + fileNameReverse
# PC的命名规则 转全角
fileName += " - " + tracks['name'].strip()
fileName = replaceName(fileName)
# 按照空格分隔歌手,例如Android端
fileNameAndroid += " - " + tracks['name'].strip()
fileNameAndroid = replaceName(fileNameAndroid.replace('/', ' ').replace('*', ' ').replace('+', half2full('+')).replace('\"', '”'))
fileNameAndroidOld = fileNameAndroid.translate(translationTable)
# 歌手翻转的
fileNameReverse += " - " + tracks['name'].strip()
fileNameReverseAndroid += " - " + tracks['name'].strip()
fileNameReverse = replaceName(fileNameReverse)
fileNameReverseAndroid = replaceName(fileNameReverseAndroid)
fileNameReverseAndroidOld = fileNameReverseAndroid.translate(translationTable)
tid = str(tracks['id'])
# 检查存在的文件
fullFileNameAndroid = findHasMusicFileFullFileName(fileNameAndroid)
if fullFileNameAndroid == '':
fullFileName = ""
lastFindName = ""
# 从db找,找到了就重命名
if tid in db:
lastFindName = db[tid]
fullFileName = findHasMusicFileFullFileName(lastFindName)
# PC 命名方式重命名为Android命名方式
if fileNameAndroid != fileName:
lastFindName = fileName
fullFileName = findHasMusicFileFullFileName(lastFindName)
# 最近网易整理了一下歌手,处理一下已有文件翻转的情况
if fullFileName == '' and fileNameAndroid != fileNameReverse:
lastFindName = fileNameReverse
fullFileName = findHasMusicFileFullFileName(lastFindName)
if fullFileName == '' and fileNameAndroid != fileNameReverseAndroid:
lastFindName = fileNameReverseAndroid
fullFileName = findHasMusicFileFullFileName(lastFindName)
# 最近整理发型客户端突然支持法语子字符了 大量文件改名
if fullFileName == '' and fileNameAndroid != fileNameAndroidOld:
lastFindName = fileNameAndroidOld
fullFileName = findHasMusicFileFullFileName(lastFindName)
if fullFileName == '' and fileNameAndroid != fileNameReverseAndroidOld:
lastFindName = fileNameReverseAndroidOld
fullFileName = findHasMusicFileFullFileName(lastFindName)
if fullFileName != '':
try:
print('Rename file: ' + fullFileName)
except:
print('Rename file: ')
try:
os.rename(m3udir + mp3dir + fullFileName, m3udir + mp3dir + fileNameAndroid + fType)
fullFileNameAndroid = fileNameAndroid + fType
try:
os.remove(lastFindName + '.lrc')
except:
pass
except:
print('Rename error!')
fullFileNameAndroid = fileNameAndroid + fType
if fullFileNameAndroid == '':
fullFileNameAndroid = fileNameAndroid + '.mp3'
if down128Music:
# 这里将下载一个无封面的128kbps的版本 听个响
if downMusic(tid, m3udir + mp3dir + fullFileNameAndroid):
db[tid] = fileNameAndroid
else:
noFileTxt += fileNameAndroid + '\r\n'
else:
print('NO File: ' + fileNameAndroid)
noFileTxt += fileNameAndroid + '\r\n'
else:
db[tid] = fileNameAndroid
# 如果需要下载歌词,不存在歌词就下载
if downLrc:
if not hasFile(fileNameAndroid + ".lrc"):
lrcS = getLrc(tid)
if len(lrcS) > 0:
writeToFile(m3udir + mp3dir + fileNameAndroid + ".lrc", lrcS)
# 添加到播放列表 理论上这里fullFileName是不会为空的
if fullFileNameAndroid != '':
addPlaylist(tracks['name'], fullFileNameAndroid)
# 写db文件
writeToFile(m3udir + 'db.json', json.dumps(db, ensure_ascii=False))
# 没有文件的让人类处理
if noFileTxt != '':
writeToFile(m3udir + "noFile.txt", noFileTxt)
# 写播放列表文件
writeToFile(m3udir + m3uName + ".m3u", m3uText)