Created
October 23, 2012 10:41
-
-
Save xiaojue/3938110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("没有找到这首歌"); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is config.js , xml2js and http
thanks!