# -*- coding:utf-8 -*-
import os
import re
import requests
UC_PATH = 'f:/我的文档/桌面/mp3/CloudMusic/Cache/' # 缓存路径 例 D:/CloudMusic/Cache/
MP3_PATH = 'f:/我的文档/桌面/mp3/CloudMusic/Mp3/' # 存放歌曲路径
class Transform():
def do_transform(self):
files = os.listdir(UC_PATH)
for file in files:
if not file[-3:] == '.uc': # 后缀uc结尾为歌曲缓存
continue
mp3_content = None
with open(UC_PATH + file, mode='rb') as uc_file:
mp3_content = bytearray(uc_file.read())
for i, byte in enumerate(mp3_content):
byte ^= 0xa3
mp3_content[i] = byte
if not mp3_content:
continue
song_name = self.get_song_info_by_file(file)
print(song_name)
mp3_file_name = MP3_PATH + '%s.mp3' % (song_name)
with open(mp3_file_name, 'wb') as mp3_file:
mp3_file.write(mp3_content)
def get_song_info_by_file(self, file_name):
# -前面的数字是歌曲ID,例:1347203552-320-0aa1, 1347203552是歌曲ID
match_inst = re.match('\d*', file_name)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Connection": "keep-alive",
"Accept": "text/html,application/json,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8"
}
if match_inst:
song_id = match_inst.group()
else:
song_id = file_name
try:
url = 'http://music.163.com/api/song/detail/?id={0}&ids=%5B{1}%5D'.format(song_id, song_id)
response = requests.get(url, headers=headers)
jsons = response.json()
song_name = jsons['songs'][0]['name']
singer = jsons['songs'][0]['artists'][0]['name']
get_lyric(song_id, singer + "-" + song_name)
return singer + "-" + song_name
except:
return song_id
def get_lyric(song_id, name):
try:
headers = {
"user-agent": "Mozilla/5.0",
"Referer": "http://music.163.com",
"Host": "music.163.com"
}
if not isinstance(song_id, str):
song_id = str(song_id) # 格式化参数
# 歌词API路劲
url = f"http://music.163.com/api/song/lyric?id={song_id}+&lv=1&tv=-1"
response = requests.get(url, headers=headers)
jsons = response.json()
lrc = jsons["lrc"]["lyric"] # 解析json
#print(lrc)
path = MP3_PATH + '%s.lrc' % (name) # 路劲,可自行更改
with open(path, "w+")as f:
f.write(lrc) # 写入文件
return 1 # 返回值,暂无用处
except:
return 2 # 返回值,暂无用处
def check_path():
global UC_PATH, MP3_PATH
if not os.path.exists(UC_PATH):
print('缓存路径错误: {}'.format(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 True
if __name__ == '__main__':
if not check_path():
exit()
transform = Transform()
transform.do_transform()
感谢
https://github.com/Grente/cloudMusicTransform
我加了个同时进行歌词下载