-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchProxy.js
52 lines (46 loc) · 1.61 KB
/
searchProxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var http = require('http');
var util = require('util');
var events = require('events');
var fs = require('fs');
var iconv = require('iconv-lite');
var buffer = require('buffer');
exports.handle = function (req, res) {
var reqQuery = req.basicServer.urlParsed.query.keyword || '';
var url = 'http://suggestion.baidu.com/su?wd=' + reqQuery + '&sugmode=2&json=1';
var bdResData = '';
http.get(url, function (bdRes) {
bdRes.setEncoding('binary');
if (bdRes.statusCode == 200) {
bdRes.on('data', function (data) {
bdResData += data;
});
bdRes.on('end', function () {
var converted = iconv.decode(bdResData, 'gbk');
console.log('Baidu Orignal Data: \n' + converted);
var suggest = parseBaiduSuggest(converted);
var sugStr = JSON.stringify(suggest);
console.log('Baidu Res Data: \n' + sugStr);
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': sugStr ? new buffer.Buffer(sugStr).length : 0
});
res.end(sugStr);
});
} else {
console.warn('Baidu Res Code: ' + bdRes.statusCode);
res.end('error');
}
}).on('error', function (e) {
console.error('Get Baidu Search Suggest Error: ' + e);
res.end('error');
}).end();
}
function parseBaiduSuggest(str) {
var suggest = [];
str = str.replace(/window.baidu./, '');
function sug(data) {
suggest = data;
}
eval(str, 0);
return suggest;
}