/* Imports */ import fs from 'fs' import axios from "axios" import { Tabletojson } from "tabletojson" import { calculateStars } from "../utils/stars.js" import toMarkdown from "../utils/toMarkdown.js" import { upsert } from "../utils/mongo-wrapper.js" /* Definitions */ let htmlEndPoint = 'https://www.gjopen.com/questions?page=' let 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"] /* Support functions */ function getcookie() { return process.env.GOODJUDGMENTOPENCOOKIE /* try { let rawcookie = fs.readFileSync("./src/input/privatekeys.json") let cookie = JSON.parse(rawcookie).goodjudmentopencookie if (cookie == undefined) { throw new Error('No cookie for Good Judgment Open!'); } return cookie } catch (error) { console.log("Error: No cookies for Good Judgment Open on src/privatekeys.json! See the README.md") process.exit() } */ } async function fetchPage(page, cookie) { let response = await axios({ url: htmlEndPoint + page, method: 'GET', headers: ({ 'Content-Type': 'text/html', 'Cookie': cookie }), }) .then(res => res.data) //console.log(response) return response } async function fetchStats(questionUrl, cookie) { let response = await axios({ url: questionUrl + "/stats", method: 'GET', headers: ({ 'Content-Type': 'text/html', 'Cookie': cookie, 'Referer': questionUrl, }), }) .then(res => res.data) //console.log(response) // Is binary? let isbinary = response.includes("binary?":true") let options = [] if (isbinary) { // Crowd percentage let htmlElements = response.split("\n") let h3Element = htmlElements.filter(str => str.includes("

"))[0] // console.log(h3Element) let crowdpercentage = h3Element.split(">")[1].split("<")[0] let probability = Number(crowdpercentage.replace("%", "")) / 100 options.push(({ name: "Yes", probability: probability, type: "PROBABILITY" }), ({ name: "No", probability: +(1 - probability).toFixed(2), // avoids floating point shenanigans type: "PROBABILITY" })) } else { let optionsHtmlElement = "" let tablesAsJson = Tabletojson.convert(optionsHtmlElement) let firstTable = tablesAsJson[0] options = firstTable.map(element => ({ name: element['0'], probability: Number(element['1'].replace("%", "")) / 100, type: "PROBABILITY" })) //console.log(optionsHtmlElement) //console.log(options) } // Description let descriptionraw = response.split(`
`)[1] let descriptionprocessed1 = descriptionraw.split(`
`)[0] let descriptionprocessed2 = toMarkdown(descriptionprocessed1) let descriptionprocessed3 = descriptionprocessed2.split("\n") .filter(string => !string.includes("Confused? Check our")) .join("\n") let description = descriptionprocessed3 // Number of forecasts let numforecasts = response.split("prediction_sets_count":")[1].split(",")[0] //console.log(numforecasts) // Number of predictors let numforecasters = response.split("predictors_count":")[1].split(",")[0] //console.log(numpredictors) // Calculate the stars let minProbability = Math.min(...options.map(option => option.probability)) let maxProbability = Math.max(...options.map(option => option.probability)) let result = { "description": description, "options": options, "timestamp": new Date().toISOString(), "qualityindicators": { "numforecasts": numforecasts, "numforecasters": numforecasters, "stars": calculateStars("Good Judgment Open", ({ numforecasts, minProbability, maxProbability })) } } return result } function isEnd(html) { return html.includes("No questions match your filter") } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } /* Body */ export async function goodjudgmentopen() { let cookie = getcookie() 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.") while (!isEnd(response)) { // console.log(`Page #${i}`) let htmlLines = response.split("\n") let h5elements = htmlLines.filter(str => str.includes("
') let url = h5elementSplit[0].split('