metaforecast/src/platforms/givewellopenphil-fetch.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-02-16 14:18:23 +00:00
/* Imports */
import fs from "fs";
import axios from "axios";
import toMarkdown from "../utils/toMarkdown.js";
import { calculateStars } from "../utils/stars.js";
import { databaseUpsert } from "../database/database-wrapper.js";
2021-02-16 14:18:23 +00:00
/* Definitions */
let locationData = "./data/";
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 */
2021-03-02 13:29:27 +00:00
async function main() {
let rawdata = fs.readFileSync("./src/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
}
// let string = JSON.stringify(results, null, 2)
// fs.writeFileSync('./data/givewell-questions-unprocessed.json', string);
await databaseUpsert({ contents: results, group: "givewell-questions-unprocessed" });
2021-02-16 14:18:23 +00:00
}
main();