扔个抓百度音乐API的Nodejs js。有想用的拿走。
扔个抓百度音乐API的Nodejs js。有想用的拿走。
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(“没有找到这首歌”); } } });
赞
针对“扔个抓百度音乐API的Nodejs js。有想用的拿走。”这个帖子,我可以提供一个简单的示例代码来抓取百度音乐的API。不过需要注意的是,百度音乐API并非公开且官方支持的接口,因此可能随时发生变化或不可用。另外,由于版权等问题,很多音乐网站不建议直接通过API获取音频文件。
尽管如此,这里我将提供一个基本的示例,说明如何使用Node.js去调用百度音乐API,并抓取一些信息。本示例代码仅供参考,具体实现时需要根据实际API文档进行调整:
const axios = require('axios');
async function fetchBaiduMusicData(songName, artist) {
const url = `http://musicapi.qianqian.com/v1/restserver/ting?method=baidu.ting.search.catalogSug&query=${songName} ${artist}`;
try {
const response = await axios.get(url);
if (response.data && response.data.song.length > 0) {
console.log("找到歌曲:", response.data.song[0]);
// 这里可以根据实际需求处理返回的数据
} else {
console.log("没有找到这首歌");
}
} catch (error) {
console.error("请求失败:", error.message);
}
}
// 使用示例
fetchBaiduMusicData('青花瓷', '周杰伦');
请注意,上述URL中的域名可能不是最新的,且由于API的私有性质,该链接可能在未来无法访问。如果需要更稳定的服务,建议寻找其他公开且官方支持的音乐API,例如网易云音乐、Spotify等。
此外,根据需求的不同,可能还需要解析更多的信息,如歌词、专辑封面等,这些都可以根据具体的API返回结果进一步处理。