metaforecast/src/backend/platforms/givewellopenphil.ts

88 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-02-16 14:18:23 +00:00
/* Imports */
import axios from "axios";
import fs from "fs";
import { calculateStars } from "../utils/stars";
import { Platform } from "./";
2021-02-16 14:18:23 +00:00
const platformName = "givewellopenphil";
2021-02-16 14:18:23 +00:00
/* Support functions */
async function fetchPage(url: string) {
2021-03-02 13:29:27 +00:00
let response = await axios({
2021-02-16 14:18:23 +00:00
url: url,
method: "GET",
headers: {
"Content-Type": "text/html",
},
}).then((res) => res.data);
return response;
2021-02-16 14:18:23 +00:00
}
/* Body */
2022-02-14 20:39:30 +00:00
async function main1() {
2022-03-16 21:02:34 +00:00
let rawdata = fs.readFileSync("./input/givewellopenphil-urls.txt");
let data = rawdata
.toString()
.split("\n")
.filter((url) => url != "");
2021-04-08 19:32:03 +00:00
// console.log(data)
let results = [];
2021-03-02 13:29:27 +00:00
for (let url of data) {
2021-04-08 19:32:03 +00:00
// console.log(url)
let page = await fetchPage(url);
2021-03-02 13:29:27 +00:00
2021-02-16 14:18:23 +00:00
// Title
let titleraw = page.split('<meta name="twitter:title" content="')[1];
let title = titleraw.split('" />')[0];
2021-03-02 13:29:27 +00:00
2021-02-16 14:18:23 +00:00
// Description
let internalforecasts = page
.split("<h2")
.filter(
(section) =>
section.includes("Internal forecast") ||
section.includes("internal forecast")
);
let description = "<h2 " + internalforecasts[1];
2021-03-02 13:29:27 +00:00
2021-02-16 14:18:23 +00:00
let result = {
title: title,
url: url,
platform: platformName,
description: description,
timestamp: new Date().toISOString(),
qualityindicators: {
stars: calculateStars(platformName, {}),
},
}; // Note: This requires some processing afterwards
2021-04-08 19:32:03 +00:00
// console.log(result)
results.push(result);
2021-02-16 14:18:23 +00:00
}
// await databaseUpsert({
// contents: results,
// group: "givewell-questions-unprocessed",
// });
2022-02-14 20:39:30 +00:00
}
export const givewellopenphil: Platform = {
name: platformName,
label: "GiveWell/OpenPhilanthropy",
color: "#32407e",
async fetcher() {
// main1()
return; // not necessary to refill the DB every time
const rawdata = fs.readFileSync("./input/givewellopenphil-questions.json", {
encoding: "utf-8",
});
const data = JSON.parse(rawdata);
const dataWithDate = data.map((datum: any) => ({
...datum,
platform: platformName,
timestamp: "2021-02-23",
}));
return dataWithDate;
},
};