-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (98 loc) · 2.9 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
const fetch = require('node-fetch');
const fs = require('fs');
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
var http = require("http");
// setInterval(function() {
// http.get("http://donde-estan-mis-cupos-uniandes.herokuapp.com/");
// }, 300000);
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var refresco = {};
var arregloPrint = [];
app.get('/', function (req, res) {
let nrc = req.query.nrc;
try {
let lectura = JSON.parse(fs.readFileSync('cache.json', 'utf8'));
let buscado = lectura.find(x => x[0] == nrc);
if (buscado !== undefined) {
res.send(buscado);
}
else {
res.send(`
<span style='color:#cc0000;'>Ingresa un NRC válido. (Estamos caídos por un cambio en registro, estamos trabajando para resolverlo)
</span>
<p>
<br>
<center>
<img src='https://media.giphy.com/media/mq5y2jHRCAqMo/giphy.gif' style='width:180px;'></center>
</p>
`);
}
}
catch (error) {
res.send(`
<span style='color:#cc0000;'>Lo sentimos, ocurrió un error recuperando los cupos.
<br>
Intenta de nuevo porfavor!
</span>
<p>
<br>
<center>
<img src='https://media.giphy.com/media/KlrMS4vyq5KSY/giphy.gif' style='width:180px;'></center>
</p>
`);
}
});
/**
* Realiza la escritura del json cacheado con cupos actualizados
*/
function pintarJson() {
fs.writeFile('cache.json', JSON.stringify(arregloPrint), 'utf8',
x => {
if (x) {
return console.log(err);
}
});
console.log('PINTADO');
}
app.listen(port, function () {
console.log('App listening on port ' + port);
});
/**
* llama el API :v
*/
function llamarAPI()
{
arregloPrint = [];
refresco = {};
fetch('https://senecacupos.herokuapp.com/')
.then(ans => ans.text())
.then(body => {
refresco = JSON.parse(body.trim());
if (refresco === undefined || refresco.records.length == 0 ) {
console.log("SOMETHING'S WRONG...");
let objeto = ['','',''];
arregloPrint.push(objeto);
pintarJson();
}
else{
refresco.records.forEach(element => {
//[nrc,capacidad,disponible]
let objeto = [element.nrc, element.limit, element.cupos];
arregloPrint.push(objeto);
});
pintarJson();
console.log('DONE');
}
})
.catch(x => console.log(x));
}
llamarAPI();
setInterval(()=>{
llamarAPI();
}, 60000)