当前位置:首页 > 未分类 > 正文内容

Search Books with Goodreads API and Google Apps Script

ceacer3周前 (05-02)未分类3389

The Goodreads API helps you query the entire database of books on the Goodreads website. You can find the ratings of books, fetch book reviews, search books by author or even publish your own reviews. This example shows how to connect to the GoodReads website through Google Apps Script, find books by title, parse the XML results as JSON and write the results in a Google Spreadsheet.

You can also extend the code to insert the thumbnail of the book image in a spreadsheet cell using the IMAGE function.

To get started, go to the Goodreads.com account and create a key. All Rest API methods will require you to register for a developer key.

Goodreads API

Goodreads will return the response in XML format (see below) and we can use the XML service of Google Apps Script to easily parse this XML response.

GoodReads XML

Here’s the complete example. Remember to replace the API key with your own.

function GoodReads() {
  var search = 'Taj Mahal';
  var books = searchBooks_(search);

  // Write Data to Google Spreadsheet.
  var sheet = SpreadsheetApp.getActiveSheet();
  books.forEach(function (book) {
    sheet.appendRow([book.title, book.author, book.rating, book.url]);
  });
}

function searchBooks_(query) {
  var baseUrl = 'https://www.goodreads.com/book/show/',
    apiUrl = 'https://www.goodreads.com/search/index.xml',
    apiKey = 'ctrlq.org',
    searchResults = [],
    payload = {
      q: query,
      key: apiKey
    },
    params = {
      method: 'GET',
      payload: payload,
      muteHttpExceptions: true
    };

  var response = UrlFetchApp.fetch(apiUrl, params);

  // API Connection Successful
  if (response.getResponseCode() === 200) {
    // Parse XML Response
    var xml = XmlService.parse(response.getContentText());
    var results = xml.getRootElement().getChildren('search')[0];

    // Save the result in JSON format
    results
      .getChild('results')
      .getChildren()
      .forEach(function (result) {
        result.getChildren('best_book').forEach(function (book) {
          searchResults.push({
            title: book.getChild('title').getText(),
            author: book.getChild('author').getChild('name').getText(),
            thumbnail: book.getChild('image_url').getText(),
            rating: result.getChild('average_rating').getText(),
            url: baseUrl + result.getChild('id').getText()
          });
        });
      });
  }

  return searchResults;
}

相关文章

How to Add Password to PDF Documents in Google Drive

How to Add Password to PDF Documents in Google Drive

The Document Studio add-on helps you create personalized PDF documents from Google Sheets. You can g...

Monitor Your Stock Portfolio with Google Sheets and Receive Daily Email Reports

Monitor Your Stock Portfolio with Google Sheets and Receive Daily Email Reports

I have a Stock tracker spreadsheet built inside Google Sheets that keeps track of my fictional stock...

How to Mail Merge with Outlook and Google Sheets

How to Mail Merge with Outlook and Google Sheets

The Mail merge add-on for Gmail lets you send personalized emails to your contacts in bulk. You can...

How to Transcribe Audio and Video Attachments in Gmail

How to Transcribe Audio and Video Attachments in Gmail

The Save Gmail to Google Drive add-on lets you automatically download email messages and file attach...

How to Make Phone Numbers Callable in Google Sheets and Docs

How to Make Phone Numbers Callable in Google Sheets and Docs

This tutorial explains how to make phone numbers clickable within Google Sheets, Slides and Google D...

How Teachers can Email Parents of Students from Google Forms

How Teachers can Email Parents of Students from Google Forms

A school provides email accounts for students that are enrolled in high school. The school has publ...