feat: wrange Insight Prediction API
This commit is contained in:
parent
a751caf8fb
commit
69ab8f905c
|
@ -1,117 +1,131 @@
|
||||||
/* Imports */
|
/* Imports */
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
import { FetchedQuestion, Platform } from ".";
|
import {FetchedQuestion, Platform} from ".";
|
||||||
|
|
||||||
/* Definitions */
|
/* Definitions */
|
||||||
const platformName = "insight";
|
const platformName = "insight";
|
||||||
const marketsEnpoint = "https://insightprediction.com/api/markets";
|
const marketsEnpoint = "https://insightprediction.com/api/markets";
|
||||||
const getMarketEndpoint = (id: number) => `https://insightprediction.com/api/markets/${id}`
|
const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/markets/${id}`;
|
||||||
|
|
||||||
/* Support functions */
|
/* Support functions */
|
||||||
|
|
||||||
async function fetchQuestionStats(bearer: string, marketId: number){
|
async function fetchQuestionStats(bearer: string, marketId: number) {
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
url: getMarketEndpoint(marketId),
|
url: getMarketEndpoint(marketId),
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Accept": "application/json",
|
Accept: "application/json",
|
||||||
"Authorization": `Bearer ${bearer}`,
|
Authorization: `Bearer ${bearer}`
|
||||||
},
|
}
|
||||||
}).then((res) => res.data);
|
}).then((res) => res.data);
|
||||||
// console.log(response)
|
// console.log(response)
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchPage(bearer: string, pageNum: number) {
|
async function fetchPage(bearer: string, pageNum: number) {
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
url: `${marketsEnpoint}?page=${pageNum}`,
|
url: `${marketsEnpoint}?page=${pageNum}`, // &orderBy=is_resolved&sortedBy=desc`,
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Accept": "application/json",
|
Accept: "application/json",
|
||||||
"Authorization": `Bearer ${bearer}`,
|
Authorization: `Bearer ${bearer}`
|
||||||
},
|
}
|
||||||
}).then((res) => res.data);
|
}).then((res) => res.data);
|
||||||
// console.log(response)
|
// console.log(response);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchData(bearer: string){
|
async function fetchData(bearer: string) {
|
||||||
let pageNum = 1
|
let pageNum = 1;
|
||||||
let reachedEnd = false
|
let reachedEnd = false;
|
||||||
let results = []
|
let results = [];
|
||||||
while(!reachedEnd){
|
while (! reachedEnd) {
|
||||||
let newPage = await fetchPage(bearer, pageNum)
|
let newPage = await fetchPage(bearer, pageNum);
|
||||||
let newPageData = newPage.data
|
let newPageData = newPage.data;
|
||||||
|
let marketsFromPage = []
|
||||||
let marketsWithStats = newPageData.map(marketData => {
|
for (let market of newPageData) {
|
||||||
let marketStats = fetchQuestionStats(bearer, marketData.id)
|
let response = await fetchQuestionStats(bearer, market.id);
|
||||||
return ({...marketStats, ...marketData})
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`Page = #${pageNum}`)
|
let finalObject = marketsFromPage
|
||||||
// console.log(newPageData)
|
|
||||||
console.log(marketsWithStats)
|
|
||||||
results.push(...marketsWithStats)
|
|
||||||
|
|
||||||
let newPagination = newPage.meta.pagination
|
console.log(`Page = #${pageNum}`);
|
||||||
if(newPagination.total_pages == pageNum ){
|
// console.log(newPageData)
|
||||||
reachedEnd = true
|
console.dir(finalObject, {depth: null});
|
||||||
}else{
|
results.push(... finalObject);
|
||||||
pageNum = pageNum + 1
|
|
||||||
|
let newPagination = newPage.meta.pagination;
|
||||||
|
if (newPagination.total_pages == pageNum) {
|
||||||
|
reachedEnd = true;
|
||||||
|
} else {
|
||||||
|
pageNum = pageNum + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processPredictions(predictions: any[]) {
|
async function processPredictions(predictions: any[]) {
|
||||||
let results = await predictions.map((prediction) => {
|
let results = await predictions.map((prediction) => {
|
||||||
const id = `${platformName}-${prediction.id}`;
|
const id = `${platformName}-${
|
||||||
const probability = prediction.probability;
|
prediction.id
|
||||||
const options: FetchedQuestion["options"] = [
|
}`;
|
||||||
{
|
const probability = prediction.probability;
|
||||||
name: "Yes",
|
const options: FetchedQuestion["options"] = [
|
||||||
probability: probability,
|
{
|
||||||
type: "PROBABILITY",
|
name: "Yes",
|
||||||
},
|
probability: probability,
|
||||||
{
|
type: "PROBABILITY"
|
||||||
name: "No",
|
}, {
|
||||||
probability: 1 - probability,
|
name: "No",
|
||||||
type: "PROBABILITY",
|
probability: 1 - probability,
|
||||||
},
|
type: "PROBABILITY"
|
||||||
];
|
},
|
||||||
const result: FetchedQuestion = {
|
];
|
||||||
id,
|
const result: FetchedQuestion = {
|
||||||
title: prediction.title,
|
id,
|
||||||
url: "https://example.com",
|
title: prediction.title,
|
||||||
description: prediction.description,
|
url: "https://example.com",
|
||||||
options,
|
description: prediction.description,
|
||||||
qualityindicators: {
|
options,
|
||||||
// other: prediction.otherx,
|
qualityindicators: {
|
||||||
// indicators: prediction.indicatorx,
|
// other: prediction.otherx,
|
||||||
},
|
// indicators: prediction.indicatorx,
|
||||||
};
|
}
|
||||||
return result;
|
};
|
||||||
});
|
return result;
|
||||||
return results; //resultsProcessed
|
});
|
||||||
|
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: "v0",
|
||||||
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 = []; // await processPredictions(data); // somehow needed
|
||||||
return results;
|
return results;
|
||||||
},
|
},
|
||||||
calculateStars(data) {
|
calculateStars(data) {
|
||||||
return 2;
|
return 2;
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user