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