-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportData.mjs
255 lines (237 loc) · 7.98 KB
/
importData.mjs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import "@babel/polyfill";
import dotenv from "dotenv";
import fetch from "node-fetch";
import sanityClient from "@sanity/client";
const BATCH_SIZE = 250;
dotenv.config();
const client = sanityClient({
apiVersion: process.env.SANITY_API_VERSION,
dataset: process.env.SANITY_DATASET,
projectId: process.env.SANITY_PROJECT_ID,
token: process.env.SANITY_ACCESS_TOKEN,
useCdn: false, // We can't use the CDN for writing
});
const flatten = (arr) => {
if (arr) {
arr = arr.reduce((flat, toFlatten) => {
return flat.concat(
Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten
);
}, []);
return arr.filter(
(item, index, self) =>
index ===
self.findIndex((a) => {
if (item !== undefined && a !== undefined) {
return a._id === item._id;
}
})
);
}
};
const transform = ([creators, tags, citations], externalCitation, idx) => {
console.log(`Found citation ${externalCitation.key}`);
const localCreators = [];
const localTags = [];
if (externalCitation.data.itemType != "attachment") {
if (externalCitation.data.creators) {
externalCitation.data.creators.map((creator, index) => {
const date = new Date();
const now = date.getMilliseconds().toString();
if (!creator.firstName || !creator.lastName) {
console.warn(
`Skipping creator with invalid name: ${creator.firstName} ${creator.lastName}`
);
return;
}
const item = {
_type: "creator",
_id: `creator-${creator.lastName.replace(
/[^A-Z0-9]/gi,
"-"
)}-${creator.firstName.replace(/[^A-Z0-9]/gi, "-")}`,
_key: `creator-${now}-${idx}-${index}`,
firstName: creator.firstName,
lastName: creator.lastName,
creatorType: creator.creatorType,
};
return localCreators.push(item);
});
}
if (externalCitation.data.tags) {
externalCitation.data.tags.map((tag, index) => {
if (tag.tag) {
const date = new Date();
const now = date.getMilliseconds().toString();
const item = {
_type: "tag",
_id: tag.tag.replace(/[^A-Z0-9]/gi, "-"),
_key: `topic-${now}-${idx}-${index}`,
name: tag.tag,
};
return localTags.push(item);
}
});
}
citations.push({
_id: externalCitation.key,
_type: "citation",
shortTitle: externalCitation.data.shortTitle,
title: externalCitation.data.title,
date: externalCitation.meta.parsedDate,
creators: localCreators.map((i, idx) => ({
_type: "reference",
_ref: i._id,
_key: `ref-creator-${idx}`,
})),
topics: localTags.map((i, idx) => ({
_type: "reference",
_ref: i._id,
_key: `ref-topic-${idx}`,
})),
url: externalCitation.data.url,
websiteTitle: externalCitation.data.websiteTitle,
institution: externalCitation.data.institution,
publicationTitle: externalCitation.data.publicationTitle,
place: externalCitation.data.place,
publisher: externalCitation.data.publisher,
blogTitle: externalCitation.data.blogTitle,
network: externalCitation.data.network,
chicagoCitation: externalCitation.citation,
});
}
return [creators.concat(localCreators), tags.concat(localTags), citations];
};
async function fetchBackoff(url, options) {
const response = await fetch(url, options);
if (response.headers.has("backoff")) {
const backoff = response.headers.get("backoff");
console.log(`Rate-limited: pausing for ${backoff} seconds.`);
await new Promise((resolve) => setTimeout(resolve, backoff));
return fetchBackoff(url, options);
} else if (response.status === 429 && response.headers.has("retry-after")) {
const retryAfter = response.headers.get("retry-after");
console.log(`System overloaded: retrying in ${retryAfter} seconds.`);
await new Promise((resolve) => setTimeout(resolve, retryAfter));
return fetchBackoff(url, options);
}
return response;
}
function zoteroUrl(start, limit) {
return `https://api.zotero.org/groups/${process.env.ZOTERO_GROUP_ID}/items?format=json&include=data,citation&style=chicago-fullnote-bibliography&limit=${limit}&start=${start}`;
}
const commit = async (documents, type) => {
if (documents.length > 0) {
const dedup = new Map();
documents.forEach((document) => {
dedup.set(document._id, document);
});
const deduped = Array.from(dedup.values());
console.log(`Deduplicated down to ${deduped.length} ${type} records.`);
let processed = 0;
do {
const batch = deduped.slice(processed, processed + BATCH_SIZE - 1);
const transaction = client.transaction();
batch.forEach((document) => {
transaction.createIfNotExists(document).patch(document._id, (p) => {
p.set(document);
return p;
});
});
console.log(
`Committing batch ${processed + 1} through ${
processed + batch.length
} of ${deduped.length} total ${type} records...`
);
await transaction.commit();
processed += batch.length;
} while (processed < deduped.length);
}
};
const prune = async (documents, type) => {
if (documents.length > 0) {
const old = await client.fetch(
'*[!(_id in path("drafts.**")) && _type == $type] {_id}',
{ type }
);
const dedup = new Map();
documents.forEach((document) => {
dedup.set(document._id, document);
});
const deduped = Array.from(dedup.values());
console.log(`Deduplicated down to ${deduped.length} ${type} records.`);
const stale = old.reduce((acc, item) => {
if (deduped.findIndex((d) => d._id === item._id) < 0) {
acc.push(item._id);
}
return acc;
}, []);
console.log(`Found ${stale.length} ${type} records to prune:`, stale);
let pruned = 0;
do {
const batch = stale.slice(pruned, pruned + BATCH_SIZE - 1);
const transaction = client.transaction();
batch.forEach((document) => {
transaction.delete(document);
});
console.log(
`Deleted batch ${pruned + 1} through ${pruned + batch.length} of ${
stale.length
} total ${type} stale records...`
);
await transaction.commit();
pruned += batch.length;
} while (pruned < stale.length);
}
};
async function fetchAllCitations() {
let finished = false;
let creators = [];
let tags = [];
let citations = [];
let start = 0;
let limit = 25;
console.log("Fetching citations...");
while (!finished) {
await fetchBackoff(zoteroUrl(start, limit), {
headers: {
"Zotero-API-Version": process.env.ZOTERO_API_VERSION,
"Zotero-API-Key": process.env.ZOTERO_API_KEY,
},
})
.then((response) => {
if (response.ok) return response.json();
throw new Error(
`HTTP Error ${response.status}: ${response.statusText}`
);
})
.then((results) => {
console.log(`Parsing batch starting at ${start}...`);
start += results.length;
if (results.length < limit) finished = true;
return results.reduce(transform, [[], [], []]);
})
.then(([cr, ta, ci]) => {
// docs is now an array of [creators, tags, citation], so we need to flatten it
creators = creators.concat(flatten(cr));
tags = tags.concat(flatten(ta));
citations = citations.concat(flatten(ci));
})
.catch((error) => {
console.error(error);
});
}
console.log(`Fetched ${creators.length} creators.`);
console.log(`Fetched ${tags.length} tags.`);
console.log(`Fetched ${citations.length} citations.`);
try {
await commit(creators, "creator");
await commit(tags, "tag");
await commit(citations, "citation");
await prune(citations, "citation");
await prune(creators, "creator");
} catch (error) {
console.error(error.name + ": " + error.message);
}
}
fetchAllCitations();