var tonicExpress = require("@runkit/runkit/express-endpoint/1.0.0")
var got = require('got');
var app = tonicExpress(module.exports)
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
var my_main = async function (req,res) {
if (req.query.magicword!=process.env.MagicWord)
{res.send("Wrong magic word!"); return 0;}
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var ajaxURL = "http://www.cts-strasbourg.eu/system/modules/eu.cts.module.horairetempsreel/actions/action_recherchetempsreel.jsp"+
"?smscode=136&type=0&hour="+h.toString()+
"&minute="+m.toString()+"&nbHoraire=3&locale=fr";
//console.log(ajaxURL);
var CTSreply = await got(ajaxURL, {"json":true});
console.log(CTSreply.body);
var reply = "<ul>";
for (var i in CTSreply.body.listeArrivee)
{
var this_arrivee = CTSreply.body.listeArrivee[i];
if ((this_arrivee.destination.value=="G Gare Centrale") ||
(this_arrivee.destination.value=="19 Rotonde") )
{
//console.log(this_arrivee);
var next_h = parseInt(this_arrivee.horaire.value.split(':')[0]);
var next_m = parseInt(this_arrivee.horaire.value.split(':')[1]);
var delay = ((next_h*60+next_m)-(h*60+m)).toString();
reply+="<li> "+this_arrivee.destination.value+" dans "+
delay+" minutes. </li>";
}
}
reply+="</ul>";
res.send(reply);
};
app.get("/", my_main);