Create contest route && modify files to reflect changes made in the creation of the new endpoint

This commit is contained in:
Amelia 2022-06-06 14:17:12 +01:00
parent 81d5ad5ede
commit 93aa4f8aca
4 changed files with 160 additions and 74 deletions

View File

@ -84,76 +84,121 @@ function getContests($: cheerio.CheerioAPI) {
/** */ /** */
function storeData() {} function storeData() {}
/** */ function getContestPage(contestId?: string) {
async function scrapeWebpage() { return new Promise(async function (resolve, reject) {
return new Promise(function (resolve, reject) { if (contestId != undefined) {
/* const [resolved, result] = await handlePromise(axios.get(EUROMILHOES_URL)); const [resolved, result] = await handlePromise(
axios.post(EUROMILHOES_URL + `?selectContest=${contestId}`)
);
if (resolved == false) { if (resolved == true) {
console.error(result); resolve(result);
} else {
console.error(result);
reject(result);
}
} else {
//???
const [resolved, result] = await handlePromise(
axios.get(EUROMILHOES_URL)
);
// Return no results if (resolved == true) {
console.error(result); resolve(result);
return; } else {
} reject(result);
}
fs.writeFileSync("index.html", result.data); */ }
const result = fs.readFileSync("index.html");
const $ = cheerio.load(result);
getContests($);
const keyLists = $(".betMiddle.twocol.regPad ul li");
const orderedKey = getKey(keyLists.first());
const sortedKey = getKey(keyLists.eq(1));
const prizesDiv = $(".stripped.betMiddle.customfiveCol.regPad ul");
const prizes: Prize[] = [];
prizesDiv.each(function (i, elem) {
const column = $(this).find("li");
const rightGuessesRow = column.eq(1).text();
// TODO: Learn Regex and use it here
const rightGuessesSplit = rightGuessesRow.split("+");
const rightnumbers = rightGuessesSplit[0].split(" ")[0];
const rightStars = rightGuessesSplit[1].split(" ")[1];
const portugalWinners = column.eq(2).text();
const totalWinners = column.eq(3).text();
const prize = {
placement: (i + 1).toString(),
requiredNumberGuesses: rightnumbers,
requiredStarGuesses: rightStars,
winners: {
portugal: portugalWinners,
total: totalWinners,
},
};
prizes.push(prize);
});
const data: EurmomilhoesData = {
keys: {
orderedKey: orderedKey,
sortedKey: sortedKey,
},
prizes: prizes,
contests: getContests($),
};
resolve(data);
}); });
} }
export default scrapeWebpage; /** */
function scrapeWebpage(content: string | Buffer): EurmomilhoesData {
// fs.writeFileSync("index.html", result.data); */
//const result = fs.readFileSync("index.html");
let data: EurmomilhoesData = {
keys: {
orderedKey: {
numbers: [],
stars: [],
},
sortedKey: {
numbers: [],
stars: [],
},
},
prizes: [],
contests: [],
};
const $ = cheerio.load(content);
getContests($);
const keyLists = $(".betMiddle.twocol.regPad ul li");
if (keyLists.length == 0) {
return data;
}
const orderedKey = getKey(keyLists.first());
const sortedKey = getKey(keyLists.eq(1));
const prizesDiv = $(".stripped.betMiddle.customfiveCol.regPad ul");
const prizes: Prize[] = [];
prizesDiv.each(function (i, elem) {
const column = $(this).find("li");
const rightGuessesRow = column.eq(1).text();
// TODO: Learn Regex and use it here
const rightGuessesSplit = rightGuessesRow.split("+");
const rightnumbers = rightGuessesSplit[0].split(" ")[0];
const rightStars = rightGuessesSplit[1].split(" ")[1];
const portugalWinners = column.eq(2).text();
const totalWinners = column.eq(3).text();
const prize = {
placement: (i + 1).toString(),
requiredNumberGuesses: rightnumbers,
requiredStarGuesses: rightStars,
winners: {
portugal: portugalWinners,
total: totalWinners,
},
};
prizes.push(prize);
});
data.keys = {
orderedKey: orderedKey,
sortedKey: sortedKey,
};
data.prizes = prizes;
data.contests = getContests($);
data = {
keys: {
orderedKey: orderedKey,
sortedKey: sortedKey,
},
prizes: prizes,
contests: getContests($),
};
return data;
}
export { getContestPage, scrapeWebpage };

View File

@ -0,0 +1,33 @@
import { Router } from "express";
import {
scrapeWebpage,
getContestPage,
} from "../../../../scrapers/euromilhoes";
import handlePromise from "../../../../util/promiseHandler";
const contestRouter = Router();
contestRouter.get("/", async (req, res) => {
const contestId = req.query.contestId;
if (contestId != undefined) {
let [resolved, result] = await handlePromise(
getContestPage(contestId.toString())
);
if (resolved == false) {
res.sendStatus(result.status);
return;
}
const data = scrapeWebpage(result.data);
res.json(data);
} else {
res.sendStatus(400);
}
});
export default contestRouter;

View File

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

View File

@ -1,16 +1,20 @@
import { Router } from "express"; import { Router } from "express";
import handlePromise from "../../../../util/promiseHandler"; import handlePromise from "../../../../util/promiseHandler";
import scrapeWebpage from "../../../../scrapers/euromilhoes"; import {
scrapeWebpage,
getContestPage,
} from "../../../../scrapers/euromilhoes";
import contestsRouter from "../euromilhoes/contests"; import contestsRouter from "../euromilhoes/contests";
import contestRouter from "../euromilhoes/contest";
const euromilhoesRouter = Router(); const euromilhoesRouter = Router();
euromilhoesRouter.get("/", async (req, res) => { euromilhoesRouter.get("/", async (req, res) => {
const [resolved, result] = await handlePromise(scrapeWebpage()); const [resolved, result] = await handlePromise(getContestPage());
if (resolved == true) { if (resolved == true) {
res.json(result); res.json(scrapeWebpage(result.data));
} else { } else {
console.error(result); console.error(result);
res.sendStatus(500); res.sendStatus(500);
@ -18,5 +22,6 @@ euromilhoesRouter.get("/", async (req, res) => {
}); });
euromilhoesRouter.use("/contests", contestsRouter); euromilhoesRouter.use("/contests", contestsRouter);
euromilhoesRouter.use("/contest", contestRouter);
export default euromilhoesRouter; export default euromilhoesRouter;