Add error printing && fix key scraping. Added function to scrape keys, in order to clean code

This commit is contained in:
Amelia 2022-06-05 18:05:12 +01:00
parent e88a6b2d0a
commit 4391df8c1a
2 changed files with 25 additions and 4 deletions

View File

@ -9,8 +9,8 @@ const EUROMILHOES_URL =
"https://www.jogossantacasa.pt/web/SCCartazResult/euroMilhoes"; "https://www.jogossantacasa.pt/web/SCCartazResult/euroMilhoes";
type Key = { type Key = {
numbers: string; numbers: string[];
stars: string; stars: string[];
}; };
type Keys = { type Keys = {
@ -35,6 +35,16 @@ type EurmomilhoesData = {
prizes: Prize[]; prizes: Prize[];
}; };
function getKey(element: cheerio.Cheerio<cheerio.Element>) {
const elementText = element.text().trim();
let splitKey = elementText.split("+");
const numbers = splitKey[0].split(" ");
const stars = splitKey[1].split(" ");
return { numbers: numbers, stars: stars };
}
/** */ /** */
function storeData() {} function storeData() {}
@ -58,12 +68,22 @@ async function scrapeWebpage() {
const $ = cheerio.load(result); const $ = cheerio.load(result);
const keyLists = $(".betMiddle.twocol.regPad ul li"); const keyLists = $(".betMiddle.twocol.regPad ul li");
const orderedKey = getKey(keyLists.first());
const sortedKey = getKey(keyLists.eq(1));
/* console.log(keyLists.first().text().trim());
console.log(keyLists.eq(1).text().trim());
const key = keyLists.first().text().trim(); const key = keyLists.first().text().trim();
let splitKey = key.split("+"); let splitKey = key.split("+");
const orderedKey = keyLists.eq(1).text(); const orderedKey = keyLists.eq(1).text();
splitKey = orderedKey.split("+"); splitKey = orderedKey.split("+");
const orderedKeyNumbers = splitKey[0].split(" ");
const orderedKeyStars = splitKey[1].split(" "); */
const prizesDiv = $(".stripped.betMiddle.customfiveCol.regPad ul"); const prizesDiv = $(".stripped.betMiddle.customfiveCol.regPad ul");
const prizes: Prize[] = []; const prizes: Prize[] = [];
@ -97,8 +117,8 @@ async function scrapeWebpage() {
const data: EurmomilhoesData = { const data: EurmomilhoesData = {
keys: { keys: {
orderedKey: { numbers: "0", stars: "0" }, orderedKey: orderedKey,
sortedKey: { numbers: "0", stars: "0" }, sortedKey: sortedKey,
}, },
prizes: prizes, prizes: prizes,

View File

@ -11,6 +11,7 @@ euromilhoesRouter.get("/", async (req, res) => {
if (resolved == true) { if (resolved == true) {
res.json(result); res.json(result);
} else { } else {
console.error(result);
res.sendStatus(500); res.sendStatus(500);
} }
}); });