-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
168 lines (158 loc) · 4.49 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const fs = require('fs');
const clear = require('clear');
const chalk = require('chalk');
const figlet = require('figlet');
const inquirer = require('inquirer');
const mkdirp = require('mkdirp');
const request = require('superagent');
const md5 = require('md5');
const Scraper = require('./scraper');
const main = async () => {
clear();
console.log(chalk.yellow(figlet.textSync('Google DL', { horizontalLayout: 'full' })));
try {
const { query, baseFolder, qtty } = await inquirer.prompt([
{ type: 'input', name: 'query', message: 'Search term?' },
{
type: 'input',
name: 'baseFolder',
message: 'Base directory to download to?',
default: './downloads',
},
{
type: 'input',
name: 'qtty',
message: 'How many pictures (max. 400)?',
default: () => 200,
validate: (val) => {
if (Number.isInteger(+val)) {
if (+val <= 400) return true;
}
return false;
},
},
]);
const { folder, domain, ...tbs } = await inquirer.prompt([
{ type: 'input', name: 'folder', message: 'Sub-folder name?', default: query },
{
type: 'list',
name: 'domain',
message: 'Which language?',
choices: [
'English',
'German',
'French',
'Spanish',
'Italian',
'Indian',
'Polish',
'Romanian',
'Turkish',
],
filter: (val) => {
switch (val) {
case 'English':
return 'com';
case 'German':
return 'de';
case 'French':
return 'fr';
case 'Spanish':
return 'es';
case 'Italian':
return 'it';
case 'Indian':
return 'co.in';
case 'Polish':
return 'pl';
case 'Romanian':
return 'ro';
case 'Turkish':
return 'com.tr';
}
},
},
{
type: 'list',
name: 'isz',
message: 'Which size?',
choices: ['large', 'medium'],
filter: (val) => val[0],
},
{
type: 'list',
name: 'itp',
message: 'What type?',
choices: ['photo', 'clipart', 'lineart', 'face'],
},
{
type: 'list',
name: 'ic',
message: 'Color type?',
choices: ['color', 'gray'], // 'trans'],
},
]);
const { confirmed } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmed',
message: `Start downloading ${qtty} ${query} images from google.${domain}. Ok?`,
default: true,
},
]);
if (!confirmed) {
console.log(chalk.red('Cancelled!'));
process.exit(0);
}
const google = new Scraper({
puppeteer: {
headless: false,
},
userAgent: 'Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0',
tbs,
});
const results = await google.scrape(query.trim(), +qtty, domain);
let processed = 0;
let skipped = 0;
if (results.length > 0) {
console.log(chalk.green(`${results.length} results found, downloading jpg files...`));
let path = `${baseFolder}`;
if (!!folder.trim()) path = `${path}/${folder.trim()}`;
await mkdirp(path);
for (let result of results) {
const ext = result.url.split('.').pop();
if (ext === 'jpg' || ext === 'jpeg') {
const filename = md5(result.url) + '.jpg';
const ws = fs.createWriteStream(`${path}/${filename}`);
ws.on('error', (err) => {
console.log(chalk.red(err.message));
process.exit(1);
});
ws.on('finish', () => {
console.log(chalk.blue(`Downloaded from ${result.url}`));
processed += 1;
if (processed === +qtty) {
console.log(chalk.green(`All done! ${processed - skipped} files downloaded.`));
if (skipped > 0)
console.log(chalk.yellow(`Skipped ${skipped} files (non-jpeg images).`));
}
});
const req = request(result.url);
req.pipe(ws);
} else {
processed += 1;
skipped += 1;
}
}
} else {
console.log(chalk.red('Nothing found.'));
}
} catch (e) {
if (e.isTtyError) {
console.log(chalk.red('Unsupported terminal.'));
} else {
console.log(e);
}
}
};
module.exports = main;