-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (109 loc) · 3.75 KB
/
index.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
const axios = require("axios");
const cheerio = require("cheerio");
const express = require("express");
const fs = require("fs");
const PORT = 4000;
const app = express();
/**
* Sleep for "ms" milliseconds.
*
* @param {*} ms
* @returns
*/
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
// name of level creator
let creatorName = encodeURIComponent("INSERT LP LEVEL CREATOR NAME HERE");
// current page of user's levels and necessary lists
let currentLevelListLink = `https://www.levelpalace.com/levels?creator=${creatorName}&level_class=All&sort=newest&difficulty=all&page=1`;
let currentPageLevelLinks = [];
// how long to wait between requests and actions (in milliseconds)
const WAIT_TIME_MS = 10000;
/**
* Download the next page of user's levels.
*/
const downloadNextLevelPage = () => {
// reset level links for new page
currentPageLevelLinks = [];
// get level links of all levels on page
sleep(WAIT_TIME_MS).then(() => {
axios
.get(currentLevelListLink, { headers: { "User-Agent": "lp-helper" } })
.then((res) => {
let $ = cheerio.load(res.data);
if (
$("div.table-container:contains('No levels found.')").length === 0
) {
$("div.card-item", "div.card-blocks").each((index, element) => {
if ($(element).find("a.card-title").length > 0) {
currentPageLevelLinks.push(
"https://www.levelpalace.com/" +
$(element).find("a.card-title").attr("href")
);
}
});
} else {
console.log("No levels were found!");
process.exit(0);
}
// get names and codes of all levels on page
currentPageLevelLinks.forEach((levelLink, index, allLinks) => {
sleep(WAIT_TIME_MS).then(() => {
axios
.get(levelLink, {
headers: { "User-Agent": "lp-helper" },
})
.then((r) => {
sleep(WAIT_TIME_MS).then(() => {
$ = cheerio.load(r.data);
let currentLevelName;
$("p.brand-logo", "div.level-section").each(
(inx, element) => {
currentLevelName = $(element).text().trim();
}
);
$("textarea.level-code-textarea", "div.level-code").each(
(inx, element) => {
fs.writeFileSync(
`levels/${currentLevelName}.txt`,
$(element).text()
);
console.log(
`Successfully saved level "${currentLevelName}"!`
);
}
);
});
})
.catch((err) => console.error(err));
});
});
// check for next page, if it exists, call function recursively until no pages remain
sleep(WAIT_TIME_MS).then(() => {
$ = cheerio.load(res.data);
$("li:not(.disabled):not(.active)", "ul.pagination").each(
(index, element) => {
if (
$(element).find("a").find("i.material-icons").text() ===
"chevron_right"
) {
// increment page number by 1 in level list link
currentLevelListLink =
currentLevelListLink.slice(0, -1) +
(parseInt(currentLevelListLink.slice(-1)) + 1);
downloadNextLevelPage();
}
}
);
});
});
});
};
app.listen(PORT, () => {
console.log(`Server running on port: ${PORT}`);
console.log("Downloading levels...");
downloadNextLevelPage();
});