-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-bing-demo.js
67 lines (58 loc) · 2.33 KB
/
js-bing-demo.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
let https = require('https');
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
let subscriptionKey = 'd9a473a7974d425aae01cff01863c9f0';
// Verify the endpoint URI. At this writing, only one endpoint is used for Bing
// search APIs. In the future, regional endpoints may be available. If you
// encounter unexpected authorization errors, double-check this host against
// the endpoint for your Bing Search instance in your Azure dashboard.
let host = 'api.cognitive.microsoft.com';
let path = '/bing/v7.0/images/search';
let term = 'Do you want to go to the toilet?';
term += ' icon';
let response_handler = function (response) {
let body = '';
let linkOne = '';
let linkTwo = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
// console.log('\nRelevant Headers:\n');
// for (var header in response.headers)
// header keys are lower-cased by Node.js
// if (header.startsWith("bingapis-") || header.startsWith("x-msedge-"))
// console.log(header + ": " + response.headers[header]);
linkOne = JSON.stringify(JSON.parse(body)["value"][0]["contentUrl"], null, ' ');
linkTwo = JSON.stringify(JSON.parse(body)["value"][1]["contentUrl"], null, ' ');
// print(json.dumps(json.loads(result)["value"][0]["contentUrl"], indent=4))
// console.log('\nJSON Response:\n');
console.log(linkOne);
console.log(linkTwo);
});
response.on('error', function (e) {
console.log('Error: ' + e.message);
});
};
let bing_image_search = function (search) {
// console.log('Searching images for: ' + term);
let request_params = {
method : 'GET',
hostname : host,
path : path + '?q=' + encodeURIComponent(search),
headers : {
'Ocp-Apim-Subscription-Key' : subscriptionKey,
}
};
let req = https.request(request_params, response_handler);
req.end();
}
if (subscriptionKey.length === 32) {
bing_image_search(term);
} else {
console.log('Invalid Bing Search API subscription key!');
console.log('Please paste yours into the source code.');
}