-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.5 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
const fs = require('fs');
const request = require('request');
const cheerio = require('cheerio');
const prompt = require("prompt-sync")({ sigint: true });
const domain = 'https://telegra.ph';
const downloadAlbum = function(url, rootFolder = 'album', callback) {
if (!fs.existsSync(rootFolder)){
fs.mkdirSync(rootFolder);
}
request({
url: url,
method: "GET"
}, function(err ,req, body) {
if(err || !body){
callback(null, 'some error happen while request');
return;
}
const $ = cheerio.load(body);
const pic = [];
const pictures = $('figure');
const title = folderNamePreprocess( $('title').text() );
if (!fs.existsSync(`${rootFolder}/${title}`)){
fs.mkdirSync(`${rootFolder}/${title}`);
}
for(let i=0; i<pictures.length; i++) {
let src = domain + pictures[i.toString()].children[0].attribs.src;
let srcType = src.split('.');
let type = srcType[srcType.length - 1];
let path = `${rootFolder}/${title}/${i}.${type}`;
pic.push({ src: src, path: path, title: title });
}
callback(title, pic);
});
};
const folderNamePreprocess = function(folder_name) {
folder_name = folder_name.replace(/[ |?<\\*:"']/g,'');
return folder_name;
}
const downloadImage = function(uri, path, callback){
try {
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(path)).on('close', callback);
});
} catch(err) {
console.log(err);
}
};
module.exports = { downloadAlbum, downloadImage }