Skip to content

Instantly share code, notes, and snippets.

@xiaojue
Created October 23, 2012 10:41
Show Gist options
  • Save xiaojue/3938110 to your computer and use it in GitHub Desktop.
Save xiaojue/3938110 to your computer and use it in GitHub Desktop.
var config = require('./config.js'),
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 {
var url = result['result']['p2p'][0]['url'][0],
type = result['result']['p2p'][0]['type'][0];
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("没有找到这首歌");
}
}
});
@flyonok
Copy link

flyonok commented Jan 31, 2013

where is config.js , xml2js and http
thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment