-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrepstein.sh
More file actions
executable file
·200 lines (161 loc) · 4.04 KB
/
Copy pathgrepstein.sh
File metadata and controls
executable file
·200 lines (161 loc) · 4.04 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bash
if [[ $1 == "--fzf" ]]; then
shift
FZF=true
else
FZF=false
fi
QUERY="$*"
PAGE=1
DEPS=(pdftotext jq httpie)
TMP_PDF="/tmp/epstein_file.pdf"
HEADERS=(
'User-Agent:Mozilla/5.0 (X11; Linux x86_64)'
'Cookie:justiceGovAgeVerified=true'
)
RED=$'\e[31m'
YEL=$'\e[33m'
GRN=$'\e[32m'
DEF=$'\e[0m'
cleanup() {
for file in "$TMP_PDF" "$TMP_TXT"
do
[[ -f "$file" ]] && rm -f "$file"
done
}
trap 'cleanup' EXIT
if [[ -z $QUERY ]]; then
echo -e "$RED Please provide a valid search term. $DEF"
exit 1
fi
dependency_check () {
echo -e "$YEL \nDependency control... $DEF"
local dep_status=0
for i in "${DEPS[@]}"; do
echo -n "Checking $i -> "
if command -v "$i" &>/dev/null; then echo "passed"; ((dep_status++)); else echo "failed"; fi
done
if [[ "$dep_status" -ne "${#DEPS[@]}" ]]; then
echo -e "$RED\nThere are missing packages. Please install them to use grepstein.sh $DEF"
exit 1
fi
}
fetch_results () {
URL="https://www.justice.gov/multimedia-search?keys=$QUERY&page=$PAGE"
DATA=$(http GET "$URL" "${HEADERS[@]}" | jq)
TOTAL_RECORDS=$(jq '.hits.total.value' <<< "$DATA")
mapfile -t PDF_URLS < <(jq -r '.hits.hits[]._source.ORIGIN_FILE_URI' <<< "$DATA" | sed 's/ /%20/g')
PDF_COUNT=${#PDF_URLS[@]}
[[ $1 == "--quiet" ]] && return 0
echo -e "$GRN \nWe found $TOTAL_RECORDS result(s) for ${QUERY} on all pages. Page $PAGE only shows $PDF_COUNT result(s) $DEF"
if [[ "$PDF_COUNT" -gt 0 ]]; then
for i in "${!PDF_URLS[@]}"; do
echo "$i - ${PDF_URLS[i]}"
done
fi
}
ask_usrcmd () {
if [[ "$PDF_COUNT" -gt 0 ]]; then
while true; do
echo -e "$GRN \nPlease select your action $DEF"
echo "-> Type OPEN to open a file"
echo "-> Type NEXT to continue to next page"
echo "-> Type EXIT to exit"
echo -n "Your command : "
read -r USRCMD
USRCMD="${USRCMD,,}"
case "$USRCMD" in
open)
echo -ne "$GRN \nPlease write the index number of the file that you want to open : $DEF"
read -r INDEX
if [[ $INDEX -ge 0 && $INDEX -lt $PDF_COUNT ]]; then
http "${PDF_URLS[INDEX]}" "${HEADERS[@]}" > "$TMP_PDF"
pdftotext -layout "$TMP_PDF" - | less
rm "$TMP_PDF"
fetch_results
else
echo -e "$RED \nPlease Provide a valid index number $DEF"
fi
;;
next)
echo -e "$YEL \nProceding to next page... $DEF"
((PAGE++))
fetch_results
;;
exit)
echo -e "$YEL \nExiting... $DEF"
break
;;
*)
echo -e "$RED \nInvalid command, please enter a valid command. $DEF"
;;
esac
done
fi
}
dependency_check
if ! $FZF
then
fetch_results
ask_usrcmd
exit 0
fi
# fzf option
if ! command -v fzf &>/dev/null; then echo "fzf is not installed" 2>&1; exit 1; fi
TMP_TXT="/tmp/epstein_file.txt"
DOJ_URL="https://www.justice.gov/epstein/files/"
export DOJ_URL QUERY TMP_PDF TMP_TXT RED DEF
get_file() {
local URL="$1"
local -a HEADERS
HEADERS=(
'User-Agent:Mozilla/5.0 (X11; Linux x86_64)'
'Cookie:justiceGovAgeVerified=true'
)
http GET "$URL" "${HEADERS[@]}" > "$TMP_PDF"
pdftotext -layout "$TMP_PDF" > "$TMP_TXT"
sed -i "s/${QUERY}/${RED}&${DEF}/gI" "$TMP_TXT"
}
preview() {
local file
file="${DOJ_URL}${1}"
file="${file// /%20}"
get_file "$file"
cat "$TMP_TXT"
}
get_all_urls() {
local page old_page
while true
do
fetch_results --quiet
page=$(
for URL in "${PDF_URLS[@]}"
do
URL="${URL#"$DOJ_URL"}"
URL="${URL//%20/ }"
echo "$URL"
done
)
[[ $page == "$old_page" || -z "$page" ]] && break
old_page="$page"
echo "$page"
((PAGE++))
done
}
export -f fetch_results get_file preview
header+="┌─┐┬─┐┌─┐┌─┐┌─┐┌┬┐┌─┐X┌┐┌"$'\n'
header+="│ ┬├┬┘├┤ ├─┘└─┐ │ ├┤ ││││"$'\n'
header+="└─┘┴└─└─┘┴ └─┘ ┴ └─┘┘┘└┘"
fzf_opts=(
--reverse
--header-first
--header "$header"
--color "header:red:bold"
--preview 'bash -c "preview {}"'
)
file=$(get_all_urls | fzf "${fzf_opts[@]}")
[[ -n "$file" ]] || exit 0
file="${DOJ_URL}${file}"
file="${file// /%20}"
get_file "$file"
less -R "$TMP_TXT"