Merge pull request #47 from QURIresearch/rootclaim-fix

Fix rootclaim fetcher
This commit is contained in:
Nuño Sempere 2022-04-06 21:14:30 -04:00 committed by GitHub
commit 7c89a1c6ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 931 additions and 27 deletions

879
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,7 @@
"dependencies": { "dependencies": {
"@tailwindcss/forms": "^0.4.0", "@tailwindcss/forms": "^0.4.0",
"@tailwindcss/typography": "^0.5.1", "@tailwindcss/typography": "^0.5.1",
"@types/jsdom": "^16.2.14",
"@types/nprogress": "^0.2.0", "@types/nprogress": "^0.2.0",
"@types/react": "^17.0.39", "@types/react": "^17.0.39",
"airtable": "^0.11.1", "airtable": "^0.11.1",
@ -48,6 +49,7 @@
"html-to-image": "^1.7.0", "html-to-image": "^1.7.0",
"https": "^1.0.0", "https": "^1.0.0",
"isomorphic-fetch": "^3.0.0", "isomorphic-fetch": "^3.0.0",
"jsdom": "^19.0.0",
"json2csv": "^5.0.5", "json2csv": "^5.0.5",
"multiselect-react-dropdown": "^2.0.17", "multiselect-react-dropdown": "^2.0.17",
"next": "12", "next": "12",
@ -82,7 +84,7 @@
"devDependencies": { "devDependencies": {
"@netlify/plugin-nextjs": "^4.2.4", "@netlify/plugin-nextjs": "^4.2.4",
"@svgr/cli": "^6.2.1", "@svgr/cli": "^6.2.1",
"netlify-cli": "^9.13.6", "@types/pg": "^8.6.5",
"@types/pg": "^8.6.5" "netlify-cli": "^9.13.6"
} }
} }

View File

@ -1,55 +1,82 @@
import axios from "axios"; import axios from "axios";
import { JSDOM } from "jsdom";
import { calculateStars } from "../utils/stars"; import { calculateStars } from "../utils/stars";
import toMarkdown from "../utils/toMarkdown"; import toMarkdown from "../utils/toMarkdown";
import { Forecast, Platform } from "./"; import { Forecast, Platform } from "./";
/* Definitions */
const platformName = "rootclaim"; const platformName = "rootclaim";
let jsonEndpoint = const jsonEndpoint =
"https://www.rootclaim.com/main_page_stories?number=100&offset=0"; //"https://subgraph-matic.poly.market/subgraphs/name/TokenUnion/polymarket"//"https://subgraph-backup.poly.market/subgraphs/name/TokenUnion/polymarket"//'https://subgraph-matic.poly.market/subgraphs/name/TokenUnion/polymarket3' "https://live-rootclaim-backend.herokuapp.com/analysis/public-list?limit=1000&offset=0";
async function fetchAllRootclaims() { const fetchAllRootclaims = async () => {
// for info which the polymarket graphql API console.log(`Fetching ${jsonEndpoint}`);
let response = await axios const response = await axios
.get(jsonEndpoint) .get(jsonEndpoint)
.then((response) => response.data); .then((response) => response.data);
if (response.length != response[0] + 1) {
console.log(response.length); const claims = response.result.main_page_stories;
console.log(response[0]); if (typeof claims !== "object") {
//throw Error("Rootclaim's backend has changed.") throw new Error("Expected result.main_page_stories field in API response");
} }
response.shift(); return claims;
return response; };
}
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 = { export const rootclaim: Platform = {
name: platformName, name: platformName,
label: "Rootclaim", label: "Rootclaim",
color: "#0d1624", color: "#0d1624",
async fetcher() { async fetcher() {
let claims = await fetchAllRootclaims(); const claims = await fetchAllRootclaims();
let results: Forecast[] = []; const results: Forecast[] = [];
for (let claim of claims) {
let id = `${platformName}-${claim.slug.toLowerCase()}`; for (const claim of claims) {
const id = `${platformName}-${claim.slug.toLowerCase()}`;
let options = []; let options = [];
for (let scenario of claim.scenarios) { for (let scenario of claim.scenarios) {
//console.log(scenario)
options.push({ options.push({
name: toMarkdown(scenario.text) name: toMarkdown(scenario.name || scenario.text)
.replace("\n", "") .replace("\n", "")
.replace("'", "'"), .replace("'", "'"),
probability: scenario.net_prob / 100, probability: scenario.net_prob / 100,
type: "PROBABILITY", type: "PROBABILITY",
}); });
} }
let claimUrlPath = claim.created_at < "2020" ? "claims" : "analysis";
let claimUrlPath = claim.isclaim ? "claims" : "analysis";
const url = `https://www.rootclaim.com/${claimUrlPath}/${claim.slug}`;
const description = await fetchDescription(url, claim.isclaim);
let obj: Forecast = { let obj: Forecast = {
id: id, id,
title: toMarkdown(claim.question).replace("\n", ""), title: toMarkdown(claim.question).replace("\n", ""),
url: `https://www.rootclaim.com/${claimUrlPath}/${claim.slug}`, url,
platform: platformName, platform: platformName,
description: toMarkdown(claim.background).replace("&#39;", "'"), description: toMarkdown(description).replace("&#39;", "'"),
options: options, options: options,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
qualityindicators: { qualityindicators: {