Skip to content

Commit 90a7801

Browse files
authored
Update library
1 parent bf0bd9d commit 90a7801

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

nodejs/Search/BingNewsSearchv7.js

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
1-
//Copyright (c) Microsoft Corporation. All rights reserved.
2-
//Licensed under the MIT License.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
33

44
'use strict';
55

6-
let https = require('https');
6+
let request = require('request');
77

8-
// **********************************************
9-
// *** Update or verify the following values. ***
10-
// **********************************************
8+
/**
9+
* This sample uses the Bing News Search API to query the web with a
10+
* search term(s) and get relevant news sites in return.
11+
*/
1112

12-
// Add your Bing Search V7 subscription key to your environment variables.
13+
// Add your Bing Search V7 subscription key and endpoint to your environment variables.
1314
let subscriptionKey = process.env['BING_SEARCH_V7_SUBSCRIPTION_KEY']
14-
15-
// Add your Bing Search V7 endpoint to your environment variables.
16-
let host = process.env['BING_SEARCH_V7_ENDPOINT']
17-
let path = '/bing/v7.0/news/search';
18-
19-
let term = 'Microsoft';
20-
21-
let response_handler = function (response) {
22-
let body = '';
23-
response.on('data', function (d) {
24-
body += d;
25-
});
26-
response.on('end', function () {
27-
console.log('\nRelevant Headers:\n');
28-
for (var header in response.headers)
29-
// header keys are lower-cased by Node.js
30-
if (header.startsWith("bingapis-") || header.startsWith("x-msedge-"))
31-
console.log(header + ": " + response.headers[header]);
32-
body = JSON.stringify(JSON.parse(body), null, ' ');
33-
console.log('\nJSON Response:\n');
34-
console.log(body);
35-
});
36-
response.on('error', function (e) {
37-
console.log('Error: ' + e.message);
38-
});
39-
};
40-
41-
let bing_news_search = function (search) {
42-
console.log('Searching news for: ' + term);
43-
let request_params = {
44-
method : 'GET',
45-
hostname : host,
46-
path : path + '?q=' + encodeURIComponent(search),
47-
headers : {
48-
'Ocp-Apim-Subscription-Key' : subscriptionKey,
49-
}
50-
};
51-
52-
let req = https.request(request_params, response_handler);
53-
req.end();
15+
let endpoint = process.env['BING_SEARCH_V7_ENDPOINT'] + '/bing/v7.0/news/search';
16+
17+
// News topics you'd like to search for.
18+
let query = 'Microsoft';
19+
// Market you'd like to search in.
20+
let mkt = 'en-US'
21+
22+
// Construct parameters
23+
let request_params = {
24+
method: 'GET',
25+
uri: endpoint,
26+
headers: {
27+
'Ocp-Apim-Subscription-Key': subscriptionKey
28+
},
29+
qs: {
30+
q: query,
31+
mkt: mkt
32+
},
33+
json: true
5434
}
55-
bing_news_search(term);
35+
36+
// Make request
37+
request(request_params, function (error, response, body) {
38+
console.error('error:', error)
39+
console.log('statusCode:', response && response.statusCode)
40+
console.log('original query: ' + body.queryContext.originalQuery)
41+
console.log()
42+
console.log(body)
43+
})

0 commit comments

Comments
 (0)