From aff30ac0c435e18a32be3e99c64d47add0ec1e60 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 9 Oct 2022 12:52:01 +0100 Subject: [PATCH] fix: insight types --- src/backend/platforms/goodjudgment.ts | 92 +++++------ src/backend/platforms/insight.ts | 223 +++++++++++++------------- 2 files changed, 148 insertions(+), 167 deletions(-) diff --git a/src/backend/platforms/goodjudgment.ts b/src/backend/platforms/goodjudgment.ts index 91742c5..bd551eb 100644 --- a/src/backend/platforms/goodjudgment.ts +++ b/src/backend/platforms/goodjudgment.ts @@ -1,10 +1,11 @@ /* Imports */ import axios from "axios"; -import { Tabletojson } from "tabletojson"; +import {Tabletojson} from "tabletojson"; -import { average } from "../../utils"; -import { hash } from "../utils/hash"; -import { FetchedQuestion, Platform } from "./"; +import {average} from "../../utils"; +import {hash} from "../utils/hash"; +import {FetchedQuestion, Platform} from "./"; +import {FullQuestionOption} from "../../common/types"; /* Definitions */ const platformName = "goodjudgment"; @@ -30,32 +31,30 @@ export const goodjudgment: Platform = { // hard-coded backup proxy */ // proxy = { - // ip: process.env.BACKUP_PROXY_IP, - // port: process.env.BACKUP_PROXY_PORT, + // ip: process.env.BACKUP_PROXY_IP, + // port: process.env.BACKUP_PROXY_PORT, // }; // // } // let agent = tunnel.httpsOverHttp({ - // proxy: { + // proxy: { // host: proxy.ip, // port: proxy.port, - // }, + // }, // }); - const content = await axios - .request({ - url: "https://goodjudgment.io/superforecasts/", - method: "get", - headers: { - "User-Agent": "Chrome", - }, - // agent, - // port: 80, - }) - .then((query) => query.data); + const content = await axios.request({ + url: "https://goodjudgment.io/superforecasts/", + method: "get", + headers: { + "User-Agent": "Chrome" + }, + // agent, + // port: 80, + }).then((query) => query.data); // Processing let results: FetchedQuestion[] = []; - let jsonTable = Tabletojson.convert(content, { stripHtmlFromCells: false }); + let jsonTable = Tabletojson.convert(content, {stripHtmlFromCells: false}); jsonTable.shift(); // deletes first element jsonTable.pop(); // deletes last element @@ -63,38 +62,21 @@ export const goodjudgment: Platform = { let title = table[0]["0"].split("\t\t\t").splice(3)[0]; if (title != undefined) { title = title.replaceAll("", ""); - const id = `${platformName}-${hash(title)}`; - const description = table - .filter((row: any) => row["0"].includes("BACKGROUND:")) - .map((row: any) => row["0"]) - .map((text: any) => - text - .split("BACKGROUND:")[1] - .split("Examples of Superforecaster")[0] - .split("AT A GLANCE")[0] - .replaceAll("\n\n", "\n") - .split("\n") - .slice(3) - .join(" ") - .replaceAll(" ", "") - .replaceAll("
", "") - )[0]; - const options = table - .filter((row: any) => "4" in row) - .map((row: any) => ({ - name: row["2"] - .split('')[1] - .replace("", ""), - probability: Number(row["3"].split("%")[0]) / 100, - type: "PROBABILITY", - })); - let analysis = table.filter((row: any) => - row[0] ? row[0].toLowerCase().includes("commentary") : false - ); + const id = `${platformName}-${ + hash(title) + }`; + const description = table.filter((row : any) => row["0"].includes("BACKGROUND:")).map((row : any) => row["0"]).map((text : any) => text.split("BACKGROUND:")[1].split("Examples of Superforecaster")[0].split("AT A GLANCE")[0].replaceAll("\n\n", "\n").split("\n").slice(3).join(" ").replaceAll(" ", "").replaceAll("
", ""))[0]; + const options = table.filter((row : any) => "4" in row).map((row : any) => ({ + name: row["2"].split('')[1].replace("", ""), + probability: Number(row["3"].split("%")[0]) / 100, + type: "PROBABILITY" + })); + let analysis = table.filter((row : any) => row[0] ? row[0].toLowerCase().includes("commentary") : false); // "Examples of Superforecaster Commentary" / Analysis // The following is necessary twice, because we want to check if there is an empty list, and then get the first element of the first element of the list. analysis = analysis ? analysis[0] : ""; - analysis = analysis ? analysis[0] : ""; // not a duplicate + analysis = analysis ? analysis[0] : ""; + // not a duplicate // console.log(analysis) let standardObj: FetchedQuestion = { id, @@ -104,16 +86,14 @@ export const goodjudgment: Platform = { options, qualityindicators: {}, extra: { - superforecastercommentary: analysis || "", - }, + superforecastercommentary: analysis || "" + } }; results.push(standardObj); } } - console.log( - "Failing is not unexpected; see utils/pullSuperforecastsManually.sh/js" - ); + console.log("Failing is not unexpected; see utils/pullSuperforecastsManually.sh/js"); return results; }, @@ -121,8 +101,8 @@ export const goodjudgment: Platform = { let nuno = () => 4; let eli = () => 4; let misha = () => 3.5; - let starsDecimal = average([nuno()]); //, eli(), misha()]) + let starsDecimal = average([nuno()]); // , eli(), misha()]) let starsInteger = Math.round(starsDecimal); return starsInteger; - }, + } }; diff --git a/src/backend/platforms/insight.ts b/src/backend/platforms/insight.ts index ee5886b..2b22861 100644 --- a/src/backend/platforms/insight.ts +++ b/src/backend/platforms/insight.ts @@ -10,122 +10,123 @@ const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/ma /* Support functions */ -async function fetchQuestionStats(bearer: string, marketId: number) { - const response = await axios({ - url: getMarketEndpoint(marketId), - method: "GET", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - Authorization: `Bearer ${bearer}` - } - }).then((res) => res.data); - // console.log(response) - return response; -} - -async function fetchPage(bearer: string, pageNum: number) { - const response = await axios({ - url: `${marketsEnpoint}?page=${pageNum}`, // &orderBy=is_resolved&sortedBy=desc`, - method: "GET", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - Authorization: `Bearer ${bearer}` - } - }).then((res) => res.data); - // console.log(response); - return response; -} - -async function fetchData(bearer: string) { - let pageNum = 1; - let reachedEnd = false; - let results = []; - while (! reachedEnd) { - let newPage = await fetchPage(bearer, pageNum); - let newPageData = newPage.data; - let marketsFromPage = [] - for (let market of newPageData) { - let response = await fetchQuestionStats(bearer, market.id); - let marketData = response.data - let marketAnswer = marketData.answer.data - delete marketData.answer - // These are the options and their prices. - let marketOptions = marketAnswer.map(answer => { - return({name: answer.title, probability: answer.latest_yes_price, type: "PROBABILITY"}) - }) - marketsFromPage.push({ - ... marketData, - options: marketOptions - }); - } - - let finalObject = marketsFromPage - - console.log(`Page = #${pageNum}`); - // console.log(newPageData) - console.dir(finalObject, {depth: null}); - results.push(... finalObject); - - let newPagination = newPage.meta.pagination; - if (newPagination.total_pages == pageNum) { - reachedEnd = true; - } else { - pageNum = pageNum + 1; - } +/* +async function fetchQuestionStats(bearer : string, marketId : number) { + const response = await axios({ + url: getMarketEndpoint(marketId), + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${bearer}` } - return results + }).then((res) => res.data); + // console.log(response) + return response; } -async function processPredictions(predictions: any[]) { - let results = await predictions.map((prediction) => { - const id = `${platformName}-${ - prediction.id - }`; - const probability = prediction.probability; - const options: FetchedQuestion["options"] = [ - { - name: "Yes", - probability: probability, - type: "PROBABILITY" - }, { - name: "No", - probability: 1 - probability, - type: "PROBABILITY" - }, - ]; - const result: FetchedQuestion = { - id, - title: prediction.title, - url: "https://example.com", - description: prediction.description, - options, - qualityindicators: { - // other: prediction.otherx, - // indicators: prediction.indicatorx, - } - }; - return result; - }); - return results; // resultsProcessed +async function fetchPage(bearer : string, pageNum : number) { + const response = await axios({ + url: `${marketsEnpoint}?page=${pageNum}`, // &orderBy=is_resolved&sortedBy=desc`, + method: "GET", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${bearer}` + } + }).then((res) => res.data); + // console.log(response); + return response; } +async function fetchData(bearer : string) { + let pageNum = 1; + let reachedEnd = false; + let results = []; + while (! reachedEnd) { + let newPage = await fetchPage(bearer, pageNum); + let newPageData = newPage.data; + let marketsFromPage = [] + for (let market of newPageData) { + let response = await fetchQuestionStats(bearer, market.id); + let marketData = response.data + let marketAnswer = marketData.answer.data + delete marketData.answer + // These are the options and their prices. + let marketOptions = marketAnswer.map(answer => { + return({name: answer.title, probability: answer.latest_yes_price, type: "PROBABILITY"}) + }) + marketsFromPage.push({ + ... marketData, + options: marketOptions + }); + } + + let finalObject = marketsFromPage + + console.log(`Page = #${pageNum}`); + // console.log(newPageData) + console.dir(finalObject, {depth: null}); + results.push(... finalObject); + + let newPagination = newPage.meta.pagination; + if (newPagination.total_pages == pageNum) { + reachedEnd = true; + } else { + pageNum = pageNum + 1; + } + } + return results +} + +async function processPredictions(predictions : any[]) { + let results = await predictions.map((prediction) => { + const id = `${platformName}-${ + prediction.id + }`; + const probability = prediction.probability; + const options: FetchedQuestion["options"] = [ + { + name: "Yes", + probability: probability, + type: "PROBABILITY" + }, { + name: "No", + probability: 1 - probability, + type: "PROBABILITY" + }, + ]; + const result: FetchedQuestion = { + id, + title: prediction.title, + url: "https://example.com", + description: prediction.description, + options, + qualityindicators: { + // other: prediction.otherx, + // indicators: prediction.indicatorx, + } + }; + return result; + }); + return results; // resultsProcessed +} +*/ + /* Body */ - export const insight: Platform = { - name: platformName, - label: "Insight Prediction", - color: "#ff0000", - version: "v0", - async fetcher() { - let bearer = process.env.INSIGHT_BEARER; - let data = await fetchData(bearer); - // console.log(data); - let results = []; // await processPredictions(data); // somehow needed - return results; - }, - calculateStars(data) { - return 2; - } + name: platformName, + label: "Insight Prediction", + color: "#ff0000", + version: "v1", + async fetcher() { + let bearer = process.env.INSIGHT_BEARER; + // let data = await fetchData(bearer); + // console.log(data); + let results: FetchedQuestion[] = []; // await processPredictions(data); // somehow needed + return results; + }, + calculateStars(data) { + return 2; + } };