-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (54 loc) · 2 KB
/
index.js
File metadata and controls
65 lines (54 loc) · 2 KB
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
// IMPORTS
const cheerio = require('cheerio'),
request = require('request'),
download = require('image-downloader'),
fs = require('fs');
// PROCESS ARGS
const term = process.argv[2] ?? 'shoes';
const numberOfPages = process.argv[3] ?? 1,
batchName = process.argv[4] ?? term;
// REQUEST OPTS
let options = {
method: 'GET',
url: `https://www.amazon.com/s?k=${term}${numberOfPages > 1 ? '&page=' + numberOfPages : ''}`,
gzip: true,
headers:
{ "User-Agent": "Chrome /70.0.3538.77" }
}
// LOOP THROUGH PAGES WORTH OF RESULTS
for (pageNum = 1; pageNum < parseInt(numberOfPages) + 1; pageNum++) {
//REQUEST
request(options,
(err, res, body) => {
// ERR THROW ERR
if (err) return console.error(err);
let $ = cheerio.load(body);
// GRAB IMAGES
($('a > div img').each((i, e) => {
// GUARD CLAUSE AGAINST TINY IMGS
if (e.attribs.height < 2 &&
e.attribs.width < 2) { return }
// SET URL AND DEST
options = {
url: `${e.attribs.src.replace('_AC_UL320_', '_AC_UL1280_')}`,
dest: `batch/${batchName}/${pageNum}_${i}.jpg`
}
// CREATE IF NOT EXISTS BATCH DIR
if (!fs.existsSync(`batch`)) {
fs.mkdirSync(`batch`);
}
// CREATE IF NOT EXISTS BATCHNAME DIR
if (!fs.existsSync(`batch/${batchName}`)) {
fs.mkdirSync(`batch/${batchName}`);
}
// DOWNLOAD IMAGE
download.image(options)
// CONSOLE FILENAME
.then(({ filename }) =>
console.log(`file saved at ${filename}`))
// OR THROW
.catch((err) =>
console.error(err))
}));
});
}