metaforecast/src/backend/platforms/predictit-fetch.js

113 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-01-12 12:43:41 +00:00
/* Imports */
import axios from "axios";
import { databaseUpsert } from "../database/database-wrapper";
import { calculateStars } from "../utils/stars";
import toMarkdown from "../utils/toMarkdown";
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/",
});
let openMarkets = response.data.markets.filter(
(market) => market.status == "Open"
);
return openMarkets;
2021-01-12 12:43:41 +00:00
}
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",
url: "https://www.predictit.org/api/Market/" + market_id,
});
return response.data.rule;
2021-02-03 17:35:38 +00:00
}
async function fetchmarketvolumes() {
let response = await axios({
method: "get",
url: "https://predictit-f497e.firebaseio.com/marketStats.json",
});
return response.data;
}
2021-02-03 17:35:38 +00:00
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
2021-02-03 17:35:38 +00:00
}
2021-01-12 12:43:41 +00:00
/* Body */
2021-03-02 13:29:27 +00:00
export async function predictit() {
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 markets) {
// console.log(market.name)
let id = `predictit-${market.id}`;
2021-02-03 17:35:38 +00:00
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 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,
probability: contract.lastTradePrice,
type: "PROBABILITY",
}));
let totalValue = options
.map((element) => Number(element.probability))
.reduce((a, b) => a + b, 0);
2021-03-02 13:29:27 +00:00
if (options.length != 1 && totalValue > 1) {
options = options.map((element) => ({
...element,
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",
2021-03-02 13:29:27 +00:00
},
{
name: "No",
probability: 1 - probability,
type: "PROBABILITY",
},
];
}
let obj = {
id: id,
title: market["name"],
url: market.url,
platform: "PredictIt",
description: description,
options: options,
timestamp: new Date().toISOString(),
qualityindicators: {
stars: calculateStars("PredictIt", {}),
shares_volume: shares_volume,
},
};
2021-04-08 20:51:02 +00:00
// console.log(obj)
results.push(obj);
2021-02-03 17:35:38 +00:00
}
await databaseUpsert({ contents: results, group: "predictit" });
console.log("Done");
2021-01-12 12:43:41 +00:00
}