integrating Ywar with Goodreads

Ywar is a little piece of productivity software that I wrote. I’ve written about Ywar before, so I won’t rehash it much. The idea is that I use The Daily Practice to track whether I’m doing things that I’ve decided to do. I track a lot of these things already, and Ywar connects up my existing tracking with The Daily Practice so that I don’t have to do more work on top of the work I’m already doing. In other words, it’s an attempt to make my data work for me, rather than just sit there.

For quite a while now, only a few of my TDP goals needed manual entry, and most of them could clearly be automated. It wasn’t clear, though, how to automate my “keep reading books” tasks. I knew Goodreads existed, but it seemed like using Goodreads would be just as much work as using TDP. Either way, I have to go to a site and click something for each book. I kept thinking about how to make my reading goals more motivating and more interesting, but nothing occurred to me until this weekend.

I was thinking about how it’s hard for me to tell how long it will take me to finish a book. Lately, I’m taking an age to read anything. Catch-22 is about 500 pages and I’ve been working on it since January 2. Should I be able to do more? I’m not sure. My current reading goals have been very vague. I thought of them as, “spend ‘enough time’ reading a book from each shelf once every five days.” This makes it easy to decide sloppily whether I’ve read enough, but it’s always an all-at-once decision.

In Goodreads, I can keep track of my progress over several days. That means I can change my goal to “get at least 50 pages read a week.” There’s no fuzzy logic there, just simple page count. It might not be right for every book, but I can adjust it as needed. If it’s too low or high, I can fix that too. It seemed like a marked improvement, and it also gave me a reason to consider looking at Goodreads a bit more, where I’ve seen some interesting recommendations.

With my mind made up, all I had to do was write the code. Almost every time that I’ve wanted to write code to talk to the developer API of a service that’s primarily addressed not via the API, it’s been sort of a mess that’s usable, but weird and a little annoying. So it was with Goodreads. The code for my Goodreads/Ywar integration is on GitHub. Below is just some of the weirdness I got to encounter.

This request gets the books on my “currently reading” shelf as XML.

sprintf '',

The resource is review/list because it’s a list of reviews. Go figure! That doesn’t mean that there are actually any reivews, though. In Goodreads, a review represents the intersection of a user and a book. If it’s on your shelf, it has a review. If there’s no review in the usual sense of the word, it just means that the review’s body is empty.

The XML document that you get in reply has a little bit of uninteresting data, followed by a <reviews> element that contains all the reviews for the page of results. Here’s a review:

    <id type="integer">168668</id>
    <text_reviews_count type="integer">7875</text_reviews_count>
    <title>Catch-22 (Catch-22, #1)</title>
    <publisher>Simon &amp; Schuster </publisher>
    <description>...omitted by rjbs...</description>
        <name>Joseph Heller</name>

    <shelf name="currently-reading" />
    <shelf name="literature" />
  <started_at>Thu Jan 02 17:04:20 -0800 2014</started_at>
  <date_added>Tue Nov 26 08:37:09 -0800 2013</date_added>
  <date_updated>Thu Jan 02 17:04:20 -0800 2014</date_updated>


It’s XML. It’s not really that bad, either. One problem, though, was that it didn’t include my current position. My current position in the book is not a function of my review, but of my status updates. I’ll need to get those, too.

I was intrigued, though by the format=xml in the URL. Maybe I could get it as JSON! I tried, and I got this:


Well! That’s certainly briefer. It’s also, obviously, missing a ton of data. It doesn’t include book titles, total page count, or any shelves other than the one that I requested. That is: note that in the XML you can see that the book is on both currently-reading and literature. In the JSON, only currently-reading is listed. Still, it turns out that this is all I need, so it’s all I fetch. I get the JSON contents of my books in progress, and then once I have them, I can get each review in full from this resource:

  sprintf '',

Why does that help? I mean, what I got in the first request was a review, too, right? Well, yes, but when you get the review via review/show.xml, you get a very slightly different set of data. In fact, almost the only difference is the inclusion of comment and user_status items. It’s a bit frustrating, because in both cases you’re getting a review element, and their ids are the same, but their contents are not. It makes it a bit less straightforward to write an XML-to-object mapper.

When I get review 774476430, which is my copy of Catch-22, this is the first user status in the review:

    <chapter type="integer" nil="true"/>
    <comments_count type="integer">0</comments_count>
    <created_at type="datetime">2014-02-16T12:47:14+00:00</created_at>
    <id type="integer">39382590</id>
    <last_comment_at type="datetime" nil="true"/>
    <note_updated_at type="datetime" nil="true"/>
    <note_uri nil="true"/>
    <page type="integer" nil="true"/>
    <percent type="integer">68</percent>
    <ratings_count type="integer">0</ratings_count>
    <updated_at type="datetime">2014-02-16T12:47:14+00:00</updated_at>
    <work_id type="integer">814330</work_id>

By the way, the XML you get back isn’t nicely indented as above. It’s not entirely unindented, either. It’s sometimes properly indented and sometimes just weird. I think I’d be less weirded out if it just stuck to being a long string of XML with indentation at all, but mostly libxml2 reads the XML, not me, so I should shut up.

The important things above are the page and percent items. They tell me how far through the book I am as of that status update. If I gave a page number when updating, the page element won’t have “true” as its nil attribute, and the text content of the element will be a number. If I gave a percentage when updatng, as I did above, you get what you see above. I can convert a percentage to a page count by using the num_pages found on the book record. The whole book record is present in the review, as it was the first time, so I just get all the data I need this time via XML.

Actually, though, there’s a reason to get the XML the first time. Each time that I do this check, it’s for in-progress books on a certain shelf. If I start by getting the XML, I can then proceed only with books that are also on the right shelf, like, above, “literature.” Although you can specify multiple shelves to the review/list endpoint, only one of them is respected. If there are four books on my “currently reading” shelf, but only one is “literature,” then by getting XML first, I’ll do two queries instead of five.

So I guess I should go back and start with the XML.

By the way, did you notice that review/list takes a query parameter called format, which can be either XML or JSON, and maybe other things… but that review/show.xml includes the type in the path? You can’t change the xml to json and get JSON. You just get a 404 instead.

In the end, making Ywar get data from Goodreads wasn’t so bad. It had some annoying moments, as is often the case when using a mostly-browser-based web service’s API. It made me finally use XML::LibXML for some real work, and hopefully it will lead to me using Goodreads more and getting some value out of that.

Written on February 18, 2014
🐪 perl
🌀 productivity
🧑🏽‍💻 programming
🏷 reading
🏷 ywar