forked from matiasfha/gatsby-source-buzzsprout-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
124 lines (108 loc) · 2.8 KB
/
gatsby-node.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
118
119
120
121
122
123
"use strict";
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const {
createFilePath,
createRemoteFileNode,
} = require(`gatsby-source-filesystem`);
const fetch = require("node-fetch");
const NODE_TYPE = "PodcastEpisode";
exports.onPreInit = () => console.log("Loaded gatsby-starter-plugin");
const createNodesFromSourceData = ({ sourceData, name, hepers }) => {
console.log(`Creating nodes for ${name}`);
const { createContentDigest, createNode, createNodeId } = helpers;
sourceData.forEach((episode) =>
createNode({
...episode,
slug: episode.title.split(" ").join("-"),
podcastName: name,
id: createNodeId(`${NODE_TYPE}-${episode.id}`),
// hashes the inputs into an ID
parent: null,
internal: {
type: `${NODE_TYPE}${name}`,
content: JSON.stringify(episode),
contentDigest: createContentDigest(episode),
},
})
);
};
exports.sourceNodes = async (
{ actions, createContentDigest, createNodeId, getNodesByType },
{ token, podcastId, name }
) => {
if (!name) {
throw new Error("Buzzsprout Source: name is required.");
}
if (!token) {
throw new Error("Buzzsprout Source: token is required");
}
if (!podcastId) {
throw new Error("Buzzsprout Source: podcastId is required");
}
const { createNode, touchNode } = actions;
const helpers = {
...actions,
createContentDigest,
createNodeId,
};
// touch nodes to ensure they aren't garbage collected
getNodesByType(`${NODE_TYPE}${name}`).forEach((node) =>
touchNode({ nodeId: node.id })
);
let response;
const cacheKey = `Cache-${name}`;
let sourceData = await cache.get(cacheKey);
if (!sourceData) {
console.log("Not using cache, fetching data from api");
response = await fetch(
`https://www.buzzsprout.com/api/${podcastId}/episodes.json`,
{
headers: {
Authorization: `Token token=${token}`,
},
}
);
sourceData = await response.json(); // loop through data returned from the api and create Gatsby nodes for them
}
// Fire interval once a week to get latest data
setInterval(() => {
createNodesFromSourceData({
sourceData,
name,
helpers,
});
}, 604800000);
createNodesFromSourceData({
sourceData,
name,
helpers,
});
return;
};
exports.onCreateNode = async ({
node,
actions: { createNode },
createNodeId,
getCache,
}) => {
if (node.podcastName != null) {
try {
const fileNode = await createRemoteFileNode({
url: node.artwork_url,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
});
if (fileNode) {
node.remoteImage___NODE = fileNode.id;
}
} catch (e) {
console.error(e);
}
}
};