Google Image Search No Longer Available
About 2 days ago, Google Image Search API was terminated, and attempts to call the API gives an error “No Longer Available”. There is a work around, using the Google Custom Search API, and here’s some code to do it using JQuery.
(You’ll need to get your own key, since it’s limited to 100 queries per day)
function GoogleImageSearch(searchTerm, callback)
{
var params = {};
params.q = searchTerm; // search text
params.num = 1; // integer value range between 1 to 10 including
params.start = 1; // integer value range between 1 to 101, it is like the offset
params.imgSize = “large”; // for image size
params.searchType = “image”; // compulsory
params.fileType = “jpg”; // you can leave these if extension does not matters you
params.key = “xxxxxxx”; // API_KEY got from https://console.developers.google.com/
params.cx = “xxxxxx”; // cx value is the custom search engine value got from https://cse.google.com/cse(if not created then create it).
$.get(“https://www.googleapis.com/customsearch/v1”,params,function(img){
console.log(img);
callback(img.items[0].link);
}).fail(function(y){
console.log(y);
});
}
Sometimes google can return a broken url in img.items[0].link, so, for better results, you can get 10 images back from Google, and check them until you get one that loads.
set params.num = 10
then set
GoogleImageData = img.items;
ReturnFirstWorkingImage(callback);
in the callback
function ReturnFirstWorkingImage(callback)
{
var img1 = new Image();
img1.onload = callback(GoogleImageData[0].link);
img1.onerror = function(){
GoogleImageData.shift();
ReturnFirstWorkingImage(callback);
};
img1.src = GoogleImageData[0].link
}
LikeLike