-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
98 lines (88 loc) · 2.92 KB
/
main.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
import fs from "fs";
import { insertAuthors, insertStories, insertStoriesTags } from "./supabase.js";
import { createStoryObjects } from "./wordpress.js";
import cliProgress from "cli-progress";
import colors from "ansi-colors";
import bb from "bluebird";
const { Promise } = bb;
const bar = new cliProgress.SingleBar(
{
format:
"|" +
colors.cyan("{bar}") +
'| {percentage}% || ETA: {eta}s || {value}/{total} stories || Current: "{storyTitle}" (segment: {segment})',
},
cliProgress.Presets.shades_classic
);
async function insertAllStoryData() {
let startStory = 0;
let endStory = 1250;
let storyObjects = createStoryObjects(startStory, endStory);
let logStream = fs.createWriteStream("errored_stories.txt", { flags: "a+" });
Promise.map(
storyObjects,
async (stories) => {
bar.start(endStory - startStory, 0);
bar.update(bar.getProgress(), {
storyTitle: "None",
segment: "Fetching Stories",
});
await Promise.map(
stories,
async (storyObject) => {
try {
bar.update({
storyTitle: storyObject.title.rendered,
segment: "Inserting Content",
});
await insertStories(storyObject);
} catch (error) {
console.log(
`\nError on story "${storyObject.title.rendered}" (id: ${storyObject.id}, link: ${storyObject.link}) for segment "Inserting Content"`
);
console.log(`Error:`);
console.log(error);
logStream.write(storyObject.id.toString());
logStream.write("\n");
}
try {
bar.update({
storyTitle: storyObject.title.rendered,
segment: "Inserting Tags",
});
await insertStoriesTags(storyObject);
} catch (error) {
console.log(
`\nError on story "${storyObject.title.rendered}" (id: ${storyObject.id}, link: ${storyObject.link}) for segment "Inserting Tags"`
);
console.log(`Error:`);
console.log(error);
logStream.write(storyObject.id.toString());
logStream.write("\n");
}
try {
bar.update({
storyTitle: storyObject.title.rendered,
segment: "Inserting Authors",
});
await insertAuthors(storyObject);
} catch (error) {
console.log(
`\nError on story "${storyObject.title.rendered}" (id: ${storyObject.id}, link: ${storyObject.link}) for segment "Inserting Authors"`
);
console.log(`Error:`);
console.log(error);
logStream.write(storyObject.id.toString());
logStream.write("\n");
}
bar.increment();
},
{ concurrency: 3 }
);
},
{ concurrency: 1 }
);
logStream.end();
bar.stop();
}
insertAllStoryData();