Spread the love
In the Search category you can also find how to search Videos. In this tutorial we are going to see how to use the Bing Video Search API to search videos with the term Cognitive Services.
Prerequisites
To use the Bing Video Search API you need to create a Cognitive Services API account with access to the Bing Search APIs. If you don’t have an Azure subscription, you can create a free account.
Make a request to the Bing Video Search API
The Bing Video Search API returns video results from the Bing search engine.
Create a new Console solution in Visual Studio 2017
Add the code below to the Program.cs file
using System; using System.Text; using System.Net; using System.IO; using System.Collections.Generic; namespace BingVideoSearchAPI { class Program { // Replace the accessKey string value with your valid access key. const string accessKey = "<accessKey>"; // Verify the endpoint URI. At this writing, only one endpoint is used for Bing // search APIs. In the future, regional endpoints may be available. If you // encounter unexpected authorization errors, double-check this value against // the endpoint for your Bing search instance in your Azure dashboard. const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/videos/search"; const string searchTerm = "cognitive services"; // Used to return video search results including relevant headers struct SearchResult { public String jsonResult; public Dictionary<String, String> relevantHeaders; } static void Main() { Console.OutputEncoding = System.Text.Encoding.UTF8; Console.WriteLine("Searching videos for: " + searchTerm); SearchResult result = BingVideoSearch(searchTerm); Console.WriteLine("\nRelevant HTTP Headers:\n"); foreach (var header in result.relevantHeaders) Console.WriteLine(header.Key + ": " + header.Value); Console.WriteLine("\nJSON Response:\n"); Console.WriteLine(result.jsonResult); Console.Write("\nPress Enter to exit "); Console.ReadLine(); } /// <summary> /// Performs a Bing video search and return the results as a SearchResult. /// </summary> static SearchResult BingVideoSearch(string searchQuery) { // Construct the URI of the search request var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery); // Perform the Web request and get the response WebRequest request = HttpWebRequest.Create(uriQuery); request.Headers["Ocp-Apim-Subscription-Key"] = accessKey; HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result; string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); // Create result object for return var searchResult = new SearchResult(); searchResult.jsonResult = json; searchResult.relevantHeaders = new Dictionary<String, String>(); // Extract Bing HTTP headers foreach (String header in response.Headers) { if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-")) searchResult.relevantHeaders[header] = response.Headers[header]; } return searchResult; } } }
Replace the accessKey value with an access key valid for your subscription in the code above.
Run the program.
You should get the following response or similar:
[ { "description": "Learn how to easily add powerful artificial intelligence capabilities to your applications with Microsoft Cognitive Services. See Cognitive Services for more information: http://www.microsoft.com/cognitive", "datePublished": "2016-11-18T23:33:14", "publisher": [ { "name": "YouTube" } ], "creator": { "name": "Microsoft Visual Studio" }, "contentUrl": "https://www.youtube.com/watch?v=f4XBxNuEifQ", "hostPageUrl": "https://www.youtube.com/watch?v=f4XBxNuEifQ", "encodingFormat": "h264", "hostPageDisplayUrl": "https://www.youtube.com/watch?v=f4XBxNuEifQ", "width": 1280, "height": 720, "duration": "PT11M43S", "embedHtml": "<iframe width=\"1280\" height=\"720\" src=\"https://www.youtube.com/embed/f4XBxNuEifQ?autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>", "allowHttpsEmbed": true, "viewCount": 32755, "thumbnail": { "width": 300, "height": 225 }, "videoId": "AEC9755C7213A0B3B500AEC9755C7213A0B3B500", "allowMobileEmbed": true, "isSuperfresh": false, "name": "Get started with Microsoft Cognitive Services", "thumbnailUrl": "https://tse2.mm.bing.net/th?id=OVP.kbyaj9L9pdH5n9g_0-7keAEsDh&pid=Api", "webSearchUrl": "https://www.bing.com/videos/search?q=cognitive%20services&view=detail&mid=AEC9755C7213A0B3B500AEC9755C7213A0B3B500", "motionThumbnailUrl": "https://tse3.mm.bing.net/th?id=OM.ALWzoBNyXHXJrg_1535193728&pid=Api" }, { "description": "Watch how Microsoft uses Azure Stack, Azure Function, Cognitive services and cameras to digitize the environment for security purposes (and more).", "datePublished": "2017-05-10T19:00:30", "publisher": [ { "name": "YouTube" } ], "creator": { "name": "ExpovistaTV" }, "contentUrl": "https://www.youtube.com/watch?v=O1pDOkzsFOU", "hostPageUrl": "https://www.youtube.com/watch?v=O1pDOkzsFOU", "encodingFormat": "h264", "hostPageDisplayUrl": "https://www.youtube.com/watch?v=O1pDOkzsFOU", "width": 480, "height": 360, "duration": "PT5M29S", "embedHtml": "<iframe width=\"1280\" height=\"720\" src=\"https://www.youtube.com/embed/O1pDOkzsFOU?autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>", "allowHttpsEmbed": true, "viewCount": 13561, "thumbnail": { "width": 300, "height": 225 }, "videoId": "AAFA95E89ADCC1739E0CAAFA95E89ADCC1739E0C", "allowMobileEmbed": true, "isSuperfresh": false, "name": "Microsoft Cognitive Services and Cameras: No Place to Hyde.", "thumbnailUrl": "https://tse4.mm.bing.net/th?id=OVP.4B13vPDpl5B85RXqRh7ApQEsDh&pid=Api", "webSearchUrl": "https://www.bing.com/videos/search?q=cognitive%20services&view=detail&mid=AAFA95E89ADCC1739E0CAAFA95E89ADCC1739E0C", "motionThumbnailUrl": "https://tse3.mm.bing.net/th?id=OM1.DJ5zwdya6JX6qg_1530168404&pid=Api" } ... ... ]