This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
149 lines (124 loc) · 4.56 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const pocketService = require("./pockerService");
const startOfDay = require("date-fns/startOfDay");
const startOfWeek = require("date-fns/startOfWeek");
const format = require("date-fns/format");
const subWeeks = require("date-fns/subWeeks");
const { URL } = require("url");
function getUnixTimeToGetArticlesFrom(pluginOptions) {
// get the data since the last time it was run, or from the earliest week
let lastGeneratedDateStamp = subWeeks(
startOfWeek(new Date()),
pluginOptions.weeksOfHistory
);
// override - usually used in prod just to update current and last week on a nightly update after the first full generation.
if (pluginOptions.getCurrentWeekOnly.toLowerCase() === "y") {
lastGeneratedDateStamp = startOfWeek(subWeeks(new Date(), 1));
}
const unixTimeToGetArticlesFrom = parseInt(
Date.parse(lastGeneratedDateStamp) / 1000
);
if (isNaN(unixTimeToGetArticlesFrom)) {
throw new Error("set a pocket start date in options");
}
return unixTimeToGetArticlesFrom;
}
function getApiParamOptions(unixTimeToGetArticlesFrom, pluginOptions) {
// get/retrieve/search parameters.
// See https://getpocket.com/developer/docs/v3/retrieve for full list of available params.
let params = {
sort: "newest",
count: parseInt(pluginOptions.apiMaxRecordsToReturn),
detailType: "complete",
state: pluginOptions.stateFilterString,
since: unixTimeToGetArticlesFrom,
};
// now do optional parameters
if (pluginOptions.tagFilter) {
params.tag = pluginOptions.tagFilterString;
}
if (pluginOptions.favouriteFilter) {
params.favorite = pluginOptions.favouriteFilterValue;
}
if (pluginOptions.tagFilter) {
params.tag = pluginOptions.tagFilterString;
}
if (pluginOptions.searchFilter) {
params.search = pluginOptions.searchFilterString;
}
if (pluginOptions.domainFilter) {
params.domain = pluginOptions.domainFilterString;
}
return params;
}
function pocketResponseToArticlesArray(pocketApiResults) {
return Object.keys(pocketApiResults.list).map(
(value) => pocketApiResults.list[value]
);
}
const POCKET_ARTICLE_NODE_TYPE = "PocketArticle";
exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest, getNodesByType },
pluginOptions
) => {
const { createNode, touchNode } = actions;
getNodesByType(POCKET_ARTICLE_NODE_TYPE).forEach((node) => touchNode(node));
const pocketClient = pocketService.initPocketClient(
pluginOptions.consumerKey,
pluginOptions.accessToken
);
const pocketCallParams = getApiParamOptions(
getUnixTimeToGetArticlesFrom(pluginOptions),
pluginOptions
);
const resp = await pocketService
.fetchArticles(pocketClient, pocketCallParams)
.catch((e) => console.log(e));
const data = pocketResponseToArticlesArray(resp);
data.forEach((datum) => {
const image =
datum.has_image && datum.image
? {
item_id: datum.item_id,
src: datum.image.src,
width: datum.image.width,
height: datum.image.height,
}
: null;
const articleDomain = Boolean(datum.resolved_url)
? new URL(datum.resolved_url).hostname
: "";
const tags = datum.tags ? Object.keys(datum.tags) : [];
const readDay = datum.time_read ? parseInt(format(startOfDay(new Date(parseInt(datum.time_read) * 1000)), "X")) : 0
const readWeek = datum.time_read ? parseInt(format(startOfWeek(new Date(parseInt(datum.time_read) * 1000)), "X")) : 0
createNode({
id: createNodeId(`${POCKET_ARTICLE_NODE_TYPE}-${datum.item_id}`),
readDay: readDay,
readWeek: readWeek,
url: datum.resolved_url,
title: datum.resolved_title,
articleDomain: articleDomain,
domainFavicon: `https://s2.googleusercontent.com/s2/favicons?domain_url=${articleDomain}`,
favourite: datum.favorite == true,
favorite: datum.favorite == true,
excerpt: datum.excerpt,
is_article: datum.is_article == true,
is_index: datum.is_index == true,
has_video: datum.has_video == true,
has_image: datum.has_image == true,
word_count: parseInt(datum.word_count),
tags: tags,
time_added: datum.time_added,
time_updated: datum.time_updated,
time_read: parseInt(datum.time_read),
image: image,
// Required fields.
parent: null, //`the-id-of-the-parent-node`, // or null if it's a source node without a parent
children: [],
internal: {
type: POCKET_ARTICLE_NODE_TYPE,
contentDigest: createContentDigest(datum),
content: JSON.stringify(datum), // optional
},
});
});
};