This repository was archived by the owner on Jul 30, 2021. It is now read-only.
forked from DanielOliver/gatsby-source-goodreads
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
137 lines (125 loc) · 3.31 KB
/
gatsby-node.js
File metadata and controls
137 lines (125 loc) · 3.31 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const crypto = require(`crypto`)
const { default: Goodreads } = require(`node-goodreads`)
exports.sourceNodes = async (
{
boundActionCreators,
reporter
},
{
userId,
shelves = [`currently-reading`],
apiKey,
apiSecret,
oauthToken,
oauthSecret,
}) => {
const { createNode } = boundActionCreators
const gr = Goodreads({ key: apiKey, secret: apiSecret })
activity = reporter.activityTimer(`fetching goodreads shelf`)
activity.start()
if (!oauthToken || !oauthSecret) {
const { oauthToken, oauthSecret, url } = await gr.authoriseUser()
console.log(
`oauthToken: ${oauthToken}\n`,
`oauthSecret: ${oauthSecret}\n`,
`Please navigate to ${url} and allow this application to access your Goodreads account, `
+ `then provide the oauthToken and oauthSecret to the plugin as options`
)
reporter.panic(`gatsby-source-goodreads: Missing oauthToken and/or oauthSecret`)
}
gr.configureOauth(oauthToken, oauthSecret)
for (let i = 0; i < shelves.length; i++) {
const shelf = shelves[i]
let reviews = []
try {
reviews = (await gr.reviewsList(userId, shelf)).reviews.review
} catch (error) {
reporter.panic(`gatsby-source-goodreads: Failed API call - ${JSON.stringify(error)}`)
}
// console.log(JSON.stringify(reviews[0].book[0].authors, null, 2))
reviews = reviews.map(({
id: [reviewID],
rating: [rating],
votes: [votes],
spoiler_flag: [spoilerFlag],
spoilers_state: [spoilersState],
date_added: [dateAdded],
date_updated: [dateUpdated],
started_at: [startedAt],
read_at: [readAt],
body: [body],
url: [url],
book: [{
id: [{ _: bookID }],
isbn: [isbn],
isbn13: [isbn13],
text_reviews_count: [{ _: textReviewsCount }],
uri: [uri],
link: [link],
title: [title],
title_without_series: [titleWithoutSeries],
image_url: [imageUrl],
small_image_url: [smallImageUrl],
large_image_url: [largeImageUrl],
description: [description],
authors
}],
}) => ({
reviewID,
rating,
votes,
spoilerFlag,
spoilersState,
dateAdded,
dateUpdated,
body,
readAt,
startedAt,
url,
book: {
bookID,
isbn: Number.isNaN(Number.parseInt(isbn)) ? `` : isbn,
isbn13: Number.isNaN(Number.parseInt(isbn13)) ? `` : isbn13,
textReviewsCount,
uri,
link,
title,
titleWithoutSeries,
imageUrl,
smallImageUrl,
largeImageUrl,
description,
authors: authors.map(({
author:
[{
id: [id],
name: [name],
role: [role],
small_image_url: [{ _: authorSmallImageUrl }]
}]
}) => ({
name,
id,
role,
smallImageUrl: authorSmallImageUrl.trim()
}))
}
}))
createNode({
shelfName: shelf,
reviews,
id: `reviewList-${userId}-${shelf}`,
parent: null,
children: [],
internal: {
type: `GoodreadsShelf`,
contentDigest: crypto
.createHash(`md5`)
.update(`shelf-${userId}-${shelf}`)
.digest(`hex`)
}
})
}
activity.end()
return
}