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