-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (63 loc) · 1.55 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
const cheerio = require('cheerio');
const rp = require('request-promise');
const fs = require('fs');
const ARCHILLECT = 'http://archillect.com';
const FOLDER_PREFIX = './images/';
const getIndex = async () => {
try {
const result = await rp(ARCHILLECT);
return result;
}
catch (err) {
return false;
}
}
const getImageSrc = async (image) => {
try {
const result = await rp(ARCHILLECT + image);
const $ = cheerio.load(result);
return $('img#ii').attr('src');
} catch (err) {
return false;
}
};
const downloadImage = async (filename, src) => {
try {
const path = `${FOLDER_PREFIX}${filename}.jpg`;
if (fs.existsSync(path)) {
console.log(`File ${path} already exists!`);
return false;
}
const options = {
url: src,
encoding: null,
gzip: true
}
const result = await rp(options);
fs.writeFileSync(`${path}`, result, 'binary');
console.log(`Successfully wrote: ${path}`);
} catch (err) {
console.log(`downloadImage(${filename}) error: ${err}`);
return false;
}
}
const main = async () => {
console.log('Getting archillect.com...');
const index = await getIndex();
console.log('Parsing results...');
const $ = cheerio.load(index);
const items = $('#container a');
const links = items.map(function(i, el) {
// this === el
return $(el).attr('href');
}).get();
links.forEach(async (e) => {
console.log(`Downloading ${e}...`);
const src = await getImageSrc(e);
downloadImage(
e.replace('/',''),
src
);
});
};
main();