-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageSearcher.js
45 lines (40 loc) · 1.33 KB
/
ImageSearcher.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
'use strict';
import https from "https";
class ImageSearcher {
//Constructor
constructor(term) {
this.key = "fd2d758f159e4132a378b8462d8b235f";
this.host = "api.cognitive.microsoft.com";
this.path = "/bing/v7.0/images/search";
this.numberResults = '20';
this.options = {
method: 'GET',
hostname: this.host,
path: this.path + '?q=' + encodeURIComponent(term) + encodeURI("&size=medium&count=" + this.numberResults +"&offset=0"),
headers: {
'Ocp-Apim-Subscription-Key': this.key,
"Ocp-Apim-Subscription-Region": "canadacentral",
'Content-type': 'application/json',
},
json: true,
};
}
//translate() function
search(cb){
let response_handler = function (response) {
let body = '';
let imgArray = [];
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
let imgResults = JSON.parse(body).value;
imgResults.forEach(res => imgArray.push(res.contentUrl));
cb(imgArray);
});
};
let req = https.request(this.options, response_handler);
req.end();
}
}
export {ImageSearcher}