-
Notifications
You must be signed in to change notification settings - Fork 6
/
guesstimate.ts
99 lines (88 loc) · 2.82 KB
/
guesstimate.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
98
99
import axios from "axios";
import { Question } from "@prisma/client";
import { ElasticQuestion, questionToElasticDocument } from "../utils/elastic";
import {
FetchedQuestion,
Platform,
prepareQuestion,
upsertSingleQuestion,
} from "./";
/* Definitions */
const searchEndpoint =
"https://m629r9ugsg-dsn.algolia.net/1/indexes/Space_production/query?x-algolia-agent=Algolia%20for%20vanilla%20JavaScript%203.32.1&x-algolia-application-id=M629R9UGSG&x-algolia-api-key=4e893740a2bd467a96c8bfcf95b2809c";
const apiEndpoint = "https://api.getguesstimate.com";
const modelToQuestion = (model: any): ReturnType<typeof prepareQuestion> => {
const { description } = model;
// const description = model.description
// ? model.description.replace(/\n/g, " ").replace(/ /g, " ")
// : "";
// const timestamp = parseISO(model.created_at);
const fq: FetchedQuestion = {
id: `guesstimate-${model.id}`,
title: model.name,
url: `https://www.getguesstimate.com/models/${model.id}`,
// timestamp,
description: description || "",
options: [],
qualityindicators: {
numforecasts: 1,
numforecasters: 1,
},
extra: {
visualization: model.big_screenshot,
},
// ranking: 10 * (index + 1) - 0.5, //(model._rankingInfo - 1*index)// hack
};
const q = prepareQuestion(fq, guesstimate);
return q;
};
async function search(query: string): Promise<ElasticQuestion[]> {
const response = await axios({
url: searchEndpoint,
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
data: `{\"params\":\"query=${query.replace(
/ /g,
"%20"
)}&hitsPerPage=20&page=0&getRankingInfo=true\"}`,
method: "POST",
});
const models: any[] = response.data.hits;
const mappedModels: ElasticQuestion[] = models.map((model) => {
const q = modelToQuestion(model);
return questionToElasticDocument({
...q,
fetched: new Date(),
firstSeen: new Date(),
});
});
// filter for duplicates. Surprisingly common.
let uniqueTitles: string[] = [];
let uniqueModels: ElasticQuestion[] = [];
for (let model of mappedModels) {
if (!uniqueTitles.includes(model.title) && !model.title.includes("copy")) {
uniqueModels.push(model);
uniqueTitles.push(model.title);
}
}
return uniqueModels;
}
const fetchQuestion = async (id: number): Promise<Question> => {
const response = await axios({ url: `${apiEndpoint}/spaces/${id}` });
const q = modelToQuestion(response.data);
return await upsertSingleQuestion(q);
};
export const guesstimate: Platform & {
search: typeof search;
fetchQuestion: typeof fetchQuestion;
} = {
name: "guesstimate",
label: "Guesstimate",
color: "#223900",
search,
version: "v1",
fetchQuestion,
calculateStars: (q) => (q.description?.length > 250 ? 2 : 1),
};