2021-01-12 12:43:41 +00:00
|
|
|
/* Imports */
|
2022-03-28 17:59:07 +00:00
|
|
|
import axios from "axios";
|
|
|
|
import { Tabletojson } from "tabletojson";
|
2022-03-27 21:10:31 +00:00
|
|
|
|
2022-03-28 17:59:07 +00:00
|
|
|
import { applyIfSecretExists } from "../utils/getSecrets";
|
|
|
|
import { calculateStars } from "../utils/stars";
|
|
|
|
import toMarkdown from "../utils/toMarkdown";
|
2022-03-29 15:10:28 +00:00
|
|
|
import { Platform } from "./";
|
2021-01-12 12:43:41 +00:00
|
|
|
|
|
|
|
/* Definitions */
|
2022-02-11 14:21:36 +00:00
|
|
|
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",
|
|
|
|
"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",
|
|
|
|
];
|
2022-03-27 21:10:31 +00:00
|
|
|
const DEBUG_MODE: "on" | "off" = "off"; // "on"
|
|
|
|
const id = () => 0;
|
2021-01-12 12:43:41 +00:00
|
|
|
|
|
|
|
/* Support functions */
|
|
|
|
|
2021-03-02 13:29:27 +00:00
|
|
|
async function fetchPage(page, cookie) {
|
|
|
|
let response = await axios({
|
|
|
|
url: htmlEndPoint + page,
|
2022-02-11 14:21:36 +00:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
Cookie: cookie,
|
|
|
|
},
|
|
|
|
}).then((res) => res.data);
|
2021-01-12 12:43:41 +00:00
|
|
|
//console.log(response)
|
2022-02-11 14:21:36 +00:00
|
|
|
return response;
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 13:29:27 +00:00
|
|
|
async function fetchStats(questionUrl, cookie) {
|
|
|
|
let response = await axios({
|
|
|
|
url: questionUrl + "/stats",
|
2022-02-11 14:21:36 +00:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "text/html",
|
|
|
|
Cookie: cookie,
|
|
|
|
Referer: questionUrl,
|
|
|
|
},
|
|
|
|
}).then((res) => res.data);
|
2021-01-12 12:43:41 +00:00
|
|
|
//console.log(response)
|
2021-03-02 13:29:27 +00:00
|
|
|
|
2021-01-12 12:43:41 +00:00
|
|
|
// Is binary?
|
2022-02-11 14:21:36 +00:00
|
|
|
let isbinary = response.includes("binary?":true");
|
2021-01-12 12:43:41 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
let options = [];
|
2021-03-02 13:29:27 +00:00
|
|
|
if (isbinary) {
|
2021-01-12 12:43:41 +00:00
|
|
|
// Crowd percentage
|
2022-02-11 14:21:36 +00:00
|
|
|
let htmlElements = response.split("\n");
|
|
|
|
let h3Element = htmlElements.filter((str) => str.includes("<h3>"))[0];
|
2021-03-16 16:55:45 +00:00
|
|
|
// console.log(h3Element)
|
2022-02-11 14:21:36 +00:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
);
|
2021-03-02 13:29:27 +00:00
|
|
|
} else {
|
2022-02-11 14:21:36 +00:00
|
|
|
let optionsHtmlElement = "<table" + response.split("tbody")[1] + "table>";
|
|
|
|
let tablesAsJson = Tabletojson.convert(optionsHtmlElement);
|
|
|
|
let firstTable = tablesAsJson[0];
|
|
|
|
options = firstTable.map((element) => ({
|
|
|
|
name: element["0"],
|
|
|
|
probability: Number(element["1"].replace("%", "")) / 100,
|
|
|
|
type: "PROBABILITY",
|
|
|
|
}));
|
2021-02-18 16:12:55 +00:00
|
|
|
//console.log(optionsHtmlElement)
|
|
|
|
//console.log(options)
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 17:35:38 +00:00
|
|
|
// Description
|
2022-02-11 14:21:36 +00:00
|
|
|
let descriptionraw = response.split(
|
|
|
|
`<div id="question-background" class="collapse smb">`
|
|
|
|
)[1];
|
|
|
|
let descriptionprocessed1 = descriptionraw.split(`</div>`)[0];
|
|
|
|
let descriptionprocessed2 = toMarkdown(descriptionprocessed1);
|
|
|
|
let descriptionprocessed3 = descriptionprocessed2
|
|
|
|
.split("\n")
|
|
|
|
.filter((string) => !string.includes("Confused? Check our"))
|
|
|
|
.join("\n");
|
|
|
|
let description = descriptionprocessed3;
|
2021-03-02 13:29:27 +00:00
|
|
|
|
2021-01-12 12:43:41 +00:00
|
|
|
// Number of forecasts
|
2022-02-11 14:21:36 +00:00
|
|
|
let numforecasts = response
|
|
|
|
.split("prediction_sets_count":")[1]
|
|
|
|
.split(",")[0];
|
2021-01-12 12:43:41 +00:00
|
|
|
//console.log(numforecasts)
|
2021-03-02 13:29:27 +00:00
|
|
|
|
2021-01-12 12:43:41 +00:00
|
|
|
// Number of predictors
|
2022-02-11 14:21:36 +00:00
|
|
|
let numforecasters = response
|
|
|
|
.split("predictors_count":")[1]
|
|
|
|
.split(",")[0];
|
2021-01-12 12:43:41 +00:00
|
|
|
//console.log(numpredictors)
|
2021-03-02 13:29:27 +00:00
|
|
|
|
|
|
|
// Calculate the stars
|
2022-02-11 14:21:36 +00:00
|
|
|
let minProbability = Math.min(...options.map((option) => option.probability));
|
|
|
|
let maxProbability = Math.max(...options.map((option) => option.probability));
|
|
|
|
|
2021-01-12 12:43:41 +00:00
|
|
|
let result = {
|
2022-02-11 14:21:36 +00:00
|
|
|
description: description,
|
|
|
|
options: options,
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
qualityindicators: {
|
|
|
|
numforecasts: Number(numforecasts),
|
|
|
|
numforecasters: Number(numforecasters),
|
|
|
|
stars: calculateStars("Good Judgment Open", {
|
|
|
|
numforecasts,
|
|
|
|
minProbability,
|
|
|
|
maxProbability,
|
|
|
|
}),
|
|
|
|
},
|
2022-03-27 21:10:31 +00:00
|
|
|
// this mismatches the code below, and needs to be fixed, but I'm doing typescript conversion and don't want to touch any logic for now
|
|
|
|
} as any;
|
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
return result;
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
function isSignedIn(html) {
|
|
|
|
let isSignedInBool = !(
|
|
|
|
html.includes("You need to sign in or sign up before continuing") ||
|
|
|
|
html.includes("Sign up")
|
|
|
|
);
|
2021-08-08 17:42:21 +00:00
|
|
|
// console.log(html)
|
2022-02-11 14:21:36 +00:00
|
|
|
if (!isSignedInBool) {
|
|
|
|
console.log("Error: Not signed in.");
|
2021-07-09 16:15:49 +00:00
|
|
|
}
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log(`is signed in? ${isSignedInBool}`);
|
|
|
|
return isSignedInBool;
|
2021-07-09 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
function isEnd(html) {
|
|
|
|
let isEndBool = html.includes("No questions match your filter");
|
|
|
|
if (isEndBool) {
|
2021-07-09 16:15:49 +00:00
|
|
|
//console.log(html)
|
2021-07-04 22:01:09 +00:00
|
|
|
}
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log(`IsEnd? ${isEndBool}`);
|
|
|
|
return isEndBool;
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 01:34:27 +00:00
|
|
|
function sleep(ms: number) {
|
2022-02-11 14:21:36 +00:00
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Body */
|
|
|
|
|
2021-04-10 18:18:22 +00:00
|
|
|
async function goodjudgmentopen_inner(cookie) {
|
2022-02-11 14:21:36 +00:00
|
|
|
let i = 1;
|
|
|
|
let response = await fetchPage(i, cookie);
|
|
|
|
|
|
|
|
let results = [];
|
|
|
|
let init = Date.now();
|
2021-04-08 19:32:03 +00:00
|
|
|
// console.log("Downloading... This might take a couple of minutes. Results will be shown.")
|
2022-02-11 14:21:36 +00:00
|
|
|
while (!isEnd(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("<h5> <a href="));
|
|
|
|
DEBUG_MODE == "on" ? console.log(h5elements) : id();
|
|
|
|
let j = 0;
|
2021-03-02 13:29:27 +00:00
|
|
|
for (let h5element of h5elements) {
|
2022-02-11 14:21:36 +00:00
|
|
|
let h5elementSplit = h5element.split('"><span>');
|
|
|
|
let url = h5elementSplit[0].split('<a href="')[1];
|
|
|
|
if (!annoyingPromptUrls.includes(url)) {
|
|
|
|
let title = h5elementSplit[1].replace("</span></a></h5>", "");
|
|
|
|
await sleep(1000 + Math.random() * 1000); // don't be as noticeable
|
2021-04-08 20:51:02 +00:00
|
|
|
try {
|
2022-02-11 14:21:36 +00:00
|
|
|
let moreinfo = await fetchStats(url, cookie);
|
2021-04-08 20:51:02 +00:00
|
|
|
if (moreinfo.isbinary) {
|
2022-02-11 14:21:36 +00:00
|
|
|
if (!moreinfo.crowdpercentage) {
|
|
|
|
// then request again.
|
|
|
|
moreinfo = await fetchStats(url, cookie);
|
2021-04-08 20:51:02 +00:00
|
|
|
}
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
2022-02-11 14:21:36 +00:00
|
|
|
let questionNumRegex = new RegExp("questions/([0-9]+)");
|
|
|
|
let questionNum = url.match(questionNumRegex)[1]; //.split("questions/")[1].split("-")[0];
|
|
|
|
let id = `goodjudmentopen-${questionNum}`;
|
|
|
|
let question = {
|
|
|
|
id: id,
|
|
|
|
title: title,
|
|
|
|
url: url,
|
|
|
|
platform: "Good Judgment Open",
|
|
|
|
...moreinfo,
|
|
|
|
};
|
|
|
|
if (j % 30 == 0 || DEBUG_MODE == "on") {
|
|
|
|
console.log(`Page #${i}`);
|
|
|
|
console.log(question);
|
2021-04-08 20:51:02 +00:00
|
|
|
}
|
|
|
|
// console.log(question)
|
2022-02-11 14:21:36 +00:00
|
|
|
results.push(question);
|
2021-04-08 20:51:02 +00:00
|
|
|
} catch (error) {
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log(error);
|
|
|
|
console.log(
|
|
|
|
`We encountered some error when fetching the URL: ${url}, so it won't appear on the final json`
|
|
|
|
);
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-11 14:21:36 +00:00
|
|
|
j = j + 1;
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
2022-02-11 14:21:36 +00:00
|
|
|
i = i + 1;
|
2021-04-08 19:32:03 +00:00
|
|
|
// console.log("Sleeping for 5secs so as to not be as noticeable to the gjopen servers")
|
2022-02-11 14:21:36 +00:00
|
|
|
await sleep(5000 + Math.random() * 1000); // don't be a dick to gjopen server
|
2021-03-02 13:29:27 +00:00
|
|
|
|
|
|
|
try {
|
2022-02-11 14:21:36 +00:00
|
|
|
response = await fetchPage(i, cookie);
|
2021-03-02 13:29:27 +00:00
|
|
|
} catch (error) {
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log(error);
|
|
|
|
console.log(
|
|
|
|
`We encountered some error when fetching page #${i}, so it won't appear on the final json`
|
|
|
|
);
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 01:34:27 +00:00
|
|
|
|
|
|
|
if (results.length === 0) {
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log("Not updating results, as process was not signed in");
|
2022-03-29 01:34:27 +00:00
|
|
|
return;
|
2021-07-09 16:38:24 +00:00
|
|
|
}
|
2021-03-02 13:29:27 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
let end = Date.now();
|
|
|
|
let difference = end - init;
|
|
|
|
console.log(
|
|
|
|
`Took ${difference / 1000} seconds, or ${difference / (1000 * 60)} minutes.`
|
|
|
|
);
|
2022-03-29 01:34:27 +00:00
|
|
|
|
|
|
|
return results;
|
2021-01-12 12:43:41 +00:00
|
|
|
}
|
2021-04-10 18:18:22 +00:00
|
|
|
|
2022-03-29 15:10:28 +00:00
|
|
|
export const goodjudmentopen: Platform = {
|
|
|
|
name: "goodjudmentopen", // note the typo! current table name is without `g`, `goodjudmentopen`
|
|
|
|
async fetcher() {
|
|
|
|
let cookie = process.env.GOODJUDGMENTOPENCOOKIE;
|
|
|
|
return await applyIfSecretExists(cookie, goodjudgmentopen_inner);
|
|
|
|
},
|
2022-03-29 01:34:27 +00:00
|
|
|
};
|