Create contest route && modify files to reflect changes made in the creation of the new endpoint
This commit is contained in:
parent
81d5ad5ede
commit
93aa4f8aca
|
@ -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 };
|
||||
|
|
|
@ -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;
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue