-
Notifications
You must be signed in to change notification settings - Fork 19
/
hypothesis.mjs
72 lines (68 loc) · 2.15 KB
/
hypothesis.mjs
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
import queryString from "query-string";
import fetch from "isomorphic-fetch";
import lodash from "lodash";
import querystring from "querystring";
import { formatDate } from "./helpers.mjs";
import { config } from "./config.mjs";
const parseAnnotation = (a) => {
const textRaw = a.text;
const quotationRaw =
lodash.get(a, "target[0]selector") &&
a.target[0].selector.find((x) => x.exact) &&
a.target[0].selector.find((x) => x.exact).exact;
const quotation = (quotationRaw || "").replace(/\n/g, " ");
const text = (textRaw || "").replace(/\n/g, " ");
const extraIndent = text ? " " : "";
const quoteString = quotation ? ` - ${quotation}` : "";
const textString = text ? extraIndent + ` - ^^${text}^^` : "";
return [quoteString, textString].join("\n");
};
const getAnnotations = async (token, annotatedUrl, user) => {
const pdfUrl = annotatedUrl.match(/viewer.html\?file=(.+)$/);
if (pdfUrl) {
annotatedUrl = Object.keys(querystring.decode(pdfUrl[1]))[0];
}
const query = queryString.stringify({
limit: 200,
url: annotatedUrl,
user,
});
const url = "https://hypothes.is/api/search?" + query;
const queryHeaders = token && {
headers: {
Authorization: "Bearer " + token,
},
};
try {
await fetch(url, queryHeaders)
.then((e) => e.json())
.then((e) => {
const article = lodash.get(e, "rows[0].document.title[0]");
const updated = lodash.get(e, "rows[0].updated");
const annotations = lodash
.orderBy(e.rows, (f) => {
try {
return lodash
.get(f, "target[0].selector")
.filter((x) => x.type === "TextPositionSelector")[0].start;
} catch (e) {
return 0;
}
})
.map((x) => parseAnnotation(x))
.join("\n");
const dateStr = formatDate(updated);
console.log(
`- ${article} #article
- Source:: ${annotatedUrl}\n
- Updated:: ${dateStr}\n
- Annotations
${annotations}`
);
});
} catch (e) {
console.error(e);
}
};
console.log("%%tana%%");
getAnnotations(config.hypothesisToken, process.argv[2].trim());