-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathBingImageSearchv7.rb
More file actions
44 lines (31 loc) · 1.39 KB
/
BingImageSearchv7.rb
File metadata and controls
44 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'net/https'
require 'uri'
require 'json'
# **********************************************
# *** Update or verify the following values. ***
# **********************************************
# Replace the accessKey string value with your valid access key.
accessKey = "enter your key here"
# 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.
uri = URI("https://api.cognitive.microsoft.com")
path = "/bing/v7.0/images/search"
term = "tropical ocean"
if accessKey.length != 32 then
puts "Invalid Bing Search API subscription key!"
puts "Please paste yours into the source code."
abort
end
uri = URI(uri + path + "?q=" + URI::Escape.escape(term))
puts "Searching images for: " + term
request = Net::HTTP::Get.new(uri)
request['Ocp-Apim-Subscription-Key'] = accessKey
response = Net::HTTP.get_response(uri)
puts "\nJSON Response:\n\n"
parsed_json = JSON.parse(response.body)
total_returned_images = parsed_json["totalEstimatedMatches"]
first_result = parsed_json["value"][0]["thumbnailUrl"]
puts "total number of returned matches: #{total_returned_images}"
puts "Url to the thumnail of the first returned search result: #{first_result}"