<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tara</title>
    <description>The latest articles on DEV Community by Tara (@tara).</description>
    <link>https://dev.to/tara</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F28798%2F92a3205b-74e7-4e38-8819-0d97195106cd.jpg</url>
      <title>DEV Community: Tara</title>
      <link>https://dev.to/tara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tara"/>
    <language>en</language>
    <item>
      <title>How I used the Goodreads API to pick my next read</title>
      <dc:creator>Tara</dc:creator>
      <pubDate>Mon, 14 Oct 2019 15:32:24 +0000</pubDate>
      <link>https://dev.to/tara/how-i-used-the-goodreads-api-to-pick-my-next-read-2le9</link>
      <guid>https://dev.to/tara/how-i-used-the-goodreads-api-to-pick-my-next-read-2le9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I like to read. A lot. I also (shockingly enough) like to code. So a little while ago when I had the idea to combine these two interests of mine, I hit up the &lt;a href="https://www.goodreads.com/api"&gt;Goodreads API docs&lt;/a&gt; to see what sorts of data I could get my hands on. After perusing the docs (aka doing a lot of command + f to find things because the docs aren't the most user-friendly), I decided to make a small program that would pick out a book for me to read next. &lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Since I had already picked out the language I was going to use, I dove right in and created the project directory on my Desktop and initialized it with Git. &lt;/p&gt;

&lt;p&gt;If you're using this as a tutorial and haven't worked with JavaScript previously, you should download &lt;a href="https://nodejs.org/"&gt;Node.js&lt;/a&gt; and get yourself a package manager. I currently use npm. &lt;/p&gt;

&lt;p&gt;I also knew from the docs that I needed to get myself an API key. As a Goodreads user, all I had to do was login to get them. I believe non-Goodreads users will need to create an account to get access.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Down to Business
&lt;/h2&gt;

&lt;p&gt;For this all I needed was a list of the books I had on my to-read shelf. After the aforementioned "command + f"-ing, I found the GET request to &lt;a href="https://www.goodreads.com/api/index#reviews.list"&gt;"get the books on a members shelf"&lt;/a&gt; which, for no reason other than previous experience with it, led me to want to use the &lt;a href="https://www.npmjs.com/package/request-promise"&gt;request promise&lt;/a&gt; package.&lt;/p&gt;

&lt;p&gt;At this point, I also decided to install &lt;a href="https://www.npmjs.com/package/dotenv"&gt;dotenv&lt;/a&gt; so I could pass in my API key and other info that I didn't want hardcoded.&lt;/p&gt;

&lt;p&gt;I set up my .env file in the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KEY=YOUR_GOODREADS_KEY
SECRET=YOUR_GOODREADS_SECRET
USER_ID=YOUR_GOODREADS_ID
VERSION=2
PER_PAGE=200
SHELF=to-read
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then I created my main.js file. My first objective was to log out what Goodreads returned from my request. Much to my surprise, there was a nice long chunk of XML in my terminal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rCRYm05y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5gn6z4acxi2lt9yc9iw6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rCRYm05y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5gn6z4acxi2lt9yc9iw6.gif" alt='Gif of Lauren Conrad going "whoa!"'&gt;&lt;/a&gt;&lt;/p&gt;
Lauren Conrad saying, "Whoa."

&lt;a href="https://giphy.com/gifs/transparent-reaction-AqEUAOCWjoU5W"&gt;via GIPHY&lt;/a&gt;




&lt;p&gt;One of the good things about the documentation is that, if you're logged in and have an API key, when you click on the sample URL for the request you want to make it'll show you what the API returns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var request = require('request-promise');

require('dotenv').config();

let options = {
    method: 'GET',
    uri: `https://www.goodreads.com/review/list/${process.env.USER_ID}.xml`,
    qs: {
        key: process.env.KEY,
        v: process.env.VERSION,
        shelf: process.env.SHELF,
        per_page: process.env.PER_PAGE
    }
}

request(options).then((shelf) =&amp;gt; {
    console.log(shelf);
}).catch(err =&amp;gt; console.error(err));

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Even though I had already seen the sample URL and what it returned, I was still surprised to see the XML in terminal so I did a quick perusal of the Goodreads developer forums to see if there was a JSON endpoint for this. &lt;/p&gt;

&lt;p&gt;Spoiler alert: there's not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nGP0vLE9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cxi0aliwrqved4g659zq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nGP0vLE9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cxi0aliwrqved4g659zq.gif" alt="DJ from Full House sighing dramatically"&gt;&lt;/a&gt;&lt;/p&gt;
DJ from Full House sighing dramatically. 

&lt;a href="https://giphy.com/gifs/90s-hmpqVzwPARTmo"&gt;via GIPHY&lt;/a&gt;



&lt;p&gt;After doing some quick searching, I decided to install &lt;a href="https://www.npmjs.com/package/xml2js"&gt;xml2js&lt;/a&gt; so I could get the response into a more manageable and readable format.&lt;/p&gt;

&lt;p&gt;I like to work incrementally so after requiring the new package with &lt;code&gt;var xml2js = require('xml2js');&lt;/code&gt;, I modified my &lt;code&gt;.then()&lt;/code&gt; block to parse the response and logged out the end result of that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;request(options).then((shelf) =&amp;gt; {
    xml2js.parseString(shelf, function (err, result) {
        console.log(result);
    });
}).catch(err =&amp;gt; console.error(err));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that I had some nice JavaScript object action going on, it was just a matter of figuring out how to access the titles within the arrays and objects. &lt;/p&gt;

&lt;p&gt;My first step was getting access to the list of books:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let books = result['GoodreadsResponse']['reviews'][0]['review'];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The books were stored in an array which meant randomly selecting an index value was just a matter of picking a number from 0 to the last index in the array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let index = Math.floor(Math.random() * books.length);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Normally, I like to set up intermediate variables when I'm traversing through dense objects and arrays like this but since the only thing I needed was the title and I wasn't going to be doing any more operations, I figured I'd skip the intermediate variables and put it all in the assignment for the title.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let title = books[index]['book'][0]['title'][0];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At this point, the only thing left to do was print out the title and run the app!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var request = require('request-promise');
var xml2js = require('xml2js');

require('dotenv').config();

let options = {
    method: 'GET',
    uri: `https://www.goodreads.com/review/list/${process.env.USER_ID}.xml`,
    qs: {
        key: process.env.KEY,
        v: process.env.VERSION,
        shelf: process.env.SHELF,
        per_page: process.env.PER_PAGE
    }
}

request(options).then((shelf) =&amp;gt; {
    xml2js.parseString(shelf, function (err, result) {
        let books = result['GoodreadsResponse']['reviews'][0]['review'];
        let index = Math.floor(Math.random() * books.length);
        let title = books[index]['book'][0]['title'][0];
        console.log(title);
    });

}).catch(err =&amp;gt; console.error(err));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;Although I don't fancy myself a designer or particularly skilled in terms of creating visually appealing things, I think my next steps will be to make a UI that shows more information like the cover, the author, and the rating and deploy it to Heroku. I may even make a fun feature where users can enter their Goodreads user id to have it randomly select a book from their to-read shelf.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;I've always found it difficult to do side projects outside of work because I could never come up with an idea I liked enough to dedicate time to when I could be reading or doing something else I like to do in my free time. But I think this was a fun way to combine two things I enjoy into a project I may actually use.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
