fix: insight types

This commit is contained in:
NunoSempere 2022-10-09 12:52:01 +01:00
parent 0cddbf69b0
commit aff30ac0c4
2 changed files with 148 additions and 167 deletions

View File

@ -1,10 +1,11 @@
/* Imports */ /* Imports */
import axios from "axios"; import axios from "axios";
import { Tabletojson } from "tabletojson"; import {Tabletojson} from "tabletojson";
import { average } from "../../utils"; import {average} from "../../utils";
import { hash } from "../utils/hash"; import {hash} from "../utils/hash";
import { FetchedQuestion, Platform } from "./"; import {FetchedQuestion, Platform} from "./";
import {FullQuestionOption} from "../../common/types";
/* Definitions */ /* Definitions */
const platformName = "goodjudgment"; const platformName = "goodjudgment";
@ -30,32 +31,30 @@ export const goodjudgment: Platform = {
// hard-coded backup proxy // hard-coded backup proxy
*/ */
// proxy = { // proxy = {
// ip: process.env.BACKUP_PROXY_IP, // ip: process.env.BACKUP_PROXY_IP,
// port: process.env.BACKUP_PROXY_PORT, // port: process.env.BACKUP_PROXY_PORT,
// }; // };
// // } // // }
// let agent = tunnel.httpsOverHttp({ // let agent = tunnel.httpsOverHttp({
// proxy: { // proxy: {
// host: proxy.ip, // host: proxy.ip,
// port: proxy.port, // port: proxy.port,
// }, // },
// }); // });
const content = await axios const content = await axios.request({
.request({ url: "https://goodjudgment.io/superforecasts/",
url: "https://goodjudgment.io/superforecasts/", method: "get",
method: "get", headers: {
headers: { "User-Agent": "Chrome"
"User-Agent": "Chrome", },
}, // agent,
// agent, // port: 80,
// port: 80, }).then((query) => query.data);
})
.then((query) => query.data);
// Processing // Processing
let results: FetchedQuestion[] = []; let results: FetchedQuestion[] = [];
let jsonTable = Tabletojson.convert(content, { stripHtmlFromCells: false }); let jsonTable = Tabletojson.convert(content, {stripHtmlFromCells: false});
jsonTable.shift(); // deletes first element jsonTable.shift(); // deletes first element
jsonTable.pop(); // deletes last 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]; let title = table[0]["0"].split("\t\t\t").splice(3)[0];
if (title != undefined) { if (title != undefined) {
title = title.replaceAll("</a>", ""); title = title.replaceAll("</a>", "");
const id = `${platformName}-${hash(title)}`; const id = `${platformName}-${
const description = table hash(title)
.filter((row: any) => row["0"].includes("BACKGROUND:")) }`;
.map((row: any) => row["0"]) 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("<br> ", ""))[0];
.map((text: any) => const options = table.filter((row : any) => "4" in row).map((row : any) => ({
text name: row["2"].split('<span class="qTitle">')[1].replace("</span>", ""),
.split("BACKGROUND:")[1] probability: Number(row["3"].split("%")[0]) / 100,
.split("Examples of Superforecaster")[0] type: "PROBABILITY"
.split("AT A GLANCE")[0] }));
.replaceAll("\n\n", "\n") let analysis = table.filter((row : any) => row[0] ? row[0].toLowerCase().includes("commentary") : false);
.split("\n")
.slice(3)
.join(" ")
.replaceAll(" ", "")
.replaceAll("<br> ", "")
)[0];
const options = table
.filter((row: any) => "4" in row)
.map((row: any) => ({
name: row["2"]
.split('<span class="qTitle">')[1]
.replace("</span>", ""),
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 // "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. // 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] : "";
analysis = analysis ? analysis[0] : ""; // not a duplicate analysis = analysis ? analysis[0] : "";
// not a duplicate
// console.log(analysis) // console.log(analysis)
let standardObj: FetchedQuestion = { let standardObj: FetchedQuestion = {
id, id,
@ -104,16 +86,14 @@ export const goodjudgment: Platform = {
options, options,
qualityindicators: {}, qualityindicators: {},
extra: { extra: {
superforecastercommentary: analysis || "", superforecastercommentary: analysis || ""
}, }
}; };
results.push(standardObj); results.push(standardObj);
} }
} }
console.log( console.log("Failing is not unexpected; see utils/pullSuperforecastsManually.sh/js");
"Failing is not unexpected; see utils/pullSuperforecastsManually.sh/js"
);
return results; return results;
}, },
@ -121,8 +101,8 @@ export const goodjudgment: Platform = {
let nuno = () => 4; let nuno = () => 4;
let eli = () => 4; let eli = () => 4;
let misha = () => 3.5; let misha = () => 3.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;
}, }
}; };

View File

@ -10,122 +10,123 @@ const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/ma
/* Support functions */ /* Support functions */
async function fetchQuestionStats(bearer: string, marketId: number) { /*
const response = await axios({ async function fetchQuestionStats(bearer : string, marketId : number) {
url: getMarketEndpoint(marketId), const response = await axios({
method: "GET", url: getMarketEndpoint(marketId),
headers: { method: "GET",
"Content-Type": "application/json", headers: {
Accept: "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${bearer}` 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;
}
} }
return results }).then((res) => res.data);
// console.log(response)
return response;
} }
async function processPredictions(predictions: any[]) { async function fetchPage(bearer : string, pageNum : number) {
let results = await predictions.map((prediction) => { const response = await axios({
const id = `${platformName}-${ url: `${marketsEnpoint}?page=${pageNum}`, // &orderBy=is_resolved&sortedBy=desc`,
prediction.id method: "GET",
}`; headers: {
const probability = prediction.probability; "Content-Type": "application/json",
const options: FetchedQuestion["options"] = [ Accept: "application/json",
{ Authorization: `Bearer ${bearer}`
name: "Yes", }
probability: probability, }).then((res) => res.data);
type: "PROBABILITY" // console.log(response);
}, { return response;
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 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 */ /* Body */
export const insight: Platform = { export const insight: Platform = {
name: platformName, name: platformName,
label: "Insight Prediction", label: "Insight Prediction",
color: "#ff0000", color: "#ff0000",
version: "v0", version: "v1",
async fetcher() { async fetcher() {
let bearer = process.env.INSIGHT_BEARER; let bearer = process.env.INSIGHT_BEARER;
let data = await fetchData(bearer); // let data = await fetchData(bearer);
// console.log(data); // console.log(data);
let results = []; // await processPredictions(data); // somehow needed let results: FetchedQuestion[] = []; // await processPredictions(data); // somehow needed
return results; return results;
}, },
calculateStars(data) { calculateStars(data) {
return 2; return 2;
} }
}; };