-
Notifications
You must be signed in to change notification settings - Fork 0
/
Netease_cloud_CaChe.py
72 lines (62 loc) · 2.49 KB
/
Netease_cloud_CaChe.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
# -*- coding:utf-8 -*-
# 网易云音乐缓存转MP3算法
import os
import re
import requests
UC_PATH = 'D:\网易云音乐\缓存\Cache/' # 缓存路径 例 D:/CloudMusic/Cache/
MP3_PATH = './' # 存放歌曲路径
class Transform():
def do_transform(self,UC_PATH,MP3_PATH):
files = os.listdir(UC_PATH + '/')
for file in files:
if file[-2:] == 'uc': # 后缀uc结尾为歌曲缓存
print(file)
uc_file = open(UC_PATH + '/' + file, mode='rb')
uc_content = uc_file.read()
mp3_content = bytearray()
for byte in uc_content:
byte ^= 0xa3
mp3_content.append(byte)
song_id = self.get_songid_by_filename(file)
song_name, singer_name = self.get_song_info(song_id)
mp3_file_name = MP3_PATH + '%s - %s.mp3' % (singer_name, song_name)
mp3_file = open(mp3_file_name, 'wb')
mp3_file.write(mp3_content)
uc_file.close()
mp3_file.close()
print('success %s' % mp3_file_name)
def get_songid_by_filename(self, file_name):
match_inst = re.match('\d*', file_name) # -前面的数字是歌曲ID,例:1347203552-320-0aa1
if match_inst:
return match_inst.group()
return ''
def get_song_info(self, song_id):
if not song_id:
return str(song_id), ''
try:
url = 'https://api.imjad.cn/cloudmusic/' # 请求url例子:https://api.imjad.cn/cloudmusic/?type=detail&id=1347203552
payload = {'type': 'detail', 'id': song_id}
reqs = requests.get(url, params=payload)
jsons = reqs.json()
song_name = jsons['songs'][0]['name']
singer = jsons['songs'][0]['ar'][0]['name']
return song_name, singer
except:
return str(song_id), 'UnName'
def check_path(UC_PATH,MP3_PATH):
if not os.path.exists(UC_PATH):
print('缓存路径错误: %s' % UC_PATH)
return False
if not os.path.exists(MP3_PATH):
print('目标路径错误: %s' % MP3_PATH)
return False
if UC_PATH[-1] != '/': # 容错处理 防止绝对路径结尾不是/
UC_PATH += '/'
if MP3_PATH[-1] != '/':
MP3_PATH += '/'
return UC_PATH,MP3_PATH
if __name__ == '__main__':
if not check_path():
exit()
transform = Transform()
transform.do_transform(UC_PATH,MP3_PATH)