From d3d41d94e1def47d9f7af1b1e936d6e3d0e636f3 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 1 Aug 2021 00:34:41 +0200 Subject: [PATCH] chore: commit before npm audit fix --- src/platforms/betfair-fetch.js | 113 +++++++++++++++++++++++++++++++++ src/platforms/example-fetch.js | 70 ++++++++++++++++++++ src/platforms/kalshi-fetch.js | 2 +- 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 src/platforms/betfair-fetch.js create mode 100644 src/platforms/example-fetch.js diff --git a/src/platforms/betfair-fetch.js b/src/platforms/betfair-fetch.js new file mode 100644 index 0000000..a15092a --- /dev/null +++ b/src/platforms/betfair-fetch.js @@ -0,0 +1,113 @@ +/* Imports */ +import fs from 'fs' +import axios from "axios" +import https from 'https'; +import toMarkdown from "../utils/toMarkdown.js" +import { calculateStars } from "../utils/stars.js" +import {upsert} from "../utils/mongo-wrapper.js" + +/* Definitions */ +let endpoint = process.env.SECRET_BETFAIR_ENDPOINT + +/* Support functions */ + +async function fetchPredictions() { + const agent = new https.Agent({ + rejectUnauthorized: false + }); + let response = await axios({ + url: endpoint, + method: 'GET', + headers: ({ + 'Content-Type': 'text/html', + }), + httpsAgent: agent + }) + .then(response => response.data) + // console.log(response) + return response +} + +async function whipIntoShape(data){ + let arraysEqual = (a, b) => { + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length !== b.length) return false; + + // If you don't care about the order of the elements inside + // the array, you should sort both arrays here. + // Please note that calling sort on an array will modify that array. + // you might want to clone your array first. + + for (var i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } + return true; + } + + let catalogues = data.market_catalogues + let books = data.market_books + let keys1 = Object.keys(catalogues) + let keys2 = Object.keys(books) + let results = ({}) + if(!arraysEqual(keys1, keys2)){ + throw new Error("Betfair: Error in endpoint; Betfair catalogues and books do not match") + }else{ + for(catalogue in catalogues){ + console.log(catalogue) + } + for(book in books){ + console.log(book) + } + } + return results +} + + +async function processPredictions(predictions) { + + let results = await predictions.map(prediction => { + let probability = prediction.probability + let options = [ + { + "name": "Yes", + "probability": probability, + "type": "PROBABILITY" + }, + { + "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 +} + +/* Body */ + +export async function betfair() { + let predictions = await fetchPredictions() + whipIntoShape(predictions) + let results = await processPredictions(predictions) // somehow needed + // console.log(results) + // let string = JSON.stringify(results, null, 2) + // fs.writeFileSync('polyprediction-questions.json', string); + // await upsert(results, "example-questions") + console.log("Done") +} +betfair() diff --git a/src/platforms/example-fetch.js b/src/platforms/example-fetch.js new file mode 100644 index 0000000..91ff2f0 --- /dev/null +++ b/src/platforms/example-fetch.js @@ -0,0 +1,70 @@ +/* Imports */ +import fs from 'fs' +import axios from "axios" +import toMarkdown from "../utils/toMarkdown.js" +import { calculateStars } from "../utils/stars.js" +import {upsert} from "../utils/mongo-wrapper.js" + +/* Definitions */ +let endpoint = 'https://example.com/' + +/* Support functions */ + +async function fetchPredictions() { + let response = await axios({ + url: endpoint, + method: 'GET', + headers: ({ + 'Content-Type': 'text/html', + }), + }) + .then(response => response.data) + // console.log(response) + return response +} + +async function processPredictions(predictions) { + let results = await predictions.map(prediction => { + let probability = prediction.probability + let options = [ + { + "name": "Yes", + "probability": probability, + "type": "PROBABILITY" + }, + { + "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 +} + +/* Body */ + +export async function example() { + let predictions = await fetchPredictions() + let results = await processPredictions(predictions) // somehow needed + // console.log(results) + // let string = JSON.stringify(results, null, 2) + // fs.writeFileSync('polyprediction-questions.json', string); + await upsert(results, "example-questions") + console.log("Done") +} +//example() diff --git a/src/platforms/kalshi-fetch.js b/src/platforms/kalshi-fetch.js index 1379d34..929a4be 100644 --- a/src/platforms/kalshi-fetch.js +++ b/src/platforms/kalshi-fetch.js @@ -15,7 +15,7 @@ async function fetchAllContractInfo(){ // for info which the polymarket graphql } */ async function fetchAllMarkets() { // for info which the polymarket graphql API - let response = await axios.get('https://trading-api.kalshi.com/v1/cached/markets/').then(response => response.data.markets) + let response = await axios.get(jsonEndpoint).then(response => response.data.markets) // console.log(response) return response }