|
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 | # -*- coding: utf-8 -*- |
5 | 5 |
|
6 | | -import http.client |
7 | 6 | import json |
8 | 7 | import os |
9 | | -import urllib.parse |
| 8 | +from pprint import pprint |
| 9 | +import requests |
10 | 10 |
|
11 | 11 | ''' |
12 | | -This sample makes a call to the Bing Video Search API with a topic query and returns relevant video links with data. |
| 12 | +This sample makes a call to the Bing Video Search API with a topic query and returns relevant video with data. |
13 | 13 | Documentation: https: // docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/ |
14 | 14 | ''' |
15 | 15 |
|
16 | 16 | # Add your Bing Search V7 subscription key and endpoint to your environment variables. |
17 | 17 | subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY'] |
18 | 18 | host = os.environ['BING_SEARCH_V7_ENDPOINT'] |
19 | | -host = host.replace('https://', '') |
20 | 19 | path = "/bing/v7.0/videos/search" |
21 | 20 |
|
22 | 21 | # Search query |
23 | | -term = "kittens" |
| 22 | +query = "kittens" |
24 | 23 |
|
25 | 24 | # Construct a request |
26 | | -headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} |
27 | | -conn = http.client.HTTPSConnection(host) |
28 | | -query = urllib.parse.quote(term) |
| 25 | +headers = { |
| 26 | + 'Content-Type': 'application/json', |
| 27 | + 'Ocp-Apim-Subscription-Key': subscriptionKey |
| 28 | + } |
| 29 | +params = { "q": query } |
29 | 30 |
|
30 | 31 | # Call the API |
31 | | -print('Searching videos for: ', term) |
32 | | -conn.request("GET", path + "?q=" + query, headers=headers) |
33 | | -response = conn.getresponse() |
34 | | -headers = [k + ": " + v for (k, v) in response.getheaders() |
35 | | - if k.startswith("BingAPIs-") or k.startswith("X-MSEdge-")] |
| 32 | +response = requests.get(host + path, headers=headers, params=params) |
| 33 | +response.raise_for_status() |
36 | 34 |
|
37 | 35 | # Print results |
38 | | -print("\nRelevant HTTP Headers:\n") |
39 | | -print("\n".join(headers)) |
| 36 | +print("\nHeaders:\n") |
| 37 | +print(response.headers) |
| 38 | + |
40 | 39 | print("\nJSON Response:\n") |
41 | | -print(json.dumps(json.loads(response.read()), indent=4)) |
| 40 | +pprint(response.json()) |
0 commit comments