lot/libs/retriever.js

43 lines
928 B
JavaScript
Raw Normal View History

2021-01-02 19:30:30 +00:00
const jsonfile = require("jsonfile");
2019-02-08 14:54:18 +00:00
2021-01-02 19:30:30 +00:00
const fetch = require("node-fetch");
const fecha = require("fecha");
2019-02-08 14:54:18 +00:00
2021-01-02 19:30:30 +00:00
const { scrapeResults } = require("./scraper");
2019-02-08 14:54:18 +00:00
2021-01-02 19:30:30 +00:00
const file = "data/data.json";
2019-02-08 14:54:18 +00:00
// https://www.euro-millions.com/results
function retrieveNew() {
2021-01-02 19:30:30 +00:00
console.log("retrieveNew...");
2019-02-08 14:54:18 +00:00
const now = new Date().getTime();
// https://www.euro-millions.com/results/05-02-2019
2021-01-02 19:30:30 +00:00
const yesterday = new Date(now - 8.64e7);
2019-02-08 14:54:18 +00:00
2021-01-02 19:30:30 +00:00
const ystring = fecha.format(yesterday, "/DD-MM-YYYY");
2019-02-08 14:54:18 +00:00
const lotData = jsonfile.readFileSync(file);
2019-02-19 15:57:16 +00:00
const u = `https://www.euro-millions.com/results${ystring}`;
2019-02-08 14:54:18 +00:00
2021-01-02 19:30:30 +00:00
console.log("retrieving:", u);
2019-02-19 15:57:16 +00:00
// https://www.euro-millions.com/results/15-02-2019
fetch(u)
2021-01-02 19:30:30 +00:00
.then((res) => res.text())
.then((body) => {
2019-02-08 14:54:18 +00:00
const m = scrapeResults(body);
console.log(m);
lotData.unshift(m);
jsonfile.writeFileSync(file, lotData);
});
}
module.exports = { retrieveNew };