2021-10-15 09:40:05 +00:00
|
|
|
/* Imports */
|
2022-03-28 17:59:07 +00:00
|
|
|
import { GoogleSpreadsheet } from "google-spreadsheet";
|
2022-03-27 21:10:31 +00:00
|
|
|
|
2022-03-28 17:59:07 +00:00
|
|
|
import { applyIfSecretExists } from "../utils/getSecrets";
|
|
|
|
import { hash } from "../utils/hash";
|
|
|
|
import { calculateStars } from "../utils/stars";
|
2022-04-23 19:44:38 +00:00
|
|
|
import { FetchedQuestion, Platform } from "./";
|
2022-02-11 17:19:33 +00:00
|
|
|
|
2021-10-15 09:40:05 +00:00
|
|
|
/* Definitions */
|
2022-04-01 20:24:35 +00:00
|
|
|
const platformName = "wildeford";
|
2022-02-11 14:21:36 +00:00
|
|
|
const SHEET_ID = "1xcgYF7Q0D95TPHLLSgwhWBHFrWZUGJn7yTyAhDR4vi0"; // spreadsheet key is the long id in the sheets URL
|
|
|
|
const endpoint = `https://docs.google.com/spreadsheets/d/${SHEET_ID}/edit#gid=0`;
|
2022-02-04 19:54:04 +00:00
|
|
|
// https://docs.google.com/spreadsheets/d/1xcgYF7Q0D95TPHLLSgwhWBHFrWZUGJn7yTyAhDR4vi0/edit#gid=0
|
2021-10-15 09:40:05 +00:00
|
|
|
/* Support functions */
|
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
const formatRow = (row) => {
|
|
|
|
let colNames = [
|
|
|
|
"Prediction Date",
|
|
|
|
"Prediction",
|
|
|
|
"Odds",
|
|
|
|
"Actual",
|
|
|
|
"Resolution Date",
|
|
|
|
"Prediction Right?",
|
|
|
|
"Brier Score",
|
|
|
|
"Notes",
|
|
|
|
];
|
|
|
|
let result = {};
|
|
|
|
row.forEach((col, i) => {
|
|
|
|
result[colNames[i]] = col;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
2021-10-15 09:40:05 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
async function fetchGoogleDoc(google_api_key) {
|
2022-01-12 13:03:31 +00:00
|
|
|
// https://gist.github.com/micalevisk/9bc831bd4b3e5a3f62b9810330129c59
|
2022-02-11 14:21:36 +00:00
|
|
|
let results = [];
|
|
|
|
const doc = new GoogleSpreadsheet(SHEET_ID);
|
|
|
|
doc.useApiKey(google_api_key);
|
2021-10-15 09:40:05 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
await doc.loadInfo(); // loads document properties and worksheets
|
|
|
|
console.log(">>", doc.title);
|
2021-10-15 09:40:05 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
const sheet = doc.sheetsByIndex[0];
|
|
|
|
const rows = await sheet.getRows({ offset: 0 });
|
2021-10-15 09:40:05 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log("# " + rows[0]._sheet.headerValues.join(","));
|
2022-01-12 13:03:31 +00:00
|
|
|
let isEnd = false;
|
|
|
|
for (let i in rows) {
|
2022-02-11 14:21:36 +00:00
|
|
|
let data = rows[i]._rawData;
|
|
|
|
if (data.length == 0) isEnd = true;
|
|
|
|
if (!isEnd) {
|
|
|
|
let result = {
|
|
|
|
...formatRow(data),
|
|
|
|
url: endpoint + `&range=A${Number(i) + 2}`,
|
|
|
|
};
|
2022-01-12 13:03:31 +00:00
|
|
|
// +2: +1 for the header row, +1 for starting at 1 and not at 0.
|
|
|
|
// console.log(result)
|
2022-02-11 14:21:36 +00:00
|
|
|
results.push(result);
|
2021-10-15 09:40:05 +00:00
|
|
|
|
2022-01-12 13:03:31 +00:00
|
|
|
// console.log(rows[i])
|
|
|
|
// console.log(rows[i]._rawData)
|
|
|
|
// console.log(rows[i]["Prediction"])
|
|
|
|
}
|
|
|
|
// console.log(row._rawData.join(','))
|
|
|
|
// console.log(row._rawData.join(','))
|
|
|
|
}
|
|
|
|
// console.log(results)
|
2022-02-11 14:21:36 +00:00
|
|
|
return results;
|
2021-10-15 09:40:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
async function processPredictions(predictions) {
|
|
|
|
let currentPredictions = predictions.filter(
|
|
|
|
(prediction) => prediction["Actual"] == "Unknown"
|
|
|
|
);
|
|
|
|
let results = currentPredictions.map((prediction) => {
|
|
|
|
let title = prediction["Prediction"].replace(" [update]", "");
|
2022-04-01 20:24:35 +00:00
|
|
|
let id = `${platformName}-${hash(title)}`;
|
2022-02-11 14:21:36 +00:00
|
|
|
let probability = Number(prediction["Odds"].replace("%", "")) / 100;
|
2022-01-12 13:03:31 +00:00
|
|
|
let options = [
|
|
|
|
{
|
2022-02-11 14:21:36 +00:00
|
|
|
name: "Yes",
|
|
|
|
probability: probability,
|
|
|
|
type: "PROBABILITY",
|
2022-01-12 13:03:31 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-11 14:21:36 +00:00
|
|
|
name: "No",
|
|
|
|
probability: 1 - probability,
|
|
|
|
type: "PROBABILITY",
|
|
|
|
},
|
|
|
|
];
|
2022-04-23 19:44:38 +00:00
|
|
|
let result: FetchedQuestion = {
|
|
|
|
id,
|
|
|
|
title,
|
2022-02-11 14:21:36 +00:00
|
|
|
url: prediction["url"],
|
|
|
|
description: prediction["Notes"] || "",
|
2022-04-23 19:44:38 +00:00
|
|
|
options,
|
|
|
|
timestamp: new Date(Date.parse(prediction["Prediction Date"] + "Z")),
|
2022-02-11 14:21:36 +00:00
|
|
|
qualityindicators: {
|
2022-04-01 20:24:35 +00:00
|
|
|
stars: calculateStars(platformName, null),
|
2022-02-11 14:21:36 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
});
|
2021-10-15 09:40:05 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
results = results.reverse();
|
|
|
|
let uniqueTitles = [];
|
|
|
|
let uniqueResults = [];
|
|
|
|
results.forEach((result) => {
|
|
|
|
if (!uniqueTitles.includes(result.title)) uniqueResults.push(result);
|
|
|
|
uniqueTitles.push(result.title);
|
|
|
|
});
|
|
|
|
return uniqueResults;
|
2021-10-15 09:40:05 +00:00
|
|
|
}
|
2022-03-29 15:10:28 +00:00
|
|
|
|
2021-10-15 09:40:05 +00:00
|
|
|
export async function wildeford_inner(google_api_key) {
|
2022-02-11 14:21:36 +00:00
|
|
|
let predictions = await fetchGoogleDoc(google_api_key);
|
2022-03-29 01:34:27 +00:00
|
|
|
return await processPredictions(predictions);
|
2021-10-15 09:40:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 15:10:28 +00:00
|
|
|
export const wildeford: Platform = {
|
2022-04-01 20:24:35 +00:00
|
|
|
name: platformName,
|
|
|
|
label: "Peter Wildeford",
|
|
|
|
color: "#984158",
|
2022-03-29 15:10:28 +00:00
|
|
|
async fetcher() {
|
|
|
|
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY; // See: https://developers.google.com/sheets/api/guides/authorizing#APIKey
|
|
|
|
return await applyIfSecretExists(GOOGLE_API_KEY, wildeford_inner);
|
|
|
|
},
|
2022-03-29 01:34:27 +00:00
|
|
|
};
|