metaforecast/src/backend/platforms/example-fetch.ts

71 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-07-31 22:34:41 +00:00
/* Imports */
import axios from "axios";
import { databaseUpsert } from "../database/database-wrapper";
import { calculateStars } from "../utils/stars";
2021-07-31 22:34:41 +00:00
/* Definitions */
let endpoint = "https://example.com/";
2021-07-31 22:34:41 +00:00
/* Support functions */
2021-07-31 23:55:08 +00:00
async function fetchData() {
2021-07-31 22:34:41 +00:00
let response = await axios({
url: endpoint,
method: "GET",
headers: {
"Content-Type": "text/html",
},
}).then((response) => response.data);
2021-07-31 22:34:41 +00:00
// console.log(response)
return response;
2021-07-31 22:34:41 +00:00
}
async function processPredictions(predictions) {
let results = await predictions.map((prediction) => {
let id = `platform-${prediction.id}`;
let probability = prediction.probability;
2021-07-31 22:34:41 +00:00
let options = [
{
name: "Yes",
probability: probability,
type: "PROBABILITY",
2021-07-31 22:34:41 +00:00
},
{
name: "No",
probability: 1 - probability,
type: "PROBABILITY",
},
];
let result = {
title: prediction.title,
url: `https://example.com`,
platform: "Example",
description: prediction.description,
options: options,
timestamp: new Date().toISOString(),
qualityindicators: {
stars: calculateStars("Example", {
/* some: somex, factors: factors */
}),
other: prediction.otherx,
indicators: prediction.indicatorx,
},
};
return result;
});
return results; //resultsProcessed
2021-07-31 22:34:41 +00:00
}
/* Body */
export async function example() {
let data = await fetchData();
let results = await processPredictions(data); // somehow needed
2021-07-31 22:34:41 +00:00
// console.log(results)
// let string = JSON.stringify(results, null, 2)
await databaseUpsert({ contents: results, group: "example" });
console.log("Done");
2021-07-31 22:34:41 +00:00
}
//example()