-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (57 loc) · 2.17 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
const fs = require('fs');
const fetch = require('node-fetch');
// NOTE visit node-fetch npm for 3.0.0 and 2.6.1 , 3.0.0 makes it an ESM-only module
// so change stuff in package.json accordingly and here as well
/*function*/ module.exports = function (/*main*/ urls, sep = '\n') {
let dir = './imgout';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
urls = urls.split(sep);
for (let url of urls) {
fetch(url).then((res) => {
res.text().then(async (doc) => {
let root = doc.indexOf('i.ibb.co');
let jpg = doc.indexOf('.jpg');
let name = doc.match(/<title>([^<]*)<\/title>/);
// let name = doc.match(/data-text="image-title">(.*)</)
let imgUrl;
let imgType = 'jpg';
imgUrl = 'https://' + doc.substring(root, jpg + 4);
name = name ? (typeof name[1] === 'string' ? name[1] : imgUrl) : imgUrl;
console.log(name, '<<<');
// these are the formats all formats get converted to in the end
let types = ['jpg', 'png', 'gif', 'webp'];
for (let type of types) {
imgUrl =
'https://' +
doc
.substring(
doc.lastIndexOf('i.ibb.co'),
doc.lastIndexOf('.' + type) + type.length + 1
)
.replace(/%2F/g, '/');
// 129 characters is the current limit to url sizes
// if the matched url is greater than 129, then it is likely we got an incorrect match
if (imgUrl.length > 129) {
continue;
}
imgType = type;
break;
}
// the url is still more than 129 characters long, so tell the user we can't download this one
if (imgUrl.length > 129) {
console.log(`cannot download img at url: ${url}`);
return;
}
let res = await fetch(imgUrl);
const buffer = await res.buffer();
console.log(`writing to ./imgout/${name}.${imgType}`);
fs.writeFile(`./imgout/${name}.${imgType}`, buffer, () => {
console.log('done');
});
});
});
}
};
// module.exports = main