-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstart.js
129 lines (109 loc) · 3.93 KB
/
start.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
const axios = require('axios');
const fs = require('fs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csv = require('csv-parser');
const cmlog = require('cmlog');
const _ = require('lodash');
const download = require('image-downloader');
const config = require('./config.json');
const package = require('./package.json');
const folderPatch = process.cwd();
const cryptos = [];
const filesIconsExists = [];
const blacklist = [];
fs.createReadStream('blacklist.csv')
.pipe(csv())
.on('data', (data) => {
blacklist.push(data.NAME);
});
try {
files = fs.readdirSync(`${folderPatch}/icons/`, { withFileTypes: true });
files.forEach(function (result) {
filesIconsExists.push(result.name.substr(0, result.name.length - 4));
});
} catch (err) {
cmlog.error(new Error(err));
}
cmlog.start(`coinmarketcap-icons-cryptos V${package.version} => Start generator`);
if (!_.isEmpty(config.apikey)) {
axios
.get('https://pro-api.coinmarketcap.com/v1/cryptocurrency/map', {
headers: { 'X-CMC_PRO_API_KEY': config.apikey }
})
.then((res) => {
res.data.data.map((crypto, index) => {
cryptos.push(_.lowerCase(crypto.symbol));
});
})
.then(() => {
const cryptosData = _.filter(
cryptos,
(o) =>
!_.includes(filesIconsExists, o.replace(/\s+/g, '')) &&
!_.includes(blacklist, o.replace(/\s+/g, ''))
);
cmlog.success(`Retrieving the list of cryptos Total: [${cryptosData.length}]`);
if (cryptosData && cryptosData.length === 0) {
cmlog.done('All icons have been updated !');
} else {
cryptosData.forEach((crypto, index) => {
let cryptoName = crypto;
if (cryptoName.indexOf(' ') >= 0) {
cryptoName = crypto.replace(/\s+/g, '');
}
setTimeout(() => {
axios
.get(
`https://pro-api.coinmarketcap.com/v1/cryptocurrency/info?symbol=${cryptoName}`,
{
headers: { 'X-CMC_PRO_API_KEY': config.apikey }
}
)
.then((o) => {
const imageUrl = _.values(o.data.data)[0].logo;
const options = {
url: imageUrl,
dest: `${folderPatch}/icons/${cryptoName}.png`
};
download
.image(options)
.then(({ filename }) => {
cmlog.success(`Icon saved ${cryptoName} => ${filename}`);
cmlog.waitting(`PROGRESS [${index + 1}/${cryptosData.length}]`);
})
.catch((err) => cmlog.error(new Error(err)));
})
.catch((err) => {
cmlog.error(new Error(`Error => "${cryptoName} ${err}"`));
//&& err.response && err.response.status === 400
if (err) {
cmlog.error(new Error(`Crypto icon not found => "${cryptoName}"`));
const csvWriter = createCsvWriter({
path: 'blacklist.csv',
header: [{ id: 'name', title: 'NAME' }],
append: true
});
const records = [{ name: cryptoName }];
csvWriter
.writeRecords(records)
.then(() => {
cmlog.info(`Crypto add blacklist => ${cryptoName}"`);
})
.catch((err) => {
cmlog.error(new Error(`Error write icon in blacklist => "${cryptoName}"`));
});
}
});
if (index + 1 === cryptosData.length) {
cmlog.done('All icons have been updated !');
}
}, 4000 * (index + 1));
});
}
})
.catch((err) => {
cmlog.error(new Error(err));
});
} else {
cmlog.error(new Error('No API key entered in config.json !'));
}