metaforecast/src/backend/platforms/givewellopenphil-fetch.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-02-16 14:18:23 +00:00
/* Imports */
import axios from "axios";
2022-03-16 21:02:34 +00:00
import fs from "fs";
import { databaseUpsert } from "../database/database-wrapper";
import { calculateStars } from "../utils/stars";
2021-02-16 14:18:23 +00:00
/* Support functions */
2021-03-02 13:29:27 +00:00
async function fetchPage(url) {
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);
2021-02-16 14:18:23 +00:00
//console.log(response)
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: "GiveWell",
description: description,
timestamp: new Date().toISOString(),
qualityindicators: {
stars: calculateStars("GiveWell/OpenPhilanthropy", {}),
},
}; // 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
}
2022-02-14 20:39:30 +00:00
await databaseUpsert({
contents: results,
group: "givewell-questions-unprocessed",
});
}
// main1()
2022-02-14 20:39:30 +00:00
async function main2() {
2022-03-16 21:02:34 +00:00
let rawdata = fs.readFileSync("./input/givewellopenphil-questions.json");
2022-02-14 20:39:30 +00:00
let data = JSON.parse(rawdata);
2022-03-16 21:02:34 +00:00
let dataWithDate = data.map((datum) => ({
...datum,
timestamp: "2021-02-23",
}));
2022-02-14 20:39:30 +00:00
await databaseUpsert({ group: "givewellopenphil", contents: dataWithDate });
2021-02-16 14:18:23 +00:00
}
2022-02-14 20:39:30 +00:00
main2();