Skip to content

Commit ab539aa

Browse files
authored
Merge pull request #6 from aahill/image-search-ruby-add
adding Bing Image Search ruby and c# quickstart samples
2 parents e747460 + 1ea8d31 commit ab539aa

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//Copyright (c) Microsoft Corporation. All rights reserved.
2+
//Licensed under the MIT License.
3+
4+
using System;
5+
using System.Net;
6+
using System.IO;
7+
using System.Collections.Generic;
8+
using Newtonsoft.Json.Linq;
9+
10+
namespace BingSearchApisQuickstart
11+
{
12+
13+
class Program
14+
{
15+
16+
// Replace this string value with your valid access key.
17+
const string subscriptionKey = "Enter your subscription key here";
18+
19+
// Verify the endpoint URI. If you encounter unexpected authorization errors,
20+
// double-check this value against the Bing search endpoint in your Azure dashboard.
21+
const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/images/search";
22+
23+
const string searchTerm = "tropical ocean";
24+
25+
// A struct to return image search results seperately from headers
26+
struct SearchResult
27+
{
28+
public string jsonResult;
29+
public Dictionary<String, String> relevantHeaders;
30+
}
31+
32+
static void Main()
33+
{
34+
Console.OutputEncoding = System.Text.Encoding.UTF8;
35+
36+
Console.WriteLine("Searching images for: " + searchTerm + "\n");
37+
//send a search request using the search term
38+
SearchResult result = BingImageSearch(searchTerm);
39+
//deserialize the JSON response from the Bing Image Search API
40+
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result.jsonResult);
41+
42+
var firstJsonObj = jsonObj["value"][0];
43+
Console.WriteLine("Title for the first image result: " + firstJsonObj["name"]+"\n");
44+
//After running the application, copy the output URL into a browser to see the image.
45+
Console.WriteLine("URL for the first image result: " + firstJsonObj["webSearchUrl"]+"\n");
46+
47+
Console.Write("\nPress Enter to exit ");
48+
Console.ReadLine();
49+
}
50+
51+
/// <summary>
52+
/// Performs a Bing Image search and return the results as a SearchResult.
53+
/// </summary>
54+
static SearchResult BingImageSearch(string searchQuery)
55+
{
56+
// Construct the URI of the search request
57+
var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(searchQuery);
58+
59+
// Perform the Web request and get the response
60+
WebRequest request = WebRequest.Create(uriQuery);
61+
request.Headers["Ocp-Apim-Subscription-Key"] = subscriptionKey;
62+
HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
63+
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
64+
65+
// Create result object for return
66+
var searchResult = new SearchResult()
67+
{
68+
jsonResult = json,
69+
relevantHeaders = new Dictionary<String, String>()
70+
};
71+
72+
// Extract Bing HTTP headers
73+
foreach (String header in response.Headers)
74+
{
75+
if (header.StartsWith("BingAPIs-") || header.StartsWith("X-MSEdge-"))
76+
searchResult.relevantHeaders[header] = response.Headers[header];
77+
}
78+
79+
return searchResult;
80+
}
81+
82+
}
83+
}

ruby/Search/BingImageSearchv7.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
ruby
2+
require 'net/https'
3+
require 'uri'
4+
require 'json'
5+
6+
# **********************************************
7+
# *** Update or verify the following values. ***
8+
# **********************************************
9+
10+
# Replace the accessKey string value with your valid access key.
11+
accessKey = "enter key here"
12+
13+
# Verify the endpoint URI. At this writing, only one endpoint is used for Bing
14+
# search APIs. In the future, regional endpoints may be available. If you
15+
# encounter unexpected authorization errors, double-check this value against
16+
# the endpoint for your Bing Search instance in your Azure dashboard.
17+
18+
uri = "https://api.cognitive.microsoft.com"
19+
path = "/bing/v7.0/images/search"
20+
21+
term = "puppies"
22+
23+
if accessKey.length != 32 then
24+
puts "Invalid Bing Search API subscription key!"
25+
puts "Please paste yours into the source code."
26+
abort
27+
end
28+
29+
uri = URI(uri + path + "?q=" + URI.escape(term))
30+
31+
puts "Searching images for: " + term
32+
33+
request = Net::HTTP::Get.new(uri)
34+
request['Ocp-Apim-Subscription-Key'] = accessKey
35+
36+
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
37+
http.request(request)
38+
end
39+
40+
puts "\nRelevant Headers:\n\n"
41+
response.each_header do |key, value|
42+
# header names are coerced to lowercase
43+
if key.start_with?("bingapis-") or key.start_with?("x-msedge-") then
44+
puts key + ": " + value
45+
end
46+
end
47+
48+
puts "\nJSON Response:\n\n"
49+
puts JSON::pretty_generate(JSON(response.body))

0 commit comments

Comments
 (0)