/* Imports */ import axios from "axios"; import { Tabletojson } from "tabletojson"; import { average } from "../../utils"; import { applyIfSecretExists } from "../utils/getSecrets"; import { sleep } from "../utils/sleep"; import toMarkdown from "../utils/toMarkdown"; import { FetchedQuestion, Platform } from "./"; /* Definitions */ const platformName = "goodjudgmentopen"; const htmlEndPoint = "https://www.gjopen.com/questions?page="; const annoyingPromptUrls = [ "https://www.gjopen.com/questions/1933-what-forecasting-questions-should-we-ask-what-questions-would-you-like-to-forecast-on-gjopen", "https://www.gjopen.com/questions/1779-are-there-any-forecasting-tips-tricks-and-experiences-you-would-like-to-share-and-or-discuss-with-your-fellow-forecasters", "https://www.gjopen.com/questions/2246-are-there-any-forecasting-tips-tricks-and-experiences-you-would-like-to-share-and-or-discuss-with-your-fellow-forecasters-2022-thread", "https://www.gjopen.com/questions/2237-what-forecasting-questions-should-we-ask-what-questions-would-you-like-to-forecast-on-gjopen", "https://www.gjopen.com/questions/2437-what-forecasting-questions-should-we-ask-what-questions-would-you-like-to-forecast-on-gjopen" ]; const DEBUG_MODE: "on" | "off" = "off"; // "on" const id = () => 0; /* Support functions */ function cleanDescription(text: string) { let md = toMarkdown(text); let result = md.replaceAll("---", "-").replaceAll(" ", " "); return result; } async function fetchPage(page: number, cookie: string) { const response: string = await axios({ url: htmlEndPoint + page, method: "GET", headers: { Cookie: cookie, }, }).then((res) => res.data); //console.log(response) return response; } async function fetchStats(questionUrl: string, cookie: string) { let response: string = await axios({ url: questionUrl + "/stats", method: "GET", headers: { "Content-Type": "text/html", Cookie: cookie, Referer: questionUrl, }, }).then((res) => res.data); if (response.includes("Sign up or sign in to forecast")) { throw Error("Not logged in"); } // Init let options: FullQuestionOption[] = []; // Parse the embedded json let htmlElements = response.split("\n"); 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(""", '"')); return json; }); let firstEmbeddedJson = embeddedJsons[0]; let title = firstEmbeddedJson.question.name; let description = cleanDescription(firstEmbeddedJson.question.description); let comments_count = firstEmbeddedJson.question.comments_count; 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 (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 result = { description: description, options: options, qualityindicators: { numforecasts: Number(numforecasts), numforecasters: Number(numforecasters), 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") ); // console.log(html) if (!isSignedInBool) { console.log("Error: Not signed in."); } 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) } console.log(`Reached end? ${reachedEndBool}`); return reachedEndBool; } /* Body */ async function goodjudgmentopen_inner(cookie: string) { let i = 1; let response = await fetchPage(i, cookie); let results = []; let init = Date.now(); // console.log("Downloading... This might take a couple of minutes. Results will be shown.") console.log("Page #1") while (!reachedEnd(response) && isSignedIn(response)) { let htmlLines = response.split("\n"); DEBUG_MODE == "on" ? htmlLines.forEach((line) => console.log(line)) : id(); let h5elements = htmlLines.filter((str) => str.includes("