forked from mwang370/InstantSearchView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (43 loc) · 1.52 KB
/
index.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
var express = require('express');
var Bing = require('node-bing-api')({ accKey: "a43582571a4946d0a3e7afb34d09807d" });
var urlToImage = require('url-to-image');
var fs = require('fs');
var Q = require('q');
var webshot = require('webshot');
var http = require('follow-redirects').http;
var https = require('follow-redirects').https;
var app = express();
app.get('/:query', function (req, res, next) {
return Q.ninvoke(Bing, 'web', req.params['query'], {
top: 5, // Number of results (max 50)
skip: 0 // Skip first 3 results
}).then(function(result) {
var body = JSON.parse(result[0]['body']);
var arr = body.webPages.value.map(function(obj) {
//return Q.ninvoke(webshot, obj['displayUrl'], 'google.png')
//return {url: obj['displayUrl'], title: obj['name'], base64: 0}
return Q(urlToImage(obj['displayUrl'], 'google.png'))
.then(function(err) {
var base64str = base64_encode('google.png');
return {url: obj['displayUrl'], title: obj['name'], base64: base64str};
}).catch(function(err) {
console.error(err);
return {url: obj['displayUrl'], title: obj['name'], base64: 0};
});
});
return Q.all(arr);
}).then(function(arr) {
res.send(arr);
}).catch(function(err) {
console.error(err);
});
});
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
})