File tree Expand file tree Collapse file tree 1 file changed +9
-12
lines changed
Expand file tree Collapse file tree 1 file changed +9
-12
lines changed Original file line number Diff line number Diff line change 1- ruby
21require 'net/https'
32require 'uri'
43require 'json'
87# **********************************************
98
109# Replace the accessKey string value with your valid access key.
11- accessKey = "enter key here "
10+ accessKey = "58620d3dcee1477ca2fbd886e23e545b "
1211
1312# Verify the endpoint URI. At this writing, only one endpoint is used for Bing
1413# search APIs. In the future, regional endpoints may be available. If you
1817uri = "https://api.cognitive.microsoft.com"
1918path = "/bing/v7.0/images/search"
2019
21- term = "puppies "
20+ term = "tropical ocean "
2221
2322if accessKey . length != 32 then
2423 puts "Invalid Bing Search API subscription key!"
3736 http . request ( request )
3837end
3938
40- puts "\n Relevant 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-
4839puts "\n JSON Response:\n \n "
49- puts JSON ::pretty_generate ( JSON ( response . body ) )
40+
41+ parsed_json = JSON . parse ( response . body )
42+ total_returned_images = parsed_json [ "totalEstimatedMatches" ]
43+ first_result = parsed_json [ "value" ] [ 0 ] [ "thumbnailUrl" ]
44+
45+ puts "total number of returned matches: #{ total_returned_images } "
46+ puts "Url to the thumnail of the first returned search result: #{ first_result } "
You can’t perform that action at this time.
0 commit comments