From 93aa4f8aca2b1d8db6b97cf90c0156c6877819b6 Mon Sep 17 00:00:00 2001 From: Amelia <9247739-limesey@users.noreply.gitlab.com> Date: Mon, 6 Jun 2022 14:17:12 +0100 Subject: [PATCH] Create contest route && modify files to reflect changes made in the creation of the new endpoint --- src/scrapers/euromilhoes.ts | 181 +++++++++++------- src/server/routes/api/euromilhoes/contest.ts | 33 ++++ src/server/routes/api/euromilhoes/contests.ts | 9 +- .../routes/api/euromilhoes/euromilhoes.ts | 11 +- 4 files changed, 160 insertions(+), 74 deletions(-) create mode 100644 src/server/routes/api/euromilhoes/contest.ts diff --git a/src/scrapers/euromilhoes.ts b/src/scrapers/euromilhoes.ts index 9bd6e95..9439e63 100644 --- a/src/scrapers/euromilhoes.ts +++ b/src/scrapers/euromilhoes.ts @@ -84,76 +84,121 @@ function getContests($: cheerio.CheerioAPI) { /** */ function storeData() {} -/** */ -async function scrapeWebpage() { - return new Promise(function (resolve, reject) { - /* const [resolved, result] = await handlePromise(axios.get(EUROMILHOES_URL)); +function getContestPage(contestId?: string) { + return new Promise(async function (resolve, reject) { + if (contestId != undefined) { + const [resolved, result] = await handlePromise( + axios.post(EUROMILHOES_URL + `?selectContest=${contestId}`) + ); - if (resolved == false) { - console.error(result); + if (resolved == true) { + resolve(result); + } else { + console.error(result); + reject(result); + } + } else { + //??? + const [resolved, result] = await handlePromise( + axios.get(EUROMILHOES_URL) + ); - // Return no results - console.error(result); - return; - } - - 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); + if (resolved == true) { + resolve(result); + } else { + reject(result); + } + } }); } -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 }; diff --git a/src/server/routes/api/euromilhoes/contest.ts b/src/server/routes/api/euromilhoes/contest.ts new file mode 100644 index 0000000..b023581 --- /dev/null +++ b/src/server/routes/api/euromilhoes/contest.ts @@ -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; diff --git a/src/server/routes/api/euromilhoes/contests.ts b/src/server/routes/api/euromilhoes/contests.ts index e1152c4..bbe003c 100644 --- a/src/server/routes/api/euromilhoes/contests.ts +++ b/src/server/routes/api/euromilhoes/contests.ts @@ -1,15 +1,18 @@ import { Router } from "express"; -import scrapeWebpage from "../../../../scrapers/euromilhoes"; +import { + getContestPage, + scrapeWebpage, +} from "../../../../scrapers/euromilhoes"; import handlePromise from "../../../../util/promiseHandler"; const contestsRouter = Router(); contestsRouter.get("/", async (req, res) => { - const [resolved, result] = await handlePromise(scrapeWebpage()); + const [resolved, result] = await handlePromise(getContestPage()); if (resolved == true) { - res.json(result.contests); + res.json(scrapeWebpage(result.data).contests); } else { console.error(result); res.send(500); diff --git a/src/server/routes/api/euromilhoes/euromilhoes.ts b/src/server/routes/api/euromilhoes/euromilhoes.ts index f0c42cc..4bd2696 100644 --- a/src/server/routes/api/euromilhoes/euromilhoes.ts +++ b/src/server/routes/api/euromilhoes/euromilhoes.ts @@ -1,16 +1,20 @@ import { Router } from "express"; import handlePromise from "../../../../util/promiseHandler"; -import scrapeWebpage from "../../../../scrapers/euromilhoes"; +import { + scrapeWebpage, + getContestPage, +} from "../../../../scrapers/euromilhoes"; import contestsRouter from "../euromilhoes/contests"; +import contestRouter from "../euromilhoes/contest"; const euromilhoesRouter = Router(); euromilhoesRouter.get("/", async (req, res) => { - const [resolved, result] = await handlePromise(scrapeWebpage()); + const [resolved, result] = await handlePromise(getContestPage()); if (resolved == true) { - res.json(result); + res.json(scrapeWebpage(result.data)); } else { console.error(result); res.sendStatus(500); @@ -18,5 +22,6 @@ euromilhoesRouter.get("/", async (req, res) => { }); euromilhoesRouter.use("/contests", contestsRouter); +euromilhoesRouter.use("/contest", contestRouter); export default euromilhoesRouter;