-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.js
105 lines (90 loc) · 2.63 KB
/
app.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
const fs = require("fs");
const $ = require("cheerio");
const puppeteer = require("puppeteer");
const slugify = require("slugify");
const inquirer = require("inquirer");
// const url = "https://www.codewars.com/collections/python-8kyu-1";
// const folder_name = "8kyu";
let url, folder_name;
inquirer
.prompt([
{
type: "input",
name: "url",
message: "Codewars collection url?",
validate: function(value) {
if (value !== "") {
return true;
}
return "URL is required";
}
},
{
type: "input",
name: "folder_name",
message: "Folder name for problems?",
validate: function(value) {
if (value !== "") {
return true;
}
return "Folder name is required";
}
}
])
.then(answers => {
url = answers.url;
folder_name = answers.folder_name;
app(url, folder_name);
});
function app(url, folder_name) {
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.goto(url);
let content = await page.content();
let katas = [];
$(".collection-items .item-title a", content).each(function() {
katas.push({
url: "https://www.codewars.com" + $(this).attr("href"),
title: $(this).text()
});
});
for (let i = 0; i < katas.length; i++) {
const kata = katas[i];
await page.goto(kata.url);
await page.waitFor(
() =>
document.querySelector("#description").innerText !==
"Loading description..."
);
let content = await page.content();
const description = $("#description", content);
kata.description = description.html();
console.log(i + 1, kata);
}
let readme = "\n\n## " + folder_name + "\n";
fs.appendFileSync("readme.md", readme);
if (!fs.existsSync(folder_name)) {
fs.mkdirSync(folder_name);
}
console.log("Writting data to files...");
katas.map((value, index) => {
let slug = slugify(value.title, { remove: /[\/:*?"<>|]/g, lower: true });
let template = `\n${index + 1}. [${value.title}](${
value.url
}) - [Solution](${folder_name}/${index + 1}-${slug}.md)`;
fs.appendFileSync("readme.md", template);
let problemTemplate = "### Problem:\n";
problemTemplate += value.description;
problemTemplate += "\n### Solution";
try {
fs.writeFileSync(
`${folder_name}/${index + 1}-${slug}.md`,
problemTemplate
);
} catch (error) {
console.log(`ERROR --- ${folder_name}/${index + 1}-${slug}.md`);
}
});
await browser.close();
});
}