-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress.js
366 lines (328 loc) · 11.9 KB
/
wordpress.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { decode } from "html-entities";
import * as cheerio from "cheerio";
import fetch from "node-fetch";
import pkg from "bluebird";
const { Promise } = pkg;
import cliProgress from "cli-progress";
import colors from "ansi-colors";
const allStoriesRoute =
"https://girlswritenow.org/wp-json/wp/v2/story/?per_page=10";
const genreMediumRoute =
"https://girlswritenow.org/wp-json/wp/v2/genre-medium/";
const mediaRoute = "https://girlswritenow.org/wp-json/wp/v2/media/";
const coauthorsRoute = "https://girlswritenow.org/wp-json/wp/v2/coauthors/";
const authorBioRoute =
"https://girlswritenow.org/wp-json/elm/v1/guest-authors/";
const manuallyChecked = [
"She’s a 7.8",
"Skinny Girl Memoir",
"A Foreshadowed Understanding",
"My Love Was Bound in Red Silk / I Contain a Silence",
,
"Pull The Pin and Release the Locking Mechanism",
"Love’s Sacrifice",
"House of Lies",
"Some Sundays",
"your little world, here",
"The Terror of Obesity",
"From the Ancient Diary of an Unwilling Globetrotter",
"The Restaurant",
"Punctures of Light in the Darkness",
"The Patient Daughter",
"the ‘filler’",
"Frostbites",
"Motherhood Mural",
"Pink",
"Pocket of Peace",
"Oma’s Hands",
"Welcome To The Neighborhood",
"The Rise of Fame, The Fall of Family",
"How will they tell me you are dead?",
"The Postcard Monologues",
"Kill Them with Laughter, Kill Me with Peanuts",
"The Scientist and the Fawn",
"Mom Pants",
"Here and There",
"Static",
"My Names",
"Crown of Thorns",
"Young Bones",
"Love as Fragile as Time",
"NYC Mayor’s Office Cover Letter",
"Glasshouse Fever Dream",
"crumbs",
];
const gwnEmail = process.env.GWN_USERNAME;
const gwnPassword = process.env.GWN_PASSWORD;
const headers = new Headers();
headers.set(
"Authorization",
"Basic " + Buffer.from(gwnEmail + ":" + gwnPassword).toString("base64")
);
/* Fetch all story objects from WP story endpoint. */
const getAllStories = async (offsetParam) => {
const response = await fetch(
`https://girlswritenow.org/wp-json/wp/v2/story/?per_page=10&offset=${offsetParam}&orderby=date&order=desc`
);
let responseJson;
try {
responseJson = await response.json();
} catch (error) {
console.log(`Error on offset: ${offsetParam}, Error: ${error}`);
console.log(
`https://girlswritenow.org/wp-json/wp/v2/story/?per_page=10&offset=${offsetParam}&orderby=date&order=desc`
);
return [];
}
// console.log(`Fetched data for offset param: ${offsetParam}`)
return responseJson;
};
/* Fetch all genre-medium IDs from WP genreMedium endpoint. */
const getAllGenreMediums = async () => {
const response = await fetch(genreMediumRoute);
await new Promise((r) => setTimeout(r, 1000)); // servers are bad
const responseJson = await response.json();
return responseJson;
};
/* Remove elements with indexes in indexes from arr. */
export function removeElementsByIndexes(arr, indexes) {
// Sort the indexes in descending order to prevent issues when removing elements
indexes.sort((a, b) => b - a);
for (const index of indexes) {
if (index >= 0 && index < arr.length) {
arr.splice(index, 1);
}
}
return arr;
}
/* Filter story objects to be only text-based stories. */
const filterStories = async (storyObjects) => {
// Hardcoded list of non-text based genre-medium IDs
const nonTextGenres = [
344, 383, 405, 414, 416, 470, 478, 503, 564, 568, 635, 712, 1202, 1205,
1216, 1315, 1350, 1351, 1771, 1804, 1885, 2437, 2438,
];
const indexesToRemove = [];
storyObjects.forEach((storyObject, index) => {
storyObject["genre-medium"].forEach((genreID) => {
if (nonTextGenres.includes(genreID)) {
indexesToRemove.push(index);
}
});
});
const filteredData = removeElementsByIndexes(storyObjects, indexesToRemove);
return filteredData;
};
const getFeaturedMediaForListOfStories = async (responseJson) => {
let stories = [];
for (let i = 0; i < responseJson.length; i++) {
let thisStory = responseJson[i];
try {
let featuredMediaLink = await getFeaturedMedia(thisStory.featured_media);
stories.push({ ...thisStory, featuredMediaLink });
} catch {
console.log(`Could not get media for ${thisStory.title.rendered}`);
} finally {
}
}
// console.log(`Got media for ${stories.length} stories`)
return stories;
};
/* Fetch featured media link for each story. */
const getFeaturedMedia = async (featuredmediaId) => {
const featuredMediaLink = mediaRoute + `${featuredmediaId}`;
const response = await fetch(featuredMediaLink, {
"User-Agent": "girlswritenow-mobile",
});
await new Promise((r) => setTimeout(r, 500)); // servers are bad
let responseText = await response.text();
let json = JSON.parse(responseText);
return json.link;
};
/* Parse storyObject content into Heading, Story, Process, and Excerpt. */
function parseNewStory(htmlString, htmlExcerpt) {
const $ = cheerio.load(htmlString);
const excerpt = cheerio.load(htmlExcerpt);
const queryStory =
"div.content-column pre, p:not(.social-sharing__text):not(.breadcrumb__content):not(.author-details__author-bio):not(blockquote > p), blockquote, li:not(.social-sharing__icon), pre.wp-block-verse, div.wp-block-embed__wrapper iframe, .wp-block-audio audio, .wp-block-video video, .wp-block-image img";
const queryProcessHeader = `:header:contains("Process")`;
const queryProcess = "p";
const queryExcerpt = "h2.wp-block-heading";
const genre_medium = $(
`h2.author-details__term-title:contains("Genre / Medium")`
)
.next("div")
.find("span")
.toArray()
.map((v) => $(v).text().trim());
const topic = $(`h2.author-details__term-title:contains("Topic")`)
.next("div")
.find("span")
.toArray()
.map((v) => $(v).text().trim());
return {
genre_medium,
topic,
story:
$(queryStory)
.not(`${queryProcessHeader} ~ ${queryProcess}`)
.toArray()
.map((v) => $(v).prop("outerHTML"))
.join("") || "Not found",
process:
$(`${queryProcessHeader} ~ ${queryProcess}`)
.toArray()
.map((v) => $(v).prop("outerHTML"))
.join("") || "Not found",
excerpt:
excerpt(":header,p").prop("outerHTML") ||
$(queryExcerpt).prop("outerHTML") ||
"Not found",
};
}
function parseOldStory(htmlString, htmlExcerpt) {
let rootedHtml = htmlString;
const $ = cheerio.load(rootedHtml);
const excerpt = cheerio.load(htmlExcerpt ?? "");
const queryStory = `h4, h5, h6, p, li, pre.wp-block-verse, iframe, .wp-block-audio audio, .wp-block-video video, .wp-block-image img`;
const queryProcessHeader = `:header:contains("Process")`;
const queryProcess = "p";
return {
story:
$(queryStory)
.not(`${queryProcessHeader} ~ ${queryProcess}`)
.toArray()
.map((v) => $(v).prop("outerHTML"))
.join("") || "Not found",
process: $(queryProcessHeader).next(queryProcess).prop("outerHTML") || "", // Many old stories don't have a process
excerpt: excerpt(":header,p").prop("outerHTML") || "Not found",
};
}
function regexParseStory(htmlString, htmlExcerpt) {
const regexStory =
/<h2 class="wp-block-heading">.*?<\/h2>[\n\r]*((.|\n|\r)*?).*?(?=(<div style="color:#ddd" class="wp-block-genesis-blocks-gb-spacer gb-block-spacer gb-divider-solid gb-spacer-divider gb-divider-size-1">|<h1 class="wp-block-heading has-text-align-center">))/;
const regexProcess = /Process.*?[\r\n]*?((<p>.*?<\/p>))/;
const regexExcerpt = /(<p>(.|\n|\r)*?<\/p>)/;
const story = htmlString.match(regexStory);
const process = regexProcess.exec(htmlString);
const excerpt = regexExcerpt.exec(htmlExcerpt);
const contentStory = story ? decode(story[1].replace(/^\s*|\s*$/g, "")) : "";
const contentProcess = process ? decode(process[1]) : "";
const contentExcerpt = excerpt ? decode(excerpt[1]) : "";
return {
story: contentStory,
process: contentProcess,
excerpt: contentExcerpt.replace(" /", ""),
valid: false,
};
}
function getFeaturedMediaFromYoast(yoastHead) {
const $ = cheerio.load(yoastHead);
return $(`meta[property="og:image"]`).prop("content");
}
function htmlParser(htmlString, htmlExcerpt, title, yoastHead, link) {
const $ = cheerio.load(htmlString);
const isOldStory = $("div.story-post-title").text().trim().length == 0;
let jquery;
if (isOldStory) {
jquery = parseOldStory(htmlString, htmlExcerpt);
} else {
jquery = parseNewStory(htmlString, htmlExcerpt);
}
jquery["featuredMediaLink"] = getFeaturedMediaFromYoast(yoastHead);
return jquery;
}
/* Create storyObject from raw WP story response. For loop to use offset parameters to avoid JSON errors */
function createStoryObjects(startOffset, endOffset) {
let offsets = [];
for (let i = startOffset; i < endOffset; i += 10) {
offsets.push(i);
}
const bar = new cliProgress.SingleBar(
{
format:
"|" +
colors.green("{bar}") +
"| {percentage}% || ETA: {eta}s || {value}/{total} requests || Fetching Data (offset: {offset})",
},
cliProgress.Presets.shades_classic
);
bar.start((endOffset - startOffset) / 10, 0);
bar.update(bar.getProgress(), { offset: "N/A" });
return Promise.map(
offsets,
async (i) => {
bar.update({ offset: i });
let responseJson = await getAllStories(i);
const stories = await getFeaturedMediaForListOfStories(responseJson);
bar.increment();
return stories;
},
{ concurrency: 2 }
);
}
/* Fetch authorObject and authorID from WP author endpoints. */
async function getAuthor(storyObject) {
const re = /\d+/; // Regex to extract integers from string
let returnObject = [];
for (const coauthor of storyObject.coauthors) {
const coauthorsEndpoint = coauthorsRoute + `${coauthor}`;
const coauthorResponse = await fetch(coauthorsEndpoint, {
method: "GET",
headers: headers,
});
await new Promise((r) => setTimeout(r, 250)); // servers are bad
let responseJson;
try {
responseJson = await coauthorResponse.clone().json();
} catch (error) {
console.log(
`Story ID: ${storyObject.id} (${storyObject.title.rendered}) failed inserting author`
);
// console.log(await coauthorResponse.clone().text())
}
const authorId = re.exec(responseJson.description)[0];
const authorBioEndpoint = authorBioRoute + `${authorId}`;
const authorBioResponse = await fetch(authorBioEndpoint, {
method: "GET",
headers: headers,
});
await new Promise((r) => setTimeout(r, 250)); // servers are bad
const authorBioResponseJson = await authorBioResponse.json();
let thumbnailId = "";
let thumbnailJpeg = "https://girlswritenow.org/?attachment_id=";
if (authorBioResponseJson.metadata._thumbnail_id) {
thumbnailId = authorBioResponseJson.metadata._thumbnail_id[0];
thumbnailJpeg = `https://girlswritenow.org/?attachment_id=${thumbnailId}`;
} else {
console.log(
`\nStory ${storyObject.title.rendered} does not have an author image`
);
}
// const thumbnailResponse = await fetch(
// mediaRoute + `${authorBioResponseJson.metadata._thumbnail_id[0]}`
// );
// const thumbnailResponseJson = await thumbnailResponse.json();
// const thumbnailJpeg = thumbnailResponseJson.link;
returnObject.push({
id: coauthor,
name: authorBioResponseJson.post_title,
pronouns: authorBioResponseJson.metadata.pronouns
? authorBioResponseJson.metadata.pronouns.length > 0
? authorBioResponseJson.metadata.pronouns[0]
: ""
: "",
bio:
(authorBioResponseJson.metadata["cap-description"] ?? []).length > 0
? authorBioResponseJson.metadata["cap-description"][0]
: "",
artist_statement:
(authorBioResponseJson.metadata.artist_statement ?? []).length > 0
? authorBioResponseJson.metadata.artist_statement[0]
: "",
thumbnail: thumbnailJpeg,
});
}
return returnObject;
}
export { createStoryObjects, getAuthor, htmlParser };