Bing Serach Api

With the Bing Serach Api, one can query the bing search engine and get the results. It can be useful for creating mashups :)

Why i did not used google api ? It is deprecated.

Bing gives 5000 queries for the month free :) That is another good reason.

The query can be web, image, news related. Enough of talk, how do we do it ?

You can see the Search API information at http://www.bing.com/dev/en-us/dev-center
  1. Get a microsoft id with any email-id like gmail by registering.
  2. Once you are registered, go to https://datamarket.azure.com/account/keys                                  you can see the key. copy it. This is the < KEY > used for authentication in each of your query.
  3. convert this string to base64 encoding -
    1. ":" + < KEY > like Example : my key is 8xgXXXXXXXAXXXXXXXXXXXQUnJdOHgXXXXXXX
    2. add colon in start of key to get 
      1.  :8xgXXXXXXXAXXXXXXXXXXXQUnJdOHgXXXXXXX
    3. get the base64 encoding of above string.  It is Base64Key.
      1. IDo4eGdYWFhYWFhYQVhYWFhYWFhYWFhYUVVuSmRPSGdYWFhYWFhY
      2. you can use http://www.base64encode.org for this conversion.
so now you need to just send a ajax call with the this Base64Key in the headers to bing url
         "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/"

NOTE: Header would have key-value as "Authorization" and "Basic"+ < Base64Key > respectively.

The following javascript code just does that :

var accountKeyEncoded = BASE64KEY;

jQuery.support.cors = true; 
// Authorization header of the ajax request. 
function setHeader(xhr) { 
  xhr.setRequestHeader('Authorization', "Basic " + accountKeyEncoded); 
} 

function GetBingResult() { 
  var kindOfSearch = "Image" ;  // this can be news or image also. 
  var searchWord = "Bing"; // search about anything 
  var formatOfResponse = "json" ; // this can be xml too. 

  //Build up the URL for the request // get the top 50 results. 
  var requestStr = "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/" + 
              kindOfSearch + "?Query=%27" + searchWord + " %27&$top=50&$format=" + formatOfResponse; 

  $.ajax({ url: requestStr, 
      // this would set the authorization header 
      beforeSend: setHeader, 
      context: this, 
      type: 'GET', 
      success: function (data, status) { 
        var results = data; 
        var imgSrc = data.d.results[0].MediaUrl; 
        console.log(imgSrc); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
        console.log(textStatus); 
      } 
    }); 
} 

GetBingResult(); 

Clojure code :)


(ns bingsearchapi.core
  (:require [clj-http.client :as client])
  (:require [clojure.data.json :as json])
)

(def mainUrl  "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image")

(def queryStart   "?Query=%27")
(def queryEnd     "%27")
(def formatOfResponse "json")
(def topWords         "&$top=2")

(def base64EncodedKey <YOUR BASE 64 ENCODED KEY> )

(defn makeQuery [searchWord]
  (clojure.string/join "" [mainUrl queryStart searchWord queryEnd]))

(defn addTop [query]
  (clojure.string/join "" [query topWords]))

(defn addFormat [query]
  (clojure.string/join "" [query "&$format=" formatOfResponse]))
  
(defn getResult [searchWord]
  (-> searchWord 
       makeQuery
       addTop
       addFormat
       
       (client/get {:headers 
                    {"Authorization" 
                     (clojure.string/join "" [ "Basic " 
                                        base64EncodedKey ])}})))


(def result (getResult "ashish negi"))

(def bodyResult (json/read-str (:body result)))

;// print the first image url 
(-> bodyResult (get "d") 
   (get "results")
   (get 0)
   (get "MediaUrl"))
 
The output on the console would be the url of first image.
You can visit the url to see if it corresponds to your own search query.

happy searching :)

Comments