feat: save further insight progress.

This commit is contained in:
NunoSempere 2022-10-28 12:37:28 +01:00
parent f5bf50456a
commit bf89e4b11d

View File

@ -1,20 +1,32 @@
/* Imports */ /* Imports */
import {or} from "ajv/dist/compile/codegen";
import axios from "axios"; import axios from "axios";
import {FetchedQuestion, Platform} from "."; import {FetchedQuestion, Platform} from ".";
import {QuestionOption} from "../../common/types";
import toMarkdown from "../utils/toMarkdown"; import toMarkdown from "../utils/toMarkdown";
/* Definitions */ /* Definitions */
const platformName = "insight"; const platformName = "insight";
const marketsEnpoint = "https://insightprediction.com/api/markets?orderBy=is_resolved&sortedBy=asc"; const marketsEnpoint = "https://insightprediction.com/api/markets?orderBy=is_resolved&sortedBy=asc";
const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/markets/${id}`; const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/markets/${id}`;
const SPORTS_CATEGORIES = [
'World Cup',
'MLB',
'Futures',
'Sports',
'EPL',
'Golf',
'NHL',
'College Football'
]
/* Support functions */ /* Support functions */
// Stubs // Stubs
const excludeMarketFromTitle = (title : any) => { const excludeMarketFromTitle = (title : any) => {
if (!!title) { if (!!title) {
return title.includes(" vs ") || title.includes(" Over: ") return title.includes(" vs ") || title.includes(" Over: ") || title.includes("NFL") || title.includes("Will there be a first time winner") || title.includes("Premier League")
} else { } else {
return true return true
} }
@ -44,6 +56,15 @@ const processDescriptionText = (text : any) => {
} }
} }
const getOrderbookPrize = (orderbook : any) => {
let yes_min_cents = orderbook.yes.buy[0].price
let yes_max_cents = orderbook.yes.sell[0].price
let yes_min = Number(yes_min_cents.slice(0, -1))
let yes_max = Number(yes_max_cents.slice(0, -1))
let yes_price_orderbook = geomMean(yes_min, yes_max)
return yes_price_orderbook
}
// Fetching // Fetching
async function fetchPage(bearer: string, pageNum: number) { async function fetchPage(bearer: string, pageNum: number) {
let pageUrl = `${marketsEnpoint}&page=${pageNum}` let pageUrl = `${marketsEnpoint}&page=${pageNum}`
@ -76,9 +97,12 @@ async function fetchMarket(bearer: string, marketId: number) {
} }
const processMarket = (market : any) => { const processMarket = (market : any) => {
let hasData = !!market && !!market.answer && !!market.answer.data let hasData = !!market && !!market.answer && !!market.answer.data
const id = `${platformName}-${
market.id
}`;
if (hasData) { if (hasData) {
let data = market.answer.data let data = market.answer.data
// console.log("has data") // console.log("has data")
@ -87,16 +111,10 @@ const processMarket = (market : any) => {
// console.log("has orderbook") // console.log("has orderbook")
// console.log(JSON.stringify(orderbook, null, 2)) // console.log(JSON.stringify(orderbook, null, 2))
if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) { // console.log("has active orderbook") if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) { // console.log("has active orderbook")
let yes_min_cents = orderbook.yes.buy[0].price
let yes_max_cents = orderbook.yes.sell[0].price
let yes_min = Number(yes_min_cents.slice(0, -1))
let yes_max = Number(yes_max_cents.slice(0, -1))
let yes_price_orderbook = geomMean(yes_min, yes_max)
let latest_yes_price = data[0].latest_yes_price let latest_yes_price = data[0].latest_yes_price
let yes_price_orderbook = getOrderbookPrize(orderbook)
let yes_probability = latest_yes_price ? geomMean(latest_yes_price, yes_price_orderbook) : yes_price_orderbook let yes_probability = latest_yes_price ? geomMean(latest_yes_price, yes_price_orderbook) : yes_price_orderbook
const id = `${platformName}-${
market.id
}`;
const probability = yes_probability / 100; const probability = yes_probability / 100;
const options: FetchedQuestion["options"] = [ const options: FetchedQuestion["options"] = [
{ {
@ -125,7 +143,35 @@ const processMarket = (market : any) => {
return null return null
} }
} else { // non binary question } else { // non binary question
return null // for now console.log("Non-binary question")
console.log(market)
console.log(data)
let options = []
for (let answer of data) {
let orderbook = answer.orderbook
if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) {
let latest_yes_price = answer.latest_yes_price
let yes_price_orderbook = getOrderbookPrize(orderbook)
let yes_probability = latest_yes_price ? geomMean(latest_yes_price, yes_price_orderbook) : yes_price_orderbook
let newOption: QuestionOption = ({
name: String(answer.title),
probability: Number(yes_probability / 100),
type: "PROBABILITY"
});
options.push(newOption)
}
}
const result: FetchedQuestion = {
id: id,
title: market.title,
url: market.url,
description: processDescriptionText(market.rules),
options,
qualityindicators: market.coin_id == "USD" ? (
{volume: market.volume}
) : ({})
};
return result;
} }
} else { } else {
return null return null
@ -133,7 +179,7 @@ const processMarket = (market : any) => {
} }
async function fetchAllMarkets(bearer: string) { async function fetchAllMarkets(bearer: string) {
let pageNum = 1 let pageNum = 2559
let markets = [] let markets = []
let categories = [] let categories = []
let isEnd = false let isEnd = false
@ -153,18 +199,18 @@ async function fetchAllMarkets(bearer: string) {
let fullMarketData = fullMarketDataResponse.data let fullMarketData = fullMarketDataResponse.data
let processedMarketData = processMarket(fullMarketData) let processedMarketData = processMarket(fullMarketData)
console.log(`- Adding: ${ if (processedMarketData != null && ! SPORTS_CATEGORIES.includes(fullMarketData.category)) {
fullMarketData.title console.log(`- Adding: ${
}`) fullMarketData.title
console.group() }`)
console.log(fullMarketData) console.group()
console.log(JSON.stringify(processedMarketData, null, 2)) console.log(fullMarketData)
console.groupEnd() console.log(JSON.stringify(processedMarketData, null, 2))
console.groupEnd()
if (processedMarketData != null) {
markets.push(processedMarketData) markets.push(processedMarketData)
} }
let category = fullMarketData.category let category = fullMarketData.category
categories.push(category) categories.push(category)