-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (137 loc) · 5.28 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
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
#!/usr/bin/env node
import axios from 'axios';
import chalk from 'chalk';
import cheerio from 'cheerio';
import inquirer from 'inquirer';
import fs from 'fs';
const HEADER = " ________ __ __ __ \n" +
"/ | / | / | / |\n" +
"$$$$$$$$/ ______ $$ |____ ______ ______ _______ $$ | __ ______ __ _______ $$/ \n" +
" /$$/ / \\ $$ \\ / \\ / \\ / \\ $$ | / | / \\ / | / |/ |\n" +
" /$$/ $$$$$$ |$$$$$$$ |/$$$$$$ |/$$$$$$ |$$$$$$$ |$$ |_/$$/ $$$$$$ | $$/ /$$$$$$$/ $$ |\n" +
" /$$/ / $$ |$$ | $$ |$$ | $$/ $$ $$ |$$ | $$ |$$ $$< / $$ | / | $$ \\ $$ |\n" +
" /$$/____ /$$$$$$$ |$$ |__$$ |$$ | $$$$$$$$/ $$ | $$ |$$$$$$ \\ /$$$$$$$ | $$ | __ $$$$$$ |$$ |\n" +
"/$$ |$$ $$ |$$ $$/ $$ | $$ |$$ | $$ |$$ | $$ |$$ $$ | $$ |/ |/ $$/ $$ |\n" +
"$$$$$$$$/ $$$$$$$/ $$$$$$$/ $$/ $$$$$$$/ $$/ $$/ $$/ $$/ $$$$$$$/__ $$ |$$/ $$$$$$$/ $$/ \n" +
" / \\__$$ | \n" +
" $$ $$/ \n" +
" $$$$$$/ ";
async function header() {
console.log(chalk.cyan(HEADER));
console.log(chalk.cyan("Scrape tabs from zabrenkaj.si"));
console.log(chalk.yellow("GitHub: \tgithub.com/cgorican"));
console.log(chalk.yellow("Discord:\tcxg#0510"));
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function scrapePage(scrapeURL) {
var res = await axios(scrapeURL)
.catch((err) => {
if (err)
console.log(chalk.red("Bad request!"));
});
if(res === undefined) {
console.log(chalk.cyan("Try another URL!"));
await sleep(1500);
return false;
}
const html = res.data;
const data = parseHTML(html);
const dirname = 'downloads';
if (!fs.existsSync(dirname)){
fs.mkdirSync(dirname);
}
var fileName = `${dirname}/${data.author}__${data.title}.tab`;
fileName = fileName.replaceAll(' ','_');
try {
var testFileName = fileName;
while(fs.existsSync(testFileName)) {
const extensionIndex = testFileName.lastIndexOf('.')
const extension = testFileName.substring(extensionIndex, testFileName.length);
testFileName = testFileName.substring(0, extensionIndex);
testFileName = `${testFileName}_${Math.floor(Math.random() * 3000)}${extension}`;
}
fileName = testFileName;
} catch(err) {
console.error(err)
}
await fs.writeFileSync(`${fileName}`, data.body);
console.log(chalk.greenBright("Success!"));
console.log(chalk.green(`File ${fileName} created.`));
await sleep(2500);
return true;
}
function parseHTML(body) {
var $ = cheerio.load(body);
const author = $('.song-facts').find('h3').first().text().trim();
const title = $('.main__title').first().text().trim();
var songContent = $('#song-content').html().toString().trim();
songContent = songContent.replaceAll('<br>', '\n');
songContent = songContent.replaceAll('<br/>', '\n');
songContent = songContent.replaceAll('{', '[');
songContent = songContent.replaceAll('}', ']');
$ = cheerio.load(songContent);
const result = $.text();
return {author: author, title: title, body: result};
}
async function handleMenu(option) {
switch(option) {
case 'Enter URL': {
const DOMAIN = "zabrenkaj.si";
var scrapeURL = await askURL();
//fix & check URL
const start = scrapeURL.toLowerCase().indexOf(DOMAIN.toLowerCase());
if(start !== -1) {
scrapeURL = scrapeURL.substring(start, scrapeURL.length);
scrapeURL = `https://${scrapeURL}`;
await scrapePage(scrapeURL);
}
else {
console.log(chalk.red("Invalid domain - you can only scrape from zabrenkaj.si."))
await sleep(3000);
}
break;
}
default: {
console.log(chalk.magentaBright("Goodbye :)"));
await sleep(1000);
console.clear();
process.exit(0);
break;
}
}
}
async function menu() {
const answer = await inquirer.prompt({
name: 'option',
type: 'list',
message: 'Choose option:',
choices: [
'Enter URL',
'Exit'
]
});
return await handleMenu(answer.option);
}
async function askURL() {
const answer = await inquirer.prompt({
name: 'scrapeURL',
type: 'input',
message: 'Enter song URL:',
default() {
return null;
},
});
return answer.scrapeURL;
}
async function main() {
while(true) {
await header();
await menu();
console.clear();
}
}
main();