1 | function fetch(url, options) {
|
2 | const fsp = require('fs-promise');
|
3 | const path = require('path');
|
4 | const request = require('request');
|
5 | const fs = require('fs');
|
6 | const chalk = require('chalk');
|
7 | const unzip = require('unzip');
|
8 |
|
9 | let abelonerc = {};
|
10 |
|
11 |
|
12 |
|
13 | abelonerc.url = url;
|
14 |
|
15 | abelonerc.basename = path.basename(url, '.zip');
|
16 | abelonerc.dir = path.dirname(url);
|
17 | abelonerc.compressor = path.extname(url) || '.zip';
|
18 | abelonerc.START_PAGE = '9';
|
19 | abelonerc.repo_url = '';
|
20 |
|
21 | fsp.writeFile('.abelonerc', JSON.stringify(abelonerc, null, 2))
|
22 | .catch((err) => {
|
23 | if (err)
|
24 | console.log(chalk.bold.red('Failed to write abelone URL', err));
|
25 | });
|
26 |
|
27 | if (!fs.existsSync(path.join('interim', 'original.html')) || options.force) {
|
28 |
|
29 | fsp.mkdirs('interim')
|
30 | .then(() => {
|
31 | request(url)
|
32 | .pipe(fs.createWriteStream(path.join('.', 'interim', 'book.zip')))
|
33 | .on('close', function() {
|
34 | console.log(chalk.blue('Prebook zip downloaded.'));
|
35 | fs.createReadStream(path.join('.', 'interim', 'book.zip')).pipe(unzip.Extract({ path: path.join('.', 'interim') }));
|
36 | });
|
37 | })
|
38 | .catch((err) => {
|
39 | if (err)
|
40 | console.log(chalk.bold.red('Failed to fetch book…', err));
|
41 | });
|
42 |
|
43 | } else {
|
44 | console.log('Page exists!');
|
45 | return true;
|
46 | }
|
47 |
|
48 | }
|
49 |
|
50 |
|
51 | module.exports.fetch = fetch;
|