Skip to content

Instantly share code, notes, and snippets.

@xyuanmu
Last active August 24, 2017 03:15
Show Gist options
  • Save xyuanmu/dcce1e88f68ed5e327f5a45294ac0f35 to your computer and use it in GitHub Desktop.
Save xyuanmu/dcce1e88f68ed5e327f5a45294ac0f35 to your computer and use it in GitHub Desktop.
关于网易云音乐的一些笔记,Python版本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
网易云音乐 New Api
'''
import os
import json
import base64
import binascii
from Crypto.Cipher import AES
from urllib.parse import urlencode
from urllib.request import Request, urlopen
modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
nonce = '0CoJUm6Qyw8W8jud'
pubKey = '010001'
# 登录加密算法, 基于https://github.com/stkevintan/nw_musicbox脚本实现
def encrypted_request(text):
text = json.dumps(text)
secKey = binascii.hexlify(os.urandom(16))[:16]
encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
encSecKey = rsaEncrypt(secKey, pubKey, modulus)
data = {'params': encText, 'encSecKey': encSecKey}
return data
def aesEncrypt(text, secKey):
pad = 16 - len(text) % 16
text = text + chr(pad) * pad
encryptor = AES.new(secKey, 2, '0102030405060708')
ciphertext = encryptor.encrypt(text)
ciphertext = base64.b64encode(ciphertext).decode('utf-8')
return ciphertext
def rsaEncrypt(text, pubKey, modulus):
text = text[::-1] # 字符串倒过来
rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16), int(modulus, 16))
return format(rs, 'x').zfill(256)
header = {
'X-Real-IP': '118.88.88.88',
'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
'Referer': 'http://music.163.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
def songs_detail_new_api(music_ids, bit_rate=320000):
action = 'http://music.163.com/weapi/song/enhance/player/url?csrf_token='
data = {'ids': [music_ids], 'br': bit_rate, 'csrf_token': ''}
request = Request(action, urlencode(encrypted_request(data)).encode(), headers=header)
result = json.loads(urlopen(request).read().decode('utf-8'))
open('info.txt', 'a').write(str(result['data'][0]['br']) + '\n' + result['data'][0]['url'] + '\n\n')
return result['data']
if __name__ == '__main__':
#print(songs_detail_new_api(326984)[0]['url'])
print(songs_detail_new_api(326984)[0]['url'])
#print(songs_detail_new_api(190449)[0]['url'])
#coding:utf-8
import urllib2
import json
import md5
import re
import sys
def encrypted_id(id):
byte1 = bytearray('3go8&$8*3*3h0k(2)2')
byte2 = bytearray(id)
byte1_len = len(byte1)
for i in xrange(len(byte2)):
byte2[i] = byte2[i]^byte1[i%byte1_len]
m = md5.new()
m.update(byte2)
result = m.digest().encode('base64')[:-1]
result = result.replace('/', '_')
result = result.replace('+', '-')
return result
if len(sys.argv)<=1:
print "\r\nURL EMPTY!\r\nPlease using same like 163.py <url>"+"\r\n"
exit()
url = sys.argv[1]
id = re.findall(r"(?:.*)[^user]id=(\d+)\D*",url)
print "\r\n"+str(id[0])
print "\r\nInfo:"
try:
id = id[0]
except:
print "\r\nURL ERROR!\r\nPlease using same like 163.py <url>"+"\r\n"
exit()
req = urllib2.urlopen("http://music.163.com/api/song/detail/?id=" + id + "&ids=%5B" + id + "%5D")
jsondata = json.loads(req.read())
try:
dfsid = jsondata['songs'][0]['hMusic']['dfsId']
print "SplRate:"+str(jsondata['songs'][0]['hMusic']['sr'])+"Hz"
print "BitRate:"+str(jsondata['songs'][0]['hMusic']['bitrate']/1000)+"kbps"
except:
try:
dfsid = jsondata['songs'][0]['mMusic']['dfsId']
print "SplRate:" + str(jsondata['songs'][0]['mMusic']['sr']) + "Hz"
print "BitRate:" + str(jsondata['songs'][0]['mMusic']['bitrate'] / 1000) + "kbps"
except:
try:
dfsid = jsondata['songs'][0]['lMusic']['dfsId']
print "SplRate:" + str(jsondata['songs'][0]['lMusic']['sr']) + "Hz"
print "BitRate:" + str(jsondata['songs'][0]['lMusic']['bitrate'] / 1000) + "kbps"
except:
try:
dfsid = jsondata['songs'][0]['bMusic']['dfsId']
print "SplRate:" + str(jsondata['songs'][0]['bMusic']['sr']) + "Hz"
print "BitRate:" + str(jsondata['songs'][0]['bMusic']['bitrate'] / 1000) + "kbps"
except:
print "Not Found Any Sound File"+"\r\n"
exit()
en_id = encrypted_id(str(dfsid))
name = jsondata['songs'][0]['name']
name = name.encode("GBK")
albname = jsondata['songs'][0]['album']['name']
albname = albname.encode("GBK")
artists = jsondata['songs'][0]['artists'][0]['name']
artists = artists.encode("GBK")
print "\r\n"+name+"\r\n"+artists+"\r\n"+albname
musicurl = "http://m2.music.126.net/"+ en_id + "/" + str(dfsid) + ".mp3"
print "\r\nDownload:"
print musicurl+"\r\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment