-
Notifications
You must be signed in to change notification settings - Fork 6
/
rootclaim.ts
97 lines (83 loc) · 2.84 KB
/
rootclaim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import axios from "axios";
import { JSDOM } from "jsdom";
import { average } from "../../utils";
import toMarkdown from "../utils/toMarkdown";
import { FetchedQuestion, Platform } from "./";
const platformName = "rootclaim";
const jsonEndpoint =
"https://live-rootclaim-backend.herokuapp.com/analysis/public-list?limit=1000&offset=0";
const fetchAllRootclaims = async () => {
console.log(`Fetching ${jsonEndpoint}`);
const response = await axios
.get(jsonEndpoint)
.then((response) => response.data);
const claims = response.result.main_page_stories;
if (typeof claims !== "object") {
throw new Error("Expected result.main_page_stories field in API response");
}
return claims;
};
const fetchDescription = async (url: string, isclaim: boolean) => {
console.log(`Fetching description for ${url}`);
const response = await axios.get(url).then((response) => response.data);
const { document } = new JSDOM(response).window;
const nextDataEl = document.querySelector("#__NEXT_DATA__");
if (!nextDataEl) {
throw new Error(`Couldn't find __NEXT_DATA__ for ${url}`);
}
const data = JSON.parse(nextDataEl.innerHTML);
const mainData = data?.props?.pageProps?.initialReduxState?.main;
const info = isclaim
? mainData?.claim?.background
: mainData?.analise?.background_info;
if (!info) {
throw new Error(`Couldn't find description for page ${url}`);
}
return info;
};
export const rootclaim: Platform = {
name: platformName,
label: "Rootclaim",
color: "#0d1624",
version: "v1",
async fetcher() {
const claims = await fetchAllRootclaims();
const results: FetchedQuestion[] = [];
for (const claim of claims) {
const id = `${platformName}-${claim.slug.toLowerCase()}`;
let options: FetchedQuestion["options"] = [];
for (let scenario of claim.scenarios) {
options.push({
name: toMarkdown(scenario.name || scenario.text)
.replace("\n", "")
.replace("'", "'"),
probability: scenario.net_prob / 100,
type: "PROBABILITY",
});
}
let claimUrlPath = claim.isclaim ? "claims" : "analysis";
const url = `https://www.rootclaim.com/${claimUrlPath}/${claim.slug}`;
const description = await fetchDescription(url, claim.isclaim);
let obj: FetchedQuestion = {
id,
title: toMarkdown(claim.question).replace("\n", ""),
url,
description: toMarkdown(description).replace("'", "'"),
options,
qualityindicators: {
numforecasts: 1,
},
};
results.push(obj);
}
return results;
},
calculateStars(data) {
let nuno = () => 4;
let eli = () => null;
let misha = () => null;
let starsDecimal = average([nuno() /*, eli(data), misha(data)*/]);
let starsInteger = Math.round(starsDecimal);
return starsInteger;
},
};