-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·80 lines (76 loc) · 2.42 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
#!/usr/bin/env node
const getParserByName = require('./src/dictionaries').getParserByName;
const downloadPromise = require('./src/downloadFile');
const autoplayFile = require('./src/autoplay');
const { addGap } = require('./src/audio');
const program = require('commander');
const options = program.opts();
const bluebird = require('bluebird');
const path = require('path');
program
.version('0.8.0', '-v, --version')
.description(
'An application for getting transcription and audio from Oxford Advanced Learner’s Dictionary, Cambridge Dictionary'
)
.usage('[options] <words>')
.option('-p, --path [value]', 'Path for downloaded files', path.resolve('./'))
.option(
'-d, --dictionary [value]',
'Dictionary [oxford, cambridge, macmillan]',
/^(oxford|cambridge|macmillan)$/i,
'cambridge'
)
.option('-g, --gap [value]', 'Add gap [value] sec to the end of file', 0)
.option('--play', 'Auto play files after downloading', false)
.option(
'-c, --concurrency [value]',
'Indicate how much process will start',
5
)
.parse(process.argv);
const destination = path.normalize(options.path);
const words = program.args;
const parser = getParserByName(options.dictionary);
const gap = options.gap;
const concurrency = options.concurrency;
const autoplay = options.play;
console.log(`Save to path: ${destination}, concurrency: ${concurrency}`);
if (!destination) return;
const constructFilePath = (
destination,
{ word, transcription, main_transcription, mp3, main_mp3, title }
) => {
const name = word === title ? word : title;
if (process.platform === 'win32')
return path.join(
destination,
`${name} ${transcription || main_transcription}.mp3`
);
return path.join(
destination,
`${name} | ${transcription || main_transcription} |.mp3`
);
};
bluebird
.map(words, parser, { concurrency: +concurrency })
.then((data) =>
bluebird.map(
data.filter((item) => item.main_mp3 || item.mp3),
(result) =>
downloadPromise(
result.mp3 || result.main_mp3,
constructFilePath(destination, result)
).then((file) => (gap > 0 && file ? addGap(file, +gap) : file)),
{ concurrency: +concurrency }
)
)
.then((files) =>
autoplay
? bluebird.mapSeries(
files.filter((file) => !!file),
autoplayFile
)
: files
)
.then((result) => console.log('Finish'))
.catch((error) => console.log('Error', error));