Retrieve contest list && IDs from webpage. Create API endpoint to retrieve only the list of contests. Remove unnecessary debug logging

This commit is contained in:
Amelia 2022-06-06 11:49:10 +01:00
parent c24794be40
commit 4db5786579
3 changed files with 51 additions and 17 deletions

View File

@ -30,9 +30,15 @@ type Prize = {
};
};
type Contest = {
id: string;
title: string;
};
type EurmomilhoesData = {
keys: Keys;
prizes: Prize[];
contests: Contest[];
};
function getKey(element: cheerio.Cheerio<cheerio.Element>) {
@ -44,7 +50,6 @@ function getKey(element: cheerio.Cheerio<cheerio.Element>) {
// Not sure why, but an empty string is being inserted into the array
numbers.forEach((value, i) => {
console.log(value == "");
if (value == "") {
numbers.splice(i, 1);
}
@ -56,11 +61,26 @@ function getKey(element: cheerio.Cheerio<cheerio.Element>) {
}
});
console.log(stars);
return { numbers: numbers, stars: stars };
}
/**
* Scrape contests data from the selection menu
*/
function getContests($: cheerio.CheerioAPI) {
const selectionMenuOptions = $("select[name=selectContest]").find("option");
const contests: Contest[] = [];
selectionMenuOptions.each(function (elem, i) {
const id = $(this).attr("value")!;
const title = $(this).text();
contests.push({ id: id, title: title });
});
return contests;
}
/** */
function storeData() {}
@ -83,23 +103,13 @@ async function scrapeWebpage() {
const $ = cheerio.load(result);
getContests($);
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();
let splitKey = key.split("+");
const orderedKey = keyLists.eq(1).text();
splitKey = orderedKey.split("+");
const orderedKeyNumbers = splitKey[0].split(" ");
const orderedKeyStars = splitKey[1].split(" "); */
const prizesDiv = $(".stripped.betMiddle.customfiveCol.regPad ul");
const prizes: Prize[] = [];
@ -138,6 +148,8 @@ async function scrapeWebpage() {
},
prizes: prizes,
contests: getContests($),
};
resolve(data);

View File

@ -0,0 +1,19 @@
import { Router } from "express";
import scrapeWebpage from "../../../../scrapers/euromilhoes";
import handlePromise from "../../../../util/promiseHandler";
const contestsRouter = Router();
contestsRouter.get("/", async (req, res) => {
const [resolved, result] = await handlePromise(scrapeWebpage());
if (resolved == true) {
res.json(result.contests);
} else {
console.error(result);
res.send(500);
}
});
export default contestsRouter;

View File

@ -1,7 +1,8 @@
import { Router } from "express";
import handlePromise from "../../../util/promiseHandler";
import scrapeWebpage from "../../../scrapers/euromilhoes";
import handlePromise from "../../../../util/promiseHandler";
import scrapeWebpage from "../../../../scrapers/euromilhoes";
import contestsRouter from "../euromilhoes/contests";
const euromilhoesRouter = Router();
@ -16,4 +17,6 @@ euromilhoesRouter.get("/", async (req, res) => {
}
});
euromilhoesRouter.use("/contests", contestsRouter);
export default euromilhoesRouter;