Skip to content

Instantly share code, notes, and snippets.

@jrmeyerhofer
Last active November 3, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmeyerhofer/19efc19d1c515be6791527662b57c2e4 to your computer and use it in GitHub Desktop.
Save jrmeyerhofer/19efc19d1c515be6791527662b57c2e4 to your computer and use it in GitHub Desktop.
function callImageSearch() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var start = new Date();
//just hard code to a really high number and break out when done!
for (var i = 1; i < 500; i++) {
var OCLCNumber = sheet.getRange(i,1).getValue();
Logger.log('OCLCNumber: ' + OCLCNumber);
// if the value is blank, no more OCLC Numbers! Break out!
if (OCLCNumber == "") { break; }
//check for timeout
if (isTimeUp_(start)) { break; }
//if cell two is not blank means it's already been run on that row. skip row!
var beecell = sheet.getRange(i,2).getValue();
if (!beecell) {
//pause after each script
//what is your Worldcat URL? For example: https://instiution.on.worldcat.org/oclc/
var OCLCURL = 'WORLDCAT URL HERE!' + OCLCNumber;
var html = UrlFetchApp.fetch(OCLCURL).getContentText();
if (html) {
if (html.indexOf('ng-src') >= 0) {
// Present
var locURL = html.indexOf('ng-src');
var locSpace = html.indexOf(' ',locURL);
var localURL = html.substring(locURL+8,locSpace-1)
localURL = "https:" + localURL;
} else {
var localURL = "No Image";
}
}
Logger.log('locURL: ' +locURL);
Logger.log('locSpace: ' +locSpace);
Logger.log('localURL: ' +localURL);
var cell = sheet.getRange("B"+i);
cell.setValue(localURL);
var image = '=image(B' + i + ',4,90,70)';
var imagecell = sheet.getRange("C"+i);
imagecell.setValue(image);
var oclclinkcell = sheet.getRange("D"+i);
oclclinkcell.setValue(OCLCURL);
var googlebookcover = getGoogleBooksImageOCLC(OCLCNumber);
var googlecell = sheet.getRange("E"+i);
googlecell.setValue(googlebookcover);
Logger.log('googlebookcover: ' +googlebookcover);
var image3 = '=image(E' + i + ',4,90,70)';
var imagecell3 = sheet.getRange("F"+i);
imagecell3.setValue(image3);
//clear the variables
locURL = "";
locSpace = "";
localURL = "";
// Sets the row to a height of 100 pixels so we can see the image
sheet.setRowHeight(i, 100);
} //end if beecell
}
}
//Run once to create the menu to run from the sheet!
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Get Image URLs from OCLC Numbers in col A','callImageSearch')
.addToUi();
}
// GOOGLE BOOKS START **********************************************************************
// Documentation - https://developers.google.com/books/docs/v1/getting_started?hl=en
// Also see the dev console for setting up API Keys - https://console.developers.google.com
// More:
// https://developers.google.com/books/docs/static-links
// https://developers.google.com/books/docs/v1/using
// info about billing: https://support.google.com/cloud/answer/6158867?hl=en&ref_topic=6262490
// info about creating API's - https://console.developers.google.com/home/dashboard
function getGoogleBooksImageOCLC(OCLC)
{
var GoogleBooks_API_KEY = 'YOU WILL NEED AN API KEY';
//example of what the URL of a call would look like. Could put this into a browser window and see the JSON:
//https://www.googleapis.com/books/v1/volumes?q=oclc:886672369&key=YOURKEYHERE
var bookJSON = 'https://www.googleapis.com/books/v1/volumes?q=oclc:' + OCLC + '&key=' + GoogleBooks_API_KEY;
Logger.log('bookJSON: ' +bookJSON);
// Make request to API and get response before this point.
var json = UrlFetchApp.fetch(bookJSON);
Logger.log('json: ' +json);
var response = json.getContentText();
Logger.log('response: ' +response);
var data = JSON.parse(response);
Logger.log('data: ' +data);
//Browser.msgBox(data.totalItems);
//find out how many books it found
if (!data['totalItems'] || data['totalItems'] == 0) {
Logger.log("No book found");
return "";
}
//get the cover image URL from the JSON
//TypeError: Cannot read property "smallThumbnail" from undefined.
var imageURL = "";
if ('imageLinks' in data['items'][0]['volumeInfo']) {
Logger.log('in if 1 ');
if ('smallThumbnail' in data['items'][0]['volumeInfo']['imageLinks']) {
Logger.log('in if 2 ');
imageURL = data['items'][0]['volumeInfo']['imageLinks']['smallThumbnail'];
}
}
return imageURL;
}
function isTimeUp_(start) {
var now = new Date();
var isItTime = now.getTime() - start.getTime() > 300000; // 5 minutes
if (isItTime) {
Browser.msgBox("Timeout occured. More to process? Just run again!");
}
return isItTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment