fix: goodjudgmentopen type

This commit is contained in:
NunoSempere 2022-10-09 12:47:25 +01:00
parent e4a3f38ddf
commit 0cddbf69b0

View File

@ -7,6 +7,7 @@ import { applyIfSecretExists } from "../utils/getSecrets";
import {sleep} from "../utils/sleep";
import toMarkdown from "../utils/toMarkdown";
import {FetchedQuestion, Platform} from "./";
import {FullQuestionOption} from "../../common/types";
/* Definitions */
const platformName = "goodjudgmentopen";
@ -35,8 +36,8 @@ async function fetchPage(page: number, cookie: string) {
url: htmlEndPoint + page,
method: "GET",
headers: {
Cookie: cookie,
},
Cookie: cookie
}
}).then((res) => res.data);
// console.log(response)
return response;
@ -49,8 +50,8 @@ async function fetchStats(questionUrl: string, cookie: string) {
headers: {
"Content-Type": "text/html",
Cookie: cookie,
Referer: questionUrl,
},
Referer: questionUrl
}
}).then((res) => res.data);
if (response.includes("Sign up or sign in to forecast")) {
@ -61,9 +62,7 @@ async function fetchStats(questionUrl: string, cookie: string) {
// Parse the embedded json
let htmlElements = response.split("\n");
let jsonLines = htmlElements.filter((element) =>
element.includes("data-react-props")
);
let jsonLines = htmlElements.filter((element) => element.includes("data-react-props"));
let embeddedJsons = jsonLines.map((jsonLine, i) => {
let innerJSONasHTML = jsonLine.split('data-react-props="')[1].split('"')[0];
let json = JSON.parse(innerJSONasHTML.replaceAll(""", '"'));
@ -76,27 +75,11 @@ async function fetchStats(questionUrl: string, cookie: string) {
let numforecasters = firstEmbeddedJson.question.predictors_count;
let numforecasts = firstEmbeddedJson.question.prediction_sets_count;
let questionType = firstEmbeddedJson.question.type;
if (
questionType.includes("Binary") ||
questionType.includes("NonExclusiveOpinionPoolQuestion") ||
questionType.includes("Forecast::Question") ||
!questionType.includes("Forecast::MultiTimePeriodQuestion")
) {
options = firstEmbeddedJson.question.answers.map((answer: any) => ({
name: answer.name,
probability: answer.normalized_probability,
type: "PROBABILITY",
}));
if (questionType.includes("Binary") || questionType.includes("NonExclusiveOpinionPoolQuestion") || questionType.includes("Forecast::Question") || ! questionType.includes("Forecast::MultiTimePeriodQuestion")) {
options = firstEmbeddedJson.question.answers.map((answer : any) => ({name: answer.name, probability: answer.normalized_probability, type: "PROBABILITY"}));
if (options.length == 1 && options[0].name == "Yes") {
let probabilityNo =
options[0].probability > 1
? 1 - options[0].probability / 100
: 1 - options[0].probability;
options.push({
name: "No",
probability: probabilityNo,
type: "PROBABILITY",
});
let probabilityNo = options[0].probability > 1 ? 1 - options[0].probability / 100 : 1 - options[0].probability;
options.push({name: "No", probability: probabilityNo, type: "PROBABILITY"});
}
}
let result = {
@ -105,30 +88,28 @@ async function fetchStats(questionUrl: string, cookie: string) {
qualityindicators: {
numforecasts: Number(numforecasts),
numforecasters: Number(numforecasters),
comments_count: Number(comments_count),
},
comments_count: Number(comments_count)
}
};
// console.log(JSON.stringify(result, null, 4));
return result;
}
function isSignedIn(html : string) {
let isSignedInBool = !(
html.includes("You need to sign in or sign up before continuing") ||
html.includes("Sign up")
);
let isSignedInBool = !(html.includes("You need to sign in or sign up before continuing") || html.includes("Sign up"));
// console.log(html)
if (! isSignedInBool) {
console.log("Error: Not signed in.");
}
console.log(`is signed in? ${isSignedInBool ? "yes" : "no"}`);
console.log(`is signed in? ${
isSignedInBool ? "yes" : "no"
}`);
return isSignedInBool;
}
function reachedEnd(html : string) {
let reachedEndBool = html.includes("No questions match your filter");
if (reachedEndBool) {
//console.log(html)
if (reachedEndBool) { // console.log(html)
}
console.log(`Reached end? ${reachedEndBool}`);
return reachedEndBool;
@ -158,12 +139,11 @@ async function goodjudgmentopen_inner(cookie: string) {
await sleep(1000 + Math.random() * 1000); // don't be as noticeable
try {
let moreinfo = await fetchStats(url, cookie);
if (moreinfo.isbinary) {
if (!moreinfo.crowdpercentage) {
// then request again.
/*if (moreinfo.isbinary) {
if (! moreinfo.crowdpercentage) { // then request again.
moreinfo = await fetchStats(url, cookie);
}
}
}*/
let questionNumRegex = new RegExp("questions/([0-9]+)");
const questionNumMatch = url.match(questionNumRegex);
if (! questionNumMatch) {
@ -176,7 +156,7 @@ async function goodjudgmentopen_inner(cookie: string) {
title: title,
url: url,
platform: platformName,
...moreinfo,
... moreinfo
};
if (j % 30 == 0 || DEBUG_MODE == "on") {
console.log(`Page #${i}`);
@ -188,9 +168,7 @@ async function goodjudgmentopen_inner(cookie: string) {
results.push(question);
} catch (error) {
console.log(error);
console.log(
`We encountered some error when fetching the URL: ${url}, so it won't appear on the final json`
);
console.log(`We encountered some error when fetching the URL: ${url}, so it won't appear on the final json`);
}
}
j = j + 1;
@ -203,9 +181,7 @@ async function goodjudgmentopen_inner(cookie: string) {
response = await fetchPage(i, cookie);
} catch (error) {
console.log(error);
console.log(
`We encountered some error when fetching page #${i}, so it won't appear on the final json`
);
console.log(`We encountered some error when fetching page #${i}, so it won't appear on the final json`);
}
}
@ -216,9 +192,11 @@ async function goodjudgmentopen_inner(cookie: string) {
let end = Date.now();
let difference = end - init;
console.log(
`Took ${difference / 1000} seconds, or ${difference / (1000 * 60)} minutes.`
);
console.log(`Took ${
difference / 1000
} seconds, or ${
difference / (1000 * 60)
} minutes.`);
return results;
}
@ -233,20 +211,15 @@ export const goodjudgmentopen: Platform = {
return(await applyIfSecretExists(cookie, goodjudgmentopen_inner)) || null;
},
calculateStars(data) {
let minProbability = Math.min(
...data.options.map((option) => option.probability || 0)
);
let maxProbability = Math.max(
...data.options.map((option) => option.probability || 0)
);
let minProbability = Math.min(...data.options.map((option) => option.probability || 0));
let maxProbability = Math.max(...data.options.map((option) => option.probability || 0));
let nuno = () => ((data.qualityindicators.numforecasts || 0) > 100 ? 3 : 2);
let eli = () => 3;
let misha = () =>
minProbability > 0.1 || maxProbability < 0.9 ? 3.1 : 2.5;
let misha = () => minProbability > 0.1 || maxProbability < 0.9 ? 3.1 : 2.5;
let starsDecimal = average([nuno(), eli(), misha()]);
let starsInteger = Math.round(starsDecimal);
return starsInteger;
},
}
};