-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (130 loc) · 3.53 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
154
155
156
157
#!/usr/bin/env node
const http = require('http');
const https = require('https');
const fs = require('fs');
const program = require('commander');
const info = require('./package');
program
.version(info.version, '-v, --version')
.usage('[options] <output-file>')
.requiredOption('-h, --host <host>', 'host without protocol')
.requiredOption('-p, --path <path>', 'path - `${page}` is the placeholder of current page')
.option('-s, --start <n>', 'start page', '1')
.option('-e, --end <n>', 'end page')
.option('-x, --exclude <exclude>', 'regex to exclude specific links')
.option('-t, --types <types>', 'comma separated list of file types', 'mp3')
.option('--ssl', 'use ssl', true)
.option('--no-ssl', 'don\'t use ssl')
.parse(process.argv);
const defaults = {
host: program.host,
path: program.path
};
/**
* Search for MP3s in `href` attributes.
* @param {string} content
* @return {string[]}
*/
function findMP3Links(content)
{
let result = [];
let types = program.types.split(/,/).map(type => type.trim());
let regExp = new RegExp(`href="([^"]+\.(?:${types.join('|')}))"`, 'gi');
let matches = null;
let exclude = (program.exclude)
? new RegExp(program.exclude)
: null;
while ((matches = regExp.exec(content)))
{
let match = decodeURIComponent(matches[1]);
if (exclude && exclude.test(match))
{
continue;
}
result.push(match);
}
return result;
}
/**
* Request content and search for MP3s.
* @param {number} number
* @return {Promise<string[]>}
*/
function getMP3OfPage(number)
{
return new Promise((resolve, reject) =>
{
const options = {
...defaults,
path: defaults.path.replace(/\${page}/g, number)
};
const client = (program.ssl) ? https : http;
client.get(options, (response) =>
{
if (response.statusCode !== 200)
{
console.error(`error: http status code \`${response.statusCode}\``);
process.exit(1);
}
let content = '';
response.on('data', chunk => content += chunk);
response.on('end', () => resolve(findMP3Links(content)));
response.on('error', error => reject(error));
});
});
}
/**
* Get all MP3s of given pages.
* @param {number} pageStart
* @param {number} pageEnd
* @return {Promise<string[]>}
*/
async function getMP3OfPages(pageStart, pageEnd = pageStart)
{
let result = [];
for (let i = pageStart; i <= pageEnd; i++)
{
result = result.concat(await getMP3OfPage(i));
}
return result;
}
/**
* Create a M3U file from the given link array.
* @param {string[]} links
* @return {string}
*/
function createM3U(links)
{
let result = '#EXTM3U\n';
links.forEach(track =>
{
let title = track.slice(track.lastIndexOf('/') + 1, -4);
let url = `http${(program.ssl) ? 's' : ''}://`
+ defaults.host
+ (/\/$/.test(track) ? '' : '/')
+ track.replace(/ /g, '%20');
result += `#EXTINF:-1,${title}\n#EXTVLCOPT:network-caching=1000\n${url}\n`;
});
return result;
}
getMP3OfPages(parseInt(program.start), parseInt(program.end || program.start))
.then(
result =>
{
const m3u = createM3U(result);
if (program.args.length === 1)
{
const filename = program.args[0];
fs.writeFile(filename, m3u, (error) =>
{
if (error)
{
console.error(`error: can\'t write file "${error.message}"`);
process.exit(1);
}
console.log(`File '${filename}' with ${result.length} links written.`);
});
}
else console.log(m3u);
}
);