1

My goal is to have a Google docs spreadsheet of keywords that I can loop through, perform an image search for each keyword, and save the resulting image URL's in the same spreadsheet.

There's a whole Google image search javascript API found here. Within Google Sheets the custom apps script also uses javascript. I'm looking for a way to access this image search API using apps script for a google doc but can't seem to find a way to import the library.

Update: non-deprecated google search API link: here

jchmilew
  • 198
  • 2
  • 10

2 Answers2

3

Taking the advice from above I abandoned trying to incorporate the google search API directly and instead used the URL approach. Here's what I ended up with in case anyone wants to do this in the future.

var delayMilli = 0;

function fetchURL(sheet,row,urlCell) {
  delay(delayMilli)
  var row = sheet.getRange(row,1,1,2).getValues()[0];
  // This example searches for two terms located in row number "row"
  var response = UrlFetchApp.fetch("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="+row[1]+"%20"+row[0]);
  var data = JSON.parse(response);
  if (data.responseData.results[0]) {
    var urlString = data.responseData.results[0].url;
    urlCell.setValue(urlString);
    if (delayMilli >= 0) {
      delayMilli -= 5; 
    }
  } else {
    // Delay the method and try again.
    delayMilli += 100;
    fetchURL(sheet,row,urlCell)
  } 
}

If the response object is empty it's usually because Google has rejected the server call. In the above code, each failed iteration will add 0.1 seconds to the delay time before trying again. Once the calls start going through again without any issues the delay time is slowly reduced back to 0. Seemed to work pretty well for me.

One big drawback is you can't filter your results by image size, restriction level, etc.

jchmilew
  • 198
  • 2
  • 10
2

Unfortunately, Google deprecated the API you linked to. Also, there does not appear to be a similar API available in Google Apps Script. Therefore, you have one choice left, and that is to construct the URL to Google's image search and use the URL fetch app to grab the HTML. From that, you might be able to find links to the images that were returned, but be forewarned, even if you do get it working, Google hates anything that uses its search like a bot and will likely flag the traffic. Sorry if this is not the answer you wanted, but it is the only answer there is.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Joshua Dannemann
  • 2,003
  • 1
  • 14
  • 34
  • Thanks for the input, I was worried that would be the case. I've added a new link to the google custom search API which seems to be the most up to date. – jchmilew Aug 19 '15 at 23:29
  • The unfortunate problem with that API is that it only uses a site-specific search engine that you create using Google tools. You will not be able to use it for accessing information from the standard Google image search. – Joshua Dannemann Aug 19 '15 at 23:33
  • By the way, if you or somebody else cannot find a more ideal solution, then please be so kind as to accept my answer. Sometimes people just won't do that only because it's not the answer they wanted to hear. – Joshua Dannemann Aug 19 '15 at 23:36
  • I have a script that does something similar to what @JoshuaDannemann has described and during development I was temporary blacklisted by Google a few times. Definitely not the way to go unless you know your requests are going to be seldom. The best way to get around this would be to find a search engine with a decent API, though in my quest to find one I've never been impressed. And Joshua's answer should definitely be accepted as correct. – Sabrina Aug 20 '15 at 04:07