-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
77 lines (66 loc) · 2.33 KB
/
seed.js
File metadata and controls
77 lines (66 loc) · 2.33 KB
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
const axios = require("axios");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
require("dotenv").config();
require("./config/database");
const Sequence = require("./models/sequence");
const start = 1;
const amountToUpdate = 1900;
const lastSequenceId = 346650;
const errors = [];
const time = "";
async function sequenceScraperLong(seqId) {
try {
const { data } = await axios.get(`http://oeis.org/A${seqId}/a${seqId}.txt`);
let dataArray = data.replace(/\n/gm, ` `).replace(/\s+/gm, ` `).split(" ");
dataArray = dataArray.filter((elt, idx) => idx % 2 === 1);
return dataArray.map((x) => +x);
} catch {
return sequenceScraperMedium(seqId);
}
}
async function sequenceScraperMedium(seqId) {
try {
const { data } = await axios.get(`http://oeis.org/A${seqId}/b${seqId}.txt`);
let dataArray = data.replace(/\n/gm, ` `).replace(/\s+/gm, ` `).split(" ");
dataArray = dataArray.filter((elt, idx) => idx % 2 === 1);
return dataArray.map((x) => +x);
} catch {
return sequenceScraperShort(seqId);
}
}
async function sequenceScraperShort(seqId) {
try {
const { data } = await axios.get(`http://oeis.org/A${seqId}/list`);
const dom = new JSDOM(data, {
runScripts: "outside-only",
resources: "usable",
});
const { document } = dom.window;
const list = document.querySelector("pre");
return JSON.parse(list.textContent.replace(/\s+/g, "")).map((x) => +x);
} catch (error) {
errors.push(seqId);
}
}
(async function () {
console.time(time);
await Sequence.deleteMany({});
for (let i = start; i < lastSequenceId; i++) {
let sequenceId = "A" + i.toString().padStart(6, "0");
let sequenceArray = await sequenceScraperMedium(i.toString().padStart(6, "0"));
console.log(`${sequenceId} has length ${sequenceArray.length}`);
sequence = await Sequence.create({ sequenceId, sequenceArray });
}
const totalCreated = await Sequence.countDocuments({});
console.log(`A total of ${amountToUpdate-errors.length} sequences were added.`);
console.log(`The collection has a total of ${totalCreated} sequences.`);
if (errors.length === 0) {
console.log("There were no errors");
} else {
console.log(`There were errors at sequences:`);
errors.forEach((error) => console.log(error));
}
console.timeEnd(time);
process.exit();
})();