-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
117 lines (95 loc) · 3.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Get access to credentials
require('dotenv').config()
const superagent = require('superagent')
const parser = require('xml2json')
const pug = require('pug')
const R = require('ramda')
const fs = require('fs')
// 'Get the books on a members shelf'
// https://goodreads.com/api/index#reviews.list
const yearMonthDay = (date) => {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
}
const goodreadsKey = process.env.GOODREADS_KEY
const goodreadsUser = process.env.GOODREADS_USER
const goodreadsShelf = process.env.GOODREADS_SHELF
const goodreadsUrlShelf = 'https://www.goodreads.com/review/list'
;(async () => {
try {
let currentPage = 1
const resultsPerPage = 100 // default 20, can go up to 100
let res, body, response, shelf
let books = []
let end, total
do {
res = await superagent.get(goodreadsUrlShelf).query({
key: goodreadsKey,
id: goodreadsUser,
shelf: goodreadsShelf,
v: 2,
page: currentPage,
per_page: resultsPerPage
}).accept('xml')
body = JSON.parse(parser.toJson(res.body.toString('utf8')))
response = body.GoodreadsResponse
shelf = response.reviews
books = books.concat(shelf.review)
;({ end, total } = shelf)
currentPage += 1
console.log(`end: ${end}, total: ${total}`)
// `end` and `total` are strings, so cast them we must
} while (Number(end) < Number(total))
const getName = R.pluck('name')
const getAuthorsNames = R.compose(R.values, getName)
const validIsbnOrNothing = maybeIsbn => {
return isNaN(Number(maybeIsbn)) ? null : maybeIsbn
}
// relevant book structure:
const bookDigest = (bookObj) => {
const book = bookObj.book
const findDate = R.ifElse(
R.compose(R.is(String), R.prop('read_at')),
R.prop('read_at'),
R.prop('date_added')
)
const date = findDate(bookObj)
const yearRead = date ? Number(date.slice(-4)) : null
return ({
id: book.id.$t,
isbn: validIsbnOrNothing(book.isbn),
title: book.title,
title_without_series: book.title_without_series,
image_url: book.image_url,
link: book.link,
rating: bookObj.rating,
authors: getAuthorsNames(book.authors),
year: yearRead
})
}
// [{ year: 2001, … }, { year: 2000, … }, …]
const booksDigest = R.map(bookDigest, books)
// { 2000: [{ year: 2000, … }, …], 2001: [{ year: 2001, … }, …], … }
const booksByYear = R.groupBy(R.prop('year'))(booksDigest)
// [[2000: [{ year: 2000, … }, …]], [2001: [{ year: 2001, … }, …]], …]
const booksInArray = R.toPairs(booksByYear)
// [[2001: [{ year: 2001, … }, …]], [2000: [{ year: 2000, … }, …]], …]
// Yes, this is still an array and not an object, because of key order
const sortedFromNewestToOldest = R.sort(R.descend(R.prop(0)))
const sortedBooks = sortedFromNewestToOldest(booksInArray)
console.log(`Digest composed of ${booksDigest.length} books`)
const html = pug.renderFile('views/index.pug', {
// variables
booksPerYear: sortedBooks,
timestamp: yearMonthDay(new Date()),
// pug config
self: true,
pretty: true
})
fs.writeFile('index.html', html, function (err) {
if (err) return console.log(err)
console.log('The file was saved!')
})
} catch (err) {
console.log('Error:', err)
}
})()