tweak: Improvements to PredictIt fetcher

This commit is contained in:
NunoSempere 2021-09-14 12:25:28 +02:00
parent d9b654d19e
commit c08ee19479

View File

@ -12,7 +12,8 @@ async function fetchmarkets() {
url: 'https://www.predictit.org/api/marketdata/all/'
})
return response.data.markets
let openMarkets = response.data.markets.filter(market => market.status == "Open")
return openMarkets
}
async function fetchmarketrules(market_id) {
@ -23,6 +24,14 @@ async function fetchmarketrules(market_id) {
return response.data.rule
}
async function fetchmarketvolumes(){
let response = await axios({
method: 'get',
url: "https://predictit-f497e.firebaseio.com/marketStats.json"
})
return response.data
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@ -30,16 +39,25 @@ function sleep(ms) {
/* Body */
export async function predictit() {
let response = await fetchmarkets()
// console.log(response)
let markets = await fetchmarkets()
let marketVolumes = await fetchmarketvolumes()
markets = markets.map(market => ({
...market,
TotalSharesTraded: marketVolumes[market.id]["TotalSharesTraded"]
}))
// console.log(markets)
let results = []
for (let market of response) {
for (let market of markets) {
// console.log(market.name)
let isbinary = market.contracts.length == 1;
await sleep(3000 * (1 + Math.random()))
let descriptionraw = await fetchmarketrules(market.id)
let descriptionprocessed1 = toMarkdown(descriptionraw)
let description = descriptionprocessed1
let percentageFormatted = isbinary ? Number(Number(market.contracts[0].lastTradePrice) * 100).toFixed(0) + "%" : "none"
let shares_volume = market["TotalSharesTraded"]
// let percentageFormatted = isbinary ? Number(Number(market.contracts[0].lastTradePrice) * 100).toFixed(0) + "%" : "none"
let options = market.contracts.map(contract => ({
"name": contract.name,
@ -80,9 +98,9 @@ export async function predictit() {
"options": options,
"timestamp": new Date().toISOString(),
"qualityindicators": {
"stars": calculateStars("PredictIt", ({}))
"stars": calculateStars("PredictIt", ({})),
"shares_volume": shares_volume
}
})
// console.log(obj)
results.push(obj)