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