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,29 +84,67 @@ 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) {
if (resolved == true) {
resolve(result);
} else {
console.error(result);
// Return no results
console.error(result);
return;
reject(result);
}
} else {
//???
const [resolved, result] = await handlePromise(
axios.get(EUROMILHOES_URL)
);
fs.writeFileSync("index.html", result.data); */
if (resolved == true) {
resolve(result);
} else {
reject(result);
}
}
});
}
const result = fs.readFileSync("index.html");
/** */
function scrapeWebpage(content: string | Buffer): EurmomilhoesData {
// fs.writeFileSync("index.html", result.data); */
const $ = cheerio.load(result);
//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));
@ -141,19 +179,26 @@ async function scrapeWebpage() {
prizes.push(prize);
});
const data: EurmomilhoesData = {
data.keys = {
orderedKey: orderedKey,
sortedKey: sortedKey,
};
data.prizes = prizes;
data.contests = getContests($);
data = {
keys: {
orderedKey: orderedKey,
sortedKey: sortedKey,
},
prizes: prizes,
contests: getContests($),
};
resolve(data);
});
return data;
}
export default scrapeWebpage;
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 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);

View File

@ -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;