metaforecast/src/predictit-fetch.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-01-12 12:43:41 +00:00
/* Imports */
import fs from 'fs'
import axios from "axios"
2021-02-16 14:18:23 +00:00
import toMarkdown from "./toMarkdown.js"
2021-03-02 13:29:27 +00:00
import { calculateStars } from "./stars.js"
2021-01-12 12:43:41 +00:00
/* Support functions */
2021-03-02 13:29:27 +00:00
async function fetchmarkets() {
2021-01-12 12:43:41 +00:00
let response = await axios({
method: 'get',
url: 'https://www.predictit.org/api/marketdata/all/'
})
return response.data.markets
}
2021-03-02 13:29:27 +00:00
async function fetchmarketrules(market_id) {
2021-02-03 17:35:38 +00:00
let response = await axios({
method: 'get',
2021-03-02 13:29:27 +00:00
url: 'https://www.predictit.org/api/Market/' + market_id
2021-02-03 17:35:38 +00:00
})
return response.data.rule
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
2021-01-12 12:43:41 +00:00
/* Body */
2021-03-02 13:29:27 +00:00
export async function predictit() {
2021-01-12 12:43:41 +00:00
let response = await fetchmarkets()
console.log(response)
2021-03-02 13:29:27 +00:00
let result = []
for (let market of response) {
2021-02-03 17:35:38 +00:00
let isbinary = market.contracts.length == 1;
2021-03-02 13:29:27 +00:00
await sleep(3000 * (1 + Math.random()))
2021-02-03 17:35:38 +00:00
let descriptionraw = await fetchmarketrules(market.id)
2021-02-16 14:18:23 +00:00
let descriptionprocessed1 = toMarkdown(descriptionraw)
2021-03-02 13:29:27 +00:00
let description = descriptionprocessed1
let percentageFormatted = isbinary ? Number(Number(market.contracts[0].lastTradePrice) * 100).toFixed(0) + "%" : "none"
let options = market.contracts.map(contract => ({
"name": contract.name,
"probability": contract.lastTradePrice,
"type": "PROBABILITY"
}))
let totalValue = options
2021-03-02 13:29:27 +00:00
.map(element => Number(element.probability))
.reduce((a, b) => (a + b), 0)
if (options.length != 1 && totalValue > 1) {
options = options.map(element => ({
...element,
2021-03-02 13:29:27 +00:00
probability: Number(element.probability) / totalValue
}))
2021-03-02 13:29:27 +00:00
} else if (options.length == 1) {
let option = options[0]
let probability = option["probability"]
options = [
2021-03-02 13:29:27 +00:00
{
"name": "Yes",
"probability": probability,
"type": "PROBABILITY"
},
{
"name": "No",
"probability": 1 - probability,
"type": "PROBABILITY"
}
]
}
2021-02-03 17:35:38 +00:00
let obj = ({
"title": market["name"],
"url": market.url,
"platform": "PredictIt",
"description": description,
2021-04-07 20:29:21 +00:00
"options": options,
"timestamp": new Date().toISOString(),
"qualityindicators": {
"stars": calculateStars("PredictIt", ({}))
}
2021-01-12 12:43:41 +00:00
})
2021-02-03 17:35:38 +00:00
console.log(obj)
result.push(obj)
}
2021-01-12 12:43:41 +00:00
//console.log(result)
2021-03-02 13:29:27 +00:00
let string = JSON.stringify(result, null, 2)
2021-01-12 12:43:41 +00:00
fs.writeFileSync('./data/predictit-questions.json', string);
console.log("Done")
}