Bing Image search using #Cognitive Search
The Bing Image search based on the DataMarket service by Microsoft is closing down, and it is moving to cognitive services. This means that if you have code that uses an endpoint like this;
https://api.datamarket.azure.com/Bing/Search/v1/Image …
It needs to be changed to code like this:
https://api.cognitive.microsoft.com/bing/v5.0/images/search …
And your quote is reduced to 1,000 per month… 😦
Here’s some code I’ve written in C# to do a basic image search
public static List<String> GetImages(string searchText)
{
var strUrl = “https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=” + searchText + “&count=1000”;
var wc = new WebClient();
wc.Headers[“Ocp-Apim-Subscription-Key”] = “xxxxxxxxxxx”;
var strJson = wc.DownloadString(strUrl);
var bing = JavascriptDeserialize<BingSearchObject>(strJson);
var lResults = new List<string>();
foreach (var result in bing.value)
{
var qs = HttpUtility.ParseQueryString(result.contentUrl);
lResults.Add(qs[“r”]);}
return lResults;
}
Where “BingSearchObject” is defined as:
#region BingSearchResults
public class Instrumentation
{
public string pageLoadPingUrl { get; set; }
}public class Thumbnail
{
public int width { get; set; }
public int height { get; set; }
}public class InsightsSourcesSummary
{
public int shoppingSourcesCount { get; set; }
public int recipeSourcesCount { get; set; }
}public class Value
{
public string name { get; set; }
public string webSearchUrl { get; set; }
public string thumbnailUrl { get; set; }
public string datePublished { get; set; }
public string contentUrl { get; set; }
public string hostPageUrl { get; set; }
public string contentSize { get; set; }
public string encodingFormat { get; set; }
public string hostPageDisplayUrl { get; set; }
public int width { get; set; }
public int height { get; set; }
public Thumbnail thumbnail { get; set; }
public string imageInsightsToken { get; set; }
public InsightsSourcesSummary insightsSourcesSummary { get; set; }
public string imageId { get; set; }
public string accentColor { get; set; }
}public class Thumbnail2
{
public string thumbnailUrl { get; set; }
}public class QueryExpansion
{
public string text { get; set; }
public string displayText { get; set; }
public string webSearchUrl { get; set; }
public string searchLink { get; set; }
public Thumbnail2 thumbnail { get; set; }
}public class Thumbnail3
{
public string thumbnailUrl { get; set; }
}public class Suggestion
{
public string text { get; set; }
public string displayText { get; set; }
public string webSearchUrl { get; set; }
public string searchLink { get; set; }
public Thumbnail3 thumbnail { get; set; }
}public class PivotSuggestion
{
public string pivot { get; set; }
public List<Suggestion> suggestions { get; set; }
}public class Thumbnail4
{
public string url { get; set; }
}public class SimilarTerm
{
public string text { get; set; }
public string displayText { get; set; }
public string webSearchUrl { get; set; }
public Thumbnail4 thumbnail { get; set; }
}public class BingSearchObject
{
public string _type { get; set; }
public Instrumentation instrumentation { get; set; }
public string readLink { get; set; }
public string webSearchUrl { get; set; }
public int totalEstimatedMatches { get; set; }
public List<Value> value { get; set; }
public List<QueryExpansion> queryExpansions { get; set; }
public int nextOffsetAddCount { get; set; }
public List<PivotSuggestion> pivotSuggestions { get; set; }
public bool displayShoppingSourcesBadges { get; set; }
public bool displayRecipeSourcesBadges { get; set; }
public List<SimilarTerm> similarTerms { get; set; }
}
#endregion
-
April 24, 2017 at 11:15 am#Bing #Image search from C# or #PHP | Network Programming in .NET
-
April 24, 2017 at 11:15 amBing Search API V2.0 | Network Programming in .NET