Add contest ID in the data returned from the webscraper. Use selector to find option instead of using .find()

This commit is contained in:
Amelia 2022-06-06 15:03:17 +01:00
parent 93aa4f8aca
commit 1f8929e8e7
1 changed files with 7 additions and 2 deletions

View File

@ -36,6 +36,7 @@ type Contest = {
}; };
type EurmomilhoesData = { type EurmomilhoesData = {
id: string;
keys: Keys; keys: Keys;
prizes: Prize[]; prizes: Prize[];
contests: Contest[]; contests: Contest[];
@ -68,7 +69,7 @@ function getKey(element: cheerio.Cheerio<cheerio.Element>) {
* Scrape contests data from the selection menu * Scrape contests data from the selection menu
*/ */
function getContests($: cheerio.CheerioAPI) { function getContests($: cheerio.CheerioAPI) {
const selectionMenuOptions = $("select[name=selectContest]").find("option"); const selectionMenuOptions = $("select[name=selectContest] option");
const contests: Contest[] = []; const contests: Contest[] = [];
selectionMenuOptions.each(function (elem, i) { selectionMenuOptions.each(function (elem, i) {
@ -98,7 +99,6 @@ function getContestPage(contestId?: string) {
reject(result); reject(result);
} }
} else { } else {
//???
const [resolved, result] = await handlePromise( const [resolved, result] = await handlePromise(
axios.get(EUROMILHOES_URL) axios.get(EUROMILHOES_URL)
); );
@ -119,6 +119,7 @@ function scrapeWebpage(content: string | Buffer): EurmomilhoesData {
//const result = fs.readFileSync("index.html"); //const result = fs.readFileSync("index.html");
let data: EurmomilhoesData = { let data: EurmomilhoesData = {
id: "",
keys: { keys: {
orderedKey: { orderedKey: {
numbers: [], numbers: [],
@ -145,6 +146,9 @@ function scrapeWebpage(content: string | Buffer): EurmomilhoesData {
return data; return data;
} }
const selectedContest = $("select[name=selectContest] option");
const contestId = selectedContest.first().attr("value");
const orderedKey = getKey(keyLists.first()); const orderedKey = getKey(keyLists.first());
const sortedKey = getKey(keyLists.eq(1)); const sortedKey = getKey(keyLists.eq(1));
@ -189,6 +193,7 @@ function scrapeWebpage(content: string | Buffer): EurmomilhoesData {
data.contests = getContests($); data.contests = getContests($);
data = { data = {
id: contestId!,
keys: { keys: {
orderedKey: orderedKey, orderedKey: orderedKey,
sortedKey: sortedKey, sortedKey: sortedKey,