|
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 | +using Newtonsoft.Json; |
4 | 5 | using System; |
5 | 6 | using System.Text; |
6 | 7 | using System.Net; |
7 | 8 | using System.IO; |
8 | | -using System.Collections.Generic; |
9 | 9 |
|
10 | | -namespace BingVideoSearchCSharpCore |
| 10 | +namespace BingVideoSearch |
11 | 11 | { |
12 | 12 |
|
13 | 13 | class Program |
14 | 14 | { |
15 | | - // ********************************************** |
16 | | - // *** Update or verify the following values. *** |
17 | | - // ********************************************** |
| 15 | + // Add your Azure Bing Search v7 key and endpoint to your environment variables. |
| 16 | + static string subscriptionKey = Environment.GetEnvironmentVariable("BING_SEARCH_V7_SUBSCRIPTION_KEY"); |
| 17 | + static string endpoint = Environment.GetEnvironmentVariable("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/videos/search"; |
18 | 18 |
|
19 | | - // Replace the accessKey string value with your valid access key. |
20 | | - const string accessKey = Environment.GetEnvironmentVariable("BING_SEARCH_V7_SUBSCRIPTION_KEY"); |
21 | | - |
22 | | - // Verify the endpoint URI. At this writing, only one endpoint is used for Bing |
23 | | - // search APIs. In the future, regional endpoints may be available. If you |
24 | | - // encounter unexpected authorization errors, double-check this value against |
25 | | - // the endpoint for your Bing search instance in your Azure dashboard. |
26 | | - const string uriBase = Environment.GetEnvironmentVariable("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/videos/search"; |
27 | | - |
28 | | - const string searchTerm = "kittens"; |
29 | | - |
30 | | - // Used to return video search results including relevant headers |
31 | | - struct SearchResult |
32 | | - { |
33 | | - public String jsonResult; |
34 | | - public Dictionary<String, String> relevantHeaders; |
35 | | - } |
| 19 | + const string query = "kittens"; |
36 | 20 |
|
37 | 21 | static void Main() |
38 | 22 | { |
39 | | - Console.OutputEncoding = System.Text.Encoding.UTF8; |
40 | | - Console.WriteLine("Searching videos for: " + searchTerm); |
41 | | - |
42 | | - SearchResult result = BingVideoSearch(searchTerm); |
| 23 | + Console.OutputEncoding = Encoding.UTF8; |
| 24 | + Console.WriteLine("Searching videos for: " + query); |
43 | 25 |
|
44 | | - Console.WriteLine("\nRelevant HTTP Headers:\n"); |
45 | | - foreach (var header in result.relevantHeaders) |
46 | | - Console.WriteLine(header.Key + ": " + header.Value); |
47 | | - |
48 | | - Console.WriteLine("\nJSON Response:\n"); |
49 | | - Console.WriteLine(JsonPrettyPrint(result.jsonResult)); |
50 | | - |
51 | | - Console.Write("\nPress Enter to exit "); |
52 | | - Console.ReadLine(); |
53 | | - } |
54 | | - |
55 | | - /// <summary> |
56 | | - /// Performs a Bing video search and return the results as a SearchResult. |
57 | | - /// </summary> |
58 | | - static SearchResult BingVideoSearch(string searchQuery) |
59 | | - { |
60 | 26 | // Construct the URI of the search request |
61 | | - var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery); |
| 27 | + var uriQuery = endpoint + "?q=" + Uri.EscapeDataString(query); |
62 | 28 |
|
63 | 29 | // Perform the Web request and get the response |
64 | 30 | WebRequest request = HttpWebRequest.Create(uriQuery); |
65 | | - request.Headers["Ocp-Apim-Subscription-Key"] = accessKey; |
| 31 | + request.Headers["Ocp-Apim-Subscription-Key"] = subscriptionKey; |
66 | 32 | HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result; |
67 | 33 | string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); |
68 | 34 |
|
69 | | - // Create result object for return |
70 | | - var searchResult = new SearchResult(); |
71 | | - searchResult.jsonResult = json; |
72 | | - searchResult.relevantHeaders = new Dictionary<String, String>(); |
73 | | - |
74 | | - // Extract Bing HTTP headers |
75 | | - foreach (String header in response.Headers) |
76 | | - { |
77 | | - if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-")) |
78 | | - searchResult.relevantHeaders[header] = response.Headers[header]; |
79 | | - } |
80 | | - |
81 | | - return searchResult; |
82 | | - } |
83 | | - |
84 | | - /// <summary> |
85 | | - /// Formats the given JSON string by adding line breaks and indents. |
86 | | - /// </summary> |
87 | | - /// <param name="json">The raw JSON string to format.</param> |
88 | | - /// <returns>The formatted JSON string.</returns> |
89 | | - static string JsonPrettyPrint(string json) |
90 | | - { |
91 | | - if (string.IsNullOrEmpty(json)) |
92 | | - return string.Empty; |
93 | | - |
94 | | - json = json.Replace(Environment.NewLine, "").Replace("\t", ""); |
95 | | - |
96 | | - StringBuilder sb = new StringBuilder(); |
97 | | - bool quote = false; |
98 | | - bool ignore = false; |
99 | | - int offset = 0; |
100 | | - int indentLength = 3; |
101 | | - |
102 | | - foreach (char ch in json) |
103 | | - { |
104 | | - switch (ch) |
105 | | - { |
106 | | - case '"': |
107 | | - if (!ignore) quote = !quote; |
108 | | - break; |
109 | | - case '\'': |
110 | | - if (quote) ignore = !ignore; |
111 | | - break; |
112 | | - } |
113 | | - |
114 | | - if (quote) |
115 | | - sb.Append(ch); |
116 | | - else |
117 | | - { |
118 | | - switch (ch) |
119 | | - { |
120 | | - case '{': |
121 | | - case '[': |
122 | | - sb.Append(ch); |
123 | | - sb.Append(Environment.NewLine); |
124 | | - sb.Append(new string(' ', ++offset * indentLength)); |
125 | | - break; |
126 | | - case '}': |
127 | | - case ']': |
128 | | - sb.Append(Environment.NewLine); |
129 | | - sb.Append(new string(' ', --offset * indentLength)); |
130 | | - sb.Append(ch); |
131 | | - break; |
132 | | - case ',': |
133 | | - sb.Append(ch); |
134 | | - sb.Append(Environment.NewLine); |
135 | | - sb.Append(new string(' ', offset * indentLength)); |
136 | | - break; |
137 | | - case ':': |
138 | | - sb.Append(ch); |
139 | | - sb.Append(' '); |
140 | | - break; |
141 | | - default: |
142 | | - if (ch != ' ') sb.Append(ch); |
143 | | - break; |
144 | | - } |
145 | | - } |
146 | | - } |
147 | | - |
148 | | - return sb.ToString().Trim(); |
| 35 | + Console.WriteLine("\nJSON Response:\n"); |
| 36 | + dynamic parsedJson = JsonConvert.DeserializeObject(json); |
| 37 | + Console.WriteLine(JsonConvert.SerializeObject(parsedJson, Formatting.Indented)); |
149 | 38 | } |
150 | 39 | } |
151 | 40 | } |
0 commit comments