https://gist.github.com/3938110
api写好了。。不过貌似这个接口必须写准作者和歌曲标题。。大家有更好的音乐api吗。。?求推荐,要有带试听的。。
以前年轻不懂事抓过百度掌门人的接口,不过当时是用C#写的,现在也懒得回去弄了:
https://zmenfm.codeplex.com/SourceControl/latest#ZMenFM.Network/ZMConstUrl.cs
修改了下 可以用了
var xml2js = require(‘xml2js’), http = require(‘http’);
var baiduZm = function() { this.root = 'box.zhangmen.baidu.com’; };
baiduZm.prototype = { constructor: baiduZm, get: function (uri, callback) { var req = http.request({ host: this.root, port: 80, path: uri, method: ‘GET’ }, function(res) { var ret = '’; res.setEncoding(‘utf8’); res.on('data’, function (chunk) { ret += chunk; }); res.on('end’, function () { callback(null, ret); }); });
req.on('error’, function (err) { callback(err); });
req.end(); }, getUri: function(param) { return ‘/x?op=12&count=1&title=’ + encodeURIComponent(param.title) + ‘$$’ + encodeURIComponent(param.author) + '$$$$’; }, searchTrack: function(param, callback) { var uri = this.getUri(param); this.get(uri, function(err, xml) { if (err) callback(err); else {
var parser = new xml2js.Parser();
parser.parseString(xml, function(err, result) {
if (err) callback(err);
else {
if (result['result']['count'] == '0') {
callback(null, {
ret: false
});
return false;
}
var path = result['result']['url'][0]['encode'] + '';
var file = result['result']['url'][0]['decode'] + '';
path = path.replace(/[^\/]+$/ig, '');
var url = path + file;
var type = '';
if (result['result']['p2p']) type = result['result']['p2p'][0]['type'][0];
else type = file.match(/\.([^?]+)/)[1] + '';
if (url) {
callback(null, {
url: url,
type: type,
ret: true
});
} else {
callback(null, {
ret: false
});
}
}
});
} }); } };
(new baiduZm()).searchTrack({ author: '温岚’, title: ‘屋顶’ }, function (err, data){ if (err) { throw err; } else { if (data.ret) { console.log(data); } else { console.log(“没有找到这首歌”); } } });