-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (67 loc) · 2.34 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
const cheerio = require('cheerio');
const fs = require('fs');
const path = require('path');
function loadTemplate() {
return new Promise((resolve, reject) => {
const fileName = path.join(__dirname, 'template.html');
fs.readFile(fileName, 'utf8', (err, content) => {
return resolve(content);
});
})
}
function cleanUrl(url) {
const URL = require('url');
const myURL = URL.parse(url);
myURL.search = '';
myURL.hash = '';
return myURL.protocol + '//' + myURL.host + myURL.pathname;
}
function donwloadPage(url) {
const rp = require('request-promise');
return rp(url + '/fullscreen')
}
function downloadImage(fileName, url) {
return new Promise((resolve, reject) => {
const request = require('request');
request(url).on('response', function (res) {
const fws = fs.createWriteStream(fileName);
res.pipe(fws);
res.on('end', resolve);
res.on('error', console.error);
fws.on('error', console.error);
}).on('error', reject);
})
}
function main(slidesURL, folderName) {
slidesURL = cleanUrl(slidesURL);
if (!folderName) {
const lastIndex = slidesURL.lastIndexOf('/');
folderName = slidesURL.substr(lastIndex + 1);
}
const imgFolder = path.join(folderName, 'img');
fs.mkdirSync(imgFolder, { recursive: true });
Promise.all([
loadTemplate(),
donwloadPage(slidesURL).then((html) => {
const $ = cheerio.load(html);
$('body').find('script').remove();
return $('body').html();
})
]).then(([template, slides]) => {
const $ = cheerio.load(template);
$('.insert').after(slides);
$('img[data-src]').map((a, b) => {
const img = $(b).attr('data-src');
if (img.startsWith('https://s3.amazonaws.com')) {
const lastIndex = img.lastIndexOf('/');
const name = img.substr(lastIndex + 1);
const imgPath = path.join(imgFolder, name);
downloadImage(imgPath, img)
$(b).attr('data-src', path.join('img', name));
}
});
fs.writeFileSync(path.join(folderName, 'index.html'), $.html());
})
}
const slides = 'https://slides.com/andes/sprint-75-instituciones-5bd030';
module.exports = main;