-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypothesis-annotations-scraper.sh
More file actions
executable file
·116 lines (84 loc) · 3.14 KB
/
hypothesis-annotations-scraper.sh
File metadata and controls
executable file
·116 lines (84 loc) · 3.14 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
# download all annotations for a user from https://hypothes.is/
set -e
#set -x
# https://github.com/hypothesis/h
# https://h.readthedocs.io/en/latest/api-reference/v1/#tag/annotations/paths/~1search/get
# https://jonudell.info/h/facet/
# https://github.com/hypothesis/product-backlog/issues/566
debug=false
# debug=true
user="$1"
if [ -z "$user" ]; then
echo "usage: $0 some_username"
exit 1
fi
sort_field=updated
# sort_field=created
# https://r-world-devs.github.io/hypothesis/reference/search_annotations.html
base_url="https://hypothes.is/api/search?sort=$sort_field&limit=200&user=$user"
num_done=0
num_total=
last_date=
search_after=
date_format="%Y%m%dT%H%M%SZ"
date=$(date --utc +"$date_format")
result_json_list=()
# https://h.readthedocs.io/en/latest/api/authorization/
api_token=""
api_token_file="$HOME/.config/hypothesis-annotations/api-token.txt"
if [ -e "$api_token_file" ]; then
echo "using api token from $api_token_file"
api_token="$(< "$api_token_file")"
else
echo "not using api token from $api_token_file"
echo " note: without an api token, we can scrape only public data = only annotations, but no highlights"
echo " you can get an api token at https://hypothes.is/account/developer"
fi
while true; do
step_time=$(date +%s.%N)
url="$base_url"
# https://github.com/hypothesis/h/issues/5191#issuecomment-419400695
# https://web.hypothes.is/blog/new-search-api-parameter-search_after/
# search_after is based on sort and order.
# sort defaults to the updated field, or the last time the annotation was updated,
# and order defaults to descending (so the most recently updated annotations will be found and returned first).
# search_after will return annotations that occur after the annotation whose sort field has the search_after’s value.
if [ -n "$search_after" ]; then
url+="&search_after=$search_after"
fi
result_json=$(curl -s -H "Authorization: Bearer $api_token" "$url")
if $debug; then
echo "$url" >debug.$step_time.url.txt
echo "$result_json" | jq >debug.$step_time.result_json.json
fi
result_json_list+=("$result_json")
if [ -z "$num_total" ]; then
num_total=$(jq -r '.total' <<<"$result_json")
fi
if [ -z "$last_date" ]; then
last_date=$(jq -r '.rows[0].updated' <<<"$result_json")
#echo "last_date: $last_date"
date=$(date --utc +"$date_format" -d "$last_date")
out="$user.$date.json"
if [ -e "$out" ]; then
echo "keeping $out"
exit
fi
fi
num_rows=$(jq -r '.rows | length' <<<"$result_json")
#echo "num_rows: $num_rows"
num_done=$((num_done + num_rows))
echo "done $num_done of $num_total"
((num_done >= num_total)) && break
search_after=$(jq -r ".rows[-1].$sort_field" <<<"$result_json")
[[ "$search_after" == "null" ]] && break
done
# pass multiple strings via process substitution
# https://stackoverflow.com/questions/51040310/process-substitution-for-each-array-entry-without-eval
echo "writing $out"
jq_script='map(.rows) | add as $rows | { total: $rows | length, rows: $rows }'
eval \
jq -s -c '"$jq_script"' \
$(printf '<(echo -n "${result_json_list[%s]}") ' "${!result_json_list[@]}") \
>"$out"