metaforecast/src/backend/platforms/insight.ts

127 lines
3.8 KiB
TypeScript
Raw Normal View History

/* Imports */
import axios from "axios";
2022-07-28 17:33:40 +00:00
import {FetchedQuestion, Platform} from ".";
/* Definitions */
const platformName = "insight";
const marketsEnpoint = "https://insightprediction.com/api/markets";
2022-07-28 17:33:40 +00:00
const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/markets/${id}`;
/* Support functions */
2022-07-28 17:33:40 +00:00
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) {
2022-07-28 17:33:40 +00:00
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;
}
2022-07-28 17:33:40 +00:00
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
2022-07-28 17:33:40 +00:00
console.log(`Page = #${pageNum}`);
// console.log(newPageData)
// console.dir(finalObject, {depth: null});
2022-07-28 17:33:40 +00:00
results.push(... finalObject);
2022-07-28 17:33:40 +00:00
let newPagination = newPage.meta.pagination;
if (newPagination.total_pages == pageNum) {
reachedEnd = true;
} else {
pageNum = pageNum + 1;
}
}
2022-07-28 17:33:40 +00:00
return results
}
async function processPredictions(predictions: any[]) {
let filteredPredictions = predictions.filter(prediction => !prediction.is_resolved && prediction.category != "Sports")
let results = filteredPredictions.map((prediction) => {
2022-07-28 17:33:40 +00:00
const id = `${platformName}-${
prediction.id
}`;
const options: FetchedQuestion["options"] = prediction.options
2022-07-28 17:33:40 +00:00
const result: FetchedQuestion = {
id,
title: prediction.title,
url: `https:${
prediction.url
}`,
description: prediction.rules,
2022-07-28 17:33:40 +00:00
options,
qualityindicators: {
volume: prediction.volume,
createdTime: prediction.created_at
2022-07-28 17:33:40 +00:00
// other: prediction.otherx,
// indicators: prediction.indicatorx,
}
};
return result;
});
// Filter results
2022-07-28 17:33:40 +00:00
return results; // resultsProcessed
}
/* Body */
export const insight: Platform = {
2022-07-28 17:33:40 +00:00
name: platformName,
label: "Insight Prediction",
color: "#ff0000",
version: "v1",
2022-07-28 17:33:40 +00:00
async fetcher() {
let bearer = process.env.INSIGHT_BEARER;
let data = await fetchData(bearer);
let results = await processPredictions(data);
console.log(results);
2022-07-28 17:33:40 +00:00
return results;
},
calculateStars(data) {
return 2;
}
};