Skip to content

Commit 7faf2b0

Browse files
authored
Updated structure
1 parent 1d704f5 commit 7faf2b0

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

python/Search/BingVideoSearchv7.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,39 @@
33

44
# -*- coding: utf-8 -*-
55

6-
import http.client, urllib.parse, json
6+
import http.client
7+
import json
8+
import os
9+
import urllib.parse
710

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+
'''
1115

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.
1317
subscriptionKey = os.environ['BING_SEARCH_V7_SUBSCRIPTION_KEY']
14-
15-
# Add your Bing Search V7 endpoint to your environment variables.
1618
host = os.environ['BING_SEARCH_V7_ENDPOINT']
19+
host = host.replace('https://', '')
1720
path = "/bing/v7.0/videos/search"
1821

22+
# Search query
1923
term = "kittens"
2024

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)
3229

30+
# Call the API
3331
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-")]
3436

35-
headers, result = BingVideoSearch(term)
37+
# Print results
3638
print("\nRelevant HTTP Headers:\n")
3739
print("\n".join(headers))
3840
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

Comments
 (0)