From 3230c0d0c88edb93770bb1046adc9f159439969b Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Thu, 19 May 2022 15:56:37 -0400 Subject: [PATCH] feat: add contrib folder --- contrib/README.md | 3 ++ contrib/download-all.mjs | 91 +++++++++++++++++++++++++++++++++++++++ contrib/iframe-snippet.md | 12 ++++++ 3 files changed, 106 insertions(+) create mode 100644 contrib/README.md create mode 100644 contrib/download-all.mjs create mode 100644 contrib/iframe-snippet.md diff --git a/contrib/README.md b/contrib/README.md new file mode 100644 index 0000000..f7a4841 --- /dev/null +++ b/contrib/README.md @@ -0,0 +1,3 @@ +## User contributions + +This folder contains utilities and snippets contributed by users. We welcome contributions. diff --git a/contrib/download-all.mjs b/contrib/download-all.mjs new file mode 100644 index 0000000..ec7dc7c --- /dev/null +++ b/contrib/download-all.mjs @@ -0,0 +1,91 @@ +/* Imports */ +import fs from "fs"; +import axios from "axios"; + +/* Definitions */ +const VERBOSE = true; +let print = (message) => (VERBOSE ? console.log(message) : null); +let graphQLendpoint = "https://metaforecast.org/api/graphql"; +let buildQuery = (endCursor) => `{ + questions(first: 1000 ${!!endCursor ? `after: "${endCursor}"` : ""}) { + edges { + node { + id + title + url + description + options { + name + probability + } + qualityIndicators { + numForecasts + stars + } + timestamp + } + } + pageInfo { + endCursor + startCursor + } + } +} +`; + +/* Support functions */ +let getSomeMetaforecastPredictions = async (query) => { + let response = await axios({ + url: graphQLendpoint, + method: "POST", + headers: { "Content-Type": "application/json" }, + data: JSON.stringify({ query: query }), + }) + .then((res) => res.data) + .then((res) => res.data); // not a typo + return response; +}; + +let save = (questions) => { + fs.writeFileSync("forecasts.json", JSON.stringify(questions, null, 4)); + let tsvHeaders = "title\tplatform\tdate\tforecast\n"; + let tsvRows = questions + .map( + (question) => + `${question.title}\t${question.platform}\t${ + question.timestamp + }\t${JSON.stringify(question.options)}` + ) + .join("\n"); + let tsvFile = tsvHeaders + tsvRows; + print("Saving results to results.tsv"); + fs.writeFileSync("forecasts.tsv", tsvFile); +}; + +let getNodes = (questions) => { + let edges = questions.edges; + let nodes = edges.map((edge) => edge.node); + return nodes; +}; +// main +let getAllMetaforecastPredictions = async () => { + print("Fetching forecasts"); + let results = []; + let firstQuery = await getSomeMetaforecastPredictions(buildQuery()); + results.push(...getNodes(firstQuery.questions)); + let endCursor = firstQuery.questions.pageInfo.endCursor; + while (endCursor) { + print("Cursor: " + endCursor); + let queryResults = await getSomeMetaforecastPredictions( + buildQuery(endCursor) + ); + let nodes = getNodes(queryResults.questions); + results.push(...nodes); + endCursor = queryResults.questions.pageInfo.endCursor; + } + //results = results.map((result) => result.node); + save(results); + return results; +}; + +getAllMetaforecastPredictions(); diff --git a/contrib/iframe-snippet.md b/contrib/iframe-snippet.md new file mode 100644 index 0000000..a028822 --- /dev/null +++ b/contrib/iframe-snippet.md @@ -0,0 +1,12 @@ +iframe snippet as used on [Global Guessing](https://globalguessing.com/russia-ukraine-forecasts/) + +```html + +```