Skip to content

Commit f2285ca

Browse files
authored
Updated library
1 parent 640c275 commit f2285ca

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

python/Search/BingVideoSearchv7.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
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
# -*- coding: utf-8 -*-
55

6-
import http.client
76
import json
87
import os
9-
import urllib.parse
8+
from pprint import pprint
9+
import requests
1010

1111
'''
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.
1313
Documentation: https: // docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/
1414
'''
1515

1616
# Add your Bing Search V7 subscription key and endpoint to your environment variables.
1717
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
1818
host = os.environ['BING_SEARCH_V7_ENDPOINT']
19-
host = host.replace('https://', '')
2019
path = "/bing/v7.0/videos/search"
2120

2221
# Search query
23-
term = "kittens"
22+
query = "kittens"
2423

2524
# 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 }
2930

3031
# 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()
3634

3735
# Print results
38-
print("\nRelevant HTTP Headers:\n")
39-
print("\n".join(headers))
36+
print("\nHeaders:\n")
37+
print(response.headers)
38+
4039
print("\nJSON Response:\n")
41-
print(json.dumps(json.loads(response.read()), indent=4))
40+
pprint(response.json())

0 commit comments

Comments
 (0)