-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
143 lines (119 loc) · 3.45 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
require("dotenv").config();
// const request = require("request-promise-native");
const fs = require("fs");
const path = require("path");
const Promise = require("bluebird");
const url = require("url");
const assert = require("assert");
function parseFilename(adr) {
var q = url.parse(adr, true);
const paths = q.pathname.split("/");
return paths[paths.length - 1];
}
var dir = path.resolve("emojis");
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
const axios = require("axios");
function DownloadFile(name, url) {
return new Promise((resolve, reject) => {
const cancelTimer = setTimeout(() => {
reject(new Error(`${name} failed to download TIMEOUT.`));
}, 5000);
let file = fs.createWriteStream(`${dir}/${name}`);
file.on("finish", () => {
clearTimeout(cancelTimer);
console.log("Downloaded:", name);
return resolve();
});
file.on("error", (e) => {
clearTimeout(cancelTimer);
reject(e);
});
// stream file to disk.
// return request(url).catch(reject).pipe(file);
return axios({ url, responseType: "stream" })
.catch((e) => {
clearTimeout(cancelTimer);
reject(e);
})
.then((res) => {
if(!res?.data) return reject()
return res.data.pipe(file)
});
});
}
async function downloadFileRetry(filename, url, count = 0) {
return DownloadFile(filename, url).catch((e) => {
++count;
assert(count <= 5, "max retries.");
console.error(e);
console.log("failed to download:", filename, url);
return downloadFileRetry(filename, url, count);
});
}
const getPage = (index = 0) => {
return axios({
url: `https://slackmojis.com/emojis.json?page=${index}`,
// json: true,
}).then((response) => response.data);
};
function DownloadImages(list) {
let count = 0;
let failures = 0;
return Promise.map(
list,
async ({ id, name, image_url, category }) => {
const filename = `${id}_${parseFilename(image_url)}`
await downloadFileRetry(filename, image_url);
console.log(`${list.length - ++count} / ${list.length} ${filename}...`);
const metadata = {
id,
filename,
name,
image_url,
slug: `:${name}:`,
category: category?.name || null,
};
// // write JSON string to a file
// fs.appendFile(
// "metadata.json",
// JSON.stringify(metadata, null, 2),
// (err) => {
// if (err) {
// throw err;
// }
// console.log("JSON data is saved.");
// }
// );
return metadata;
},
{ concurrency: Number(process.env.CONCURRENCY) || 25 }
);
}
// recursively download each page until no more are found.
// could buffer results in memory, however this method is more efficient
function Run() {
async function ProcessPage(index = 0) {
console.log("> PROCESSING PAGE:", index);
const list = await getPage(index);
if (!list.length) return;
const downloaded = await DownloadImages(list);
var dir = path.resolve("metadata");
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
// write JSON string to a file
fs.writeFile(
`${dir}/page_${index}.json`,
JSON.stringify(downloaded, null, 2),
(err) => {
if (err) {
throw err;
}
console.log("JSON data is saved.");
}
);
return ProcessPage(++index);
}
return ProcessPage(process.env.PAGE_INDEX);
}
Run().then(() => {
console.log("> DONE.");
});