Skip to content

Instantly share code, notes, and snippets.

@mammuth
Last active December 8, 2018 11:50
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 mammuth/05cb465732d1033bd7142ac1a9cff463 to your computer and use it in GitHub Desktop.
Save mammuth/05cb465732d1033bd7142ac1a9cff463 to your computer and use it in GitHub Desktop.
Get the average number of pages of your books on a given Goodreads shelf
#!/usr/bin/python3
"""
This script returns the average number of pages of the books on a given Goodreads shelf.
Example Output:
> $ python3 goodreads_shelf_average_page_number.py
Average page number: 354
Number of books in shelf to-read-next with known page numbers: 19/24
Total number of pages in your shelf: 8496
To read that within one year, you need to read 23 pages a day
Usage:
- Edit the three variables below (user, shelf, key)
- Get API Developer Key here (only couple of clicks): https://www.goodreads.com/api/keys
- User ID can be retrieved from the URL of your Goodreads profile
- Make your profile temporarily public: https://www.goodreads.com/user/edit
- Run the script with python >= 3.6
"""
from functools import reduce
import requests
from lxml import etree
USER_ID = '' # something like '54988141'. Profile needs to be public!
SHELF = 'to-read-next'
DEVELOPER_KEY = ''
url = f'https://www.goodreads.com/review/list/{USER_ID}.xml?key={DEVELOPER_KEY}&v=2&shelf={SHELF}&per_page=200&page=1'
# print(f'Querying ... ({url})')
xml_string = requests.get(url).content
root = etree.fromstring(xml_string)
pages_list = []
for review in root.find('reviews'):
book = review.find('book')
pages = book.find('num_pages').text
if pages is not None:
pages_list.append(int(pages))
averge_page_number = reduce(lambda x, y: x + y, pages_list) // len(pages_list)
number_of_books_in_shelf = len(root.find('reviews').getchildren())
total_number_of_pages_in_shelf = averge_page_number * number_of_books_in_shelf
print(f'Average page number: {averge_page_number}')
print(f'Number of books in shelf {SHELF} with known page numbers: {len(pages_list)}/{number_of_books_in_shelf}')
print(f'Total number of pages in your shelf: {total_number_of_pages_in_shelf}')
print(f'To read that within one year, you need to read {total_number_of_pages_in_shelf // 365} pages a day')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment