|
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. |
3 | 3 |
|
4 | 4 | 'use strict'; |
5 | 5 |
|
6 | | -let https = require('https'); |
| 6 | +let request = require('request'); |
7 | 7 |
|
8 | | -// ********************************************** |
9 | | -// *** Update or verify the following values. *** |
10 | | -// ********************************************** |
| 8 | +/** |
| 9 | + * This sample uses the Bing Video Search API with a query text that returns |
| 10 | + * links to relevant videos and metadata about them. |
| 11 | + */ |
11 | 12 |
|
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. |
13 | 14 | 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/videos/search'; |
18 | | - |
19 | | -let term = 'kittens'; |
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_video_search = function (search) { |
42 | | - console.log('Searching videos 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/videos/search'; |
| 16 | + |
| 17 | +// Topic you'd like to search for. |
| 18 | +let query = 'kittens'; |
| 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 |
54 | 34 | } |
55 | | -bing_video_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