-
Notifications
You must be signed in to change notification settings - Fork 0
/
dolar-scrapper.js
executable file
·255 lines (222 loc) · 8.14 KB
/
dolar-scrapper.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const requestPromise = require('request-promise');
const cheerio = require('cheerio');
const yargs = require('yargs');
const DraftLog = require('draftlog'); DraftLog.into(console);
const colors = require('colors');
const promiseRetry = require('promise-retry');
const SPINNER_CHARS = '⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷'.split(' ');
const argv = yargs
.option('ars', {
alias: 'p',
description: 'Amount of ARS to convert to USD',
type: 'number',
})
.option('usd', {
alias: 'd',
description: 'Amount of USD to convert to ARS',
type: 'number',
})
.option('al30', {
description: 'Unit price of buyed al30 to see real conversion',
type: 'number',
})
.option('refresh', {
alias: 'r',
describe: 'Refresh every {r} seconds',
type: 'number'
})
.help()
.alias('help', 'h')
.argv;
class Retriever {
retryCallback = () => {};
name() { throw Error('Must be implemented') }
retrieve() { throw Error('Must be implemented') }
onRetry(callBack) { this.retryCallback = callBack; }
retry(promise) {
return promiseRetry((retry, number) => {
this.retryCallback(number);
return promise.catch(retry);
}, {retries: 3});
}
}
class IOLRetriever extends Retriever {
retrieveValue(url) {
return this.retry(requestPromise(url))
.then(html => {
const $ = cheerio.load(html);
return $("#IdTitulo > span[data-field='UltimoPrecio']").text()
})
.then(value => {
const dotValue = value.replace('.', '').replace(',', '.');
return parseFloat(dotValue);
});
}
getUrls() {
throw Error('Must be implemented and returns {ars, usd}');
}
retrieveMep() {
const {ars, usd} = this.getUrls();
const titlePromise = this.retrieveValue(ars);
const titleDPromise = this.retrieveValue(usd);
return Promise.all([titlePromise, titleDPromise])
.then(([title, titleD]) => {
const mep = {title, titleD, total: title/titleD};
return {value: mep.total, pretty: `${mep.total.toFixed(2)} [${mep.title} / ${mep.titleD}]`, extra: {ars: mep.title, usd: mep.titleD}};
});
}
retrieve() {
return this.retrieveMep();
}
}
class AY24Retriever extends IOLRetriever {
name() { return 'AY24'; }
getUrls() {
return {
ars: "https://www.invertironline.com/titulo/cotizacion/BCBA/AY24/",
usd: "https://www.invertironline.com/titulo/cotizacion/BCBA/AY24D/"
}
}
}
class AL30Retriever extends IOLRetriever {
name() { return 'AL30'; }
getUrls() {
return {
ars: "https://www.invertironline.com/titulo/cotizacion/BCBA/AL30/BONO-REP.-ARGENTINA-USD-STEP-UP-2030/",
usd: "https://www.invertironline.com/titulo/cotizacion/BCBA/AL30D/BONO-REP.-ARGENTINA-USD-STEP-UP-2030/"
}
}
async retrieve() {
const data = await super.retrieve();
if (argv.al30) {
const value = argv.al30/data.extra.usd;
const actualValue = `${data.value.toFixed(2)}`;
const pretty = `${value.toFixed(2)} [${argv.al30} / ${data.extra.usd}] {${actualValue}}`;
return {...data, pretty, value};
}
return data;
}
}
class BlueRetriever extends Retriever {
name() { return 'BLUE' }
retrieve() {
return this.retry(requestPromise("https://api-contenidos.lanacion.com.ar/json/V3/economia/cotizacionblue/DBLUE"))
.then(response => {
const blueData = JSON.parse(response);
const value = parseFloat(blueData.venta.replace(',', '.'));
return {value, pretty: `${value.toFixed(2)}`};
});
}
}
class OfficialRetriever extends Retriever {
name() { return 'BNA'; }
retrieve() {
return this.retry(requestPromise("https://www.bna.com.ar/Personas"))
.then(html => {
const $ = cheerio.load(html);
return $("#billetes > table > tbody > tr:nth-child(1) > td:nth-child(3)").text();
})
.then(valueString => {
//console.log(value);
const value = parseFloat(valueString.replace(',', '.'));
return { value, pretty: `${value} (${value * (1.3 + 0.35)})` }
});
}
}
class DAIRetriever extends Retriever {
name() { return 'DAI' }
retrieve() {
return this.retry(requestPromise("https://be.buenbit.com/api/market/tickers/"))
.then(res => {
const data = JSON.parse(res);
const arsdai = parseFloat(data.object.daiars.selling_price);
const daiusd = parseFloat(data.object.daiusd.purchase_price);
const daiToUsd = arsdai/daiusd;
return { value: daiToUsd, pretty: `${daiToUsd.toFixed(2)}` };
});
}
}
class USDCRetriever extends Retriever {
name() { return 'USDC'; }
retrieve() {
return this.retry(requestPromise('https://app.ripio.com/api/v3/rates/?country=AR'))
.then(res => {
const data = JSON.parse(res);
const usdc = data.find(a => a.ticker === 'USDC_ARS');
return usdc && usdc.buy_rate ? { value: usdc.buy_rate, pretty: `${usdc.buy_rate} {${usdc.variation}}` } : { value: -1, pretty: `-1` };
});
}
}
class ConsoleLine {
message = '';
consoleUpdated = () => {};
lap = 0;
timeout = null;
constructor(message) {
this.message = message;
this.consoleUpdated = console.draft(message);
}
setMessage(message) {
this.message = message;
this.consoleUpdated(this.message);
}
endSpinner(message) {
this.activeSpinner = false;
clearTimeout(this.timeout);
this.setMessage(message ? message : this.message);
}
async startSpinner() {
this.lap = 0;
this.activeSpinner = true;
while (this.activeSpinner) {
this.renderSpinner();
await new Promise((accept) => { this.timeout = setTimeout(() => accept(), 100); });
}
}
renderSpinner() {
const spinner = `${SPINNER_CHARS[this.lap++ % SPINNER_CHARS.length]}`.bold.green;
this.consoleUpdated(`${this.message} ${spinner}`);
}
}
async function instanceRetrieve(name, instance, consoleLine) {
instance.onRetry((number) => {
if (number > 1) {
consoleLine.setMessage(`${name}| Retrying ${number}`.bold.yellow);
}
});
consoleLine.startSpinner();
return instance.retrieve()
.then(data => {
let message = `${name}| ${data.pretty}`;
if (argv.ars) {
const conversion = `${(parseFloat(argv.ars)/data.value).toFixed(2)}`.bold.green;
message = `${message.padEnd(50, ' ')}| ARS -> USD: ${conversion}`;
}
if (argv.usd) {
const conversion = `${(parseFloat(argv.usd) * data.value).toFixed(2)}`.bold.blue;
message = `${message.padEnd(90, ' ')}| USD -> ARS: ${conversion}`;
}
consoleLine.endSpinner(message);
})
.catch(err => consoleLine.endSpinner(`${name}| ERR: ${err.message}`.bold.red));
}
async function retrieveAndLog(retrieverClass) {
const instance = new retrieverClass();
const name = instance.name().padEnd(5, ' ');
const consoleLine = new ConsoleLine(`${name}| Loading`);
if (!argv.refresh) {
return instanceRetrieve(name, instance, consoleLine);
}
while (true) {
await instanceRetrieve(name, instance, consoleLine);
await new Promise((accept) => setTimeout(() => accept(), argv.refresh * 1000));
}
}
async function show() {
const retrievers = [AY24Retriever, AL30Retriever, BlueRetriever, OfficialRetriever, DAIRetriever, USDCRetriever];
const retrievePromises = retrievers.map(retrieverClass => {
return retrieveAndLog(retrieverClass);
});
return Promise.all(retrievePromises);
}
show().catch(err => console.log(err));