-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnks
executable file
·355 lines (290 loc) · 8.31 KB
/
lnks
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/bash
# To Do
# - add option to read urls from a file and run a single command for all
# - eg. lnks -r urls.txt --instapaper
# - improve code quality and style consistency
# - to inlcude renaming functions and changing the case statment to use 'getopts'
# - extract pastebin functions into a standalone file as a 'plugin'
# - remove all commented code
#
lnks() {
OIFS="$IFS"
IFS=$'\n\t'
local option; option="$1"
local srch; srch="$2"
local lfile; lfile="$3"
# https://www.w3.org/Tools/HTML-XML-utils/README
local hxclean; hxclean="$(command -v hxclean)"
local hxselect; hxselect="$(command -v hxselect)"
_lnx_help() {
echo "lnks <option> <search term>"
echo
echo "Options:"
echo " -s|--save save the links to a file on the desktop"
echo " -c|--copy copy the links to your clipboard"
echo " -p|--print print the links to stdout"
echo " -m|--markdown print links in mardown format: [title](url)"
echo " -i|--instapaper save the link(s) to instapaper"
echo " -b|--pinboard save the link(s) to pinboard.in (requires 'html-xml-utils')"
echo " -w|--pdf save each url as a pdf (requires 'wkhtmltopdf')"
echo " -h|--help print this help message"
echo
echo "Note"
echo " - one (and only one) option is permitted. lnks will fail if multiple options are specified."
echo " - lnks will allow multiple options in a future version"
}
_htmlxmlutil_warning() {
echo "Error: hxclean or hxselect is not installed, or they are not in your \$PATH"
echo "Please visit https://www.w3.org/Tools/HTML-XML-utils/README for installation information"
echo "or install the 'html-xml-utils' package via homebrew (https://brew.sh)"
}
_prog() {
[[ -z "$1" ]] && exit 0
set +m
tput civis
eval "$1" >/dev/null 2>&1 &
local pid; pid=$!
local spin; spin="-\|/"
local i; i=0
while kill -0 "$pid" 2>/dev/null; do
i=$(((i + 1) % 4))
printf "\r%s" ${spin:$i:1}
sleep .07
done
printf "\r"
tput cnorm
set -m
}
links() {
_pull() {
osascript <<EOT
tell application "Google Chrome"
set links to get URL of tabs of first window
return links
end tell
EOT
}
local count; count="$(_pull | grep -i "$srch" | sed "s|^ ||g" | wc -l)"
local links; links="$(_pull | tr ',' '\n' | grep -i "$srch" | sed "s|^ ||g")"
if [[ "$count" -eq 0 ]]; then
echo "Error: No matching links"
else
echo "$links"
fi
unset count
unset links
}
_save() {
if [[ -z "$lfile" ]]; then
echo "No filename entered."
_lnx_help
fi
links > "$lfile" && echo "Links matching $srch saved to $lfile"
unset -f _save
}
_copy() {
copylinks() {
links | pbcopy
echo "Links matching $srch copied to clipboard"
}
local lnx
lnx="$(links)"
if [[ "$lnx" == "Error: No matching links" ]]; then
links
else
copylinks
fi
unset -f _copy
}
_markdown() {
if [[ -z "$hxclean" ]] || [[ -z "$hxselect" ]]; then
_htmlxmlutil_warning
else
links | while read -r lnk; do
local title; title="$(curl "${lnk}" -so - | "$hxclean" | "$hxselect" -s '\n' -c 'title')"
echo -en "[${title}](${lnk})\n"
done
fi
unset -f _markdown
}
_instapaper() {
_instapaper_get_credentials() {
instapaper_username="$(
grep -i 'instapaper_username' ~/.lnks.conf | awk -F= '{print $2}' | sed 's|\"||g'
)"
instapaper_password="$(
grep -i 'instapaper_password' ~/.lnks.conf | awk -F= '{print $2}' | sed 's|\"||g'
)"
}
_instapaper_store_credentials() {
echo "lnks needs to store your Instapaper credentials"
sleep 1
tput cnorm
read -r -p "enter your username (email address): " instapaper_username
read -rs -p " enter your password: " instapaper_password
sleep .2
{
echo "instapaper_username=\"$instapaper_username\"";
echo "instapaper_password=\"$instapaper_password\"";
echo;
} >> ~/.lnks.conf
echo "done! your credentials are stored at $HOME/.lnks.conf"
echo "delete this file at any time to revoke Instapaper access."
}
if grep -q 'instapaper' ~/.lnks.conf; then
_instapaper_get_credentials
else
_instapaper_store_credentials
fi
}
_instapaper_add() {
local lnx
lnx="$(links)"
if [[ "$lnx" == "Error: No matching links" ]]; then
links
else
links | while read -r url; do
local base="https://www.instapaper.com/api/add"
curl -d --silent \
--data-urlencode "username=${instapaper_username}" \
--data-urlencode "password=${instapaper_password}" \
--data-urlencode "url=${url}" \
"${base}" >/dev/null 2>&1
echo "$url Saved!"
sleep .2
done
fi
unset instapaper_username
unset instapaper_password
unset -f _instapaper
unset -f _instapaper_add
}
_pinboard() {
_pinboard_get_credentials() {
pinboard_api="$(grep -i 'pinboard_api' ~/.lnks.conf | awk -F= '{print $2}' | sed 's|\"||g')"
}
_pinboard_store_credentials() {
echo "lnks needs to store your Pinboard API Token."
echo -en "You can find your API Token by visiting your pinboard settings:\n"
echo -en "https://pinboard.in/settings/password. \nYour Token will appear as"
echo "'userName:tokenString.' For example 'lnksuser:1F4E2SD32418AF103FDD'"
echo
sleep 1
tput cnorm
read -r -p " enter your API Key: " pinboard_api
echo
echo "done! your credentials are stored at $HOME/.lnks.conf"
echo "delete this file at any time to revoke Pinboard access."
sleep .2
echo -en "pinboard_api=\"$pinboard_api\"" >> ~/.lnks.conf
}
if grep -q 'pinboard_api' ~/.lnks.conf; then
_pinboard_get_credentials
else
_pinboard_store_credentials
fi
}
_pinboard_add() {
_get_info_and_post() {
local url; url="$1"
if [[ -z "$hxclean" ]] || [[ -z "$hxselect" ]]; then
_htmlxmlutil_warning
else
local title
title="$(curl -so - "$url" | "$hxclean" | "$hxselect" -s '\n' -c 'title')"
title="${title:-$url}"
echo
echo "TITLE: $title"
echo "URL: $url"
echo -en "========\n"
echo
echo "Enter tags for this bookmark: "
tput cnorm
read -r tags
sleep 1
local base="https://api.pinboard.in/v1/posts/add"
curl -G --silent \
--data-urlencode "auth_token=${pinboard_api}" \
--data-urlencode "url=${url}" \
--data-urlencode "description=${title}" \
--data-urlencode "tags=${tags}" \
"${base}" >/dev/null 2>&1
echo "saved to pinboard!"
fi
}
local lnx; lnx="$(links | tr ' ' '\n')"
if [[ "$lnx" == "Error: No matching links" ]]; then
links
else
while read -r url; do
_get_info_and_post "$url" </dev/tty
sleep .2
done < <(echo "$lnx")
fi
unset pinboard_api
unset -f _pinboard
unset -f _pinboard_add
}
_wkhtmltopdf() {
_to_pdf() {
"$(command -v wkhtmltopdf)" --quiet --title "$url" "$url" "$title".pdf
sleep .2
}
# test for the existense of wkhtmltopdf
if [[ ! "$(command -v wkhtmltopdf)" ]]; then
echo "Error: wkhtmltopdf is not installed, or it is not in your \$PATH"
echo "Please visit http://wkhtmltopdf.org/downloads.html for installation instructions"
else
local lnx; lnx="$(links | tr ' ' '\n')"
while read -r url; do
echo "Converting \"${url}\" to PDF..."
_prog _to_pdf
local title
title="$(
readonly hxclean="$(command -v hxclean)";
readonly hxselect="$(command -v hxselect)";
curl -so - "$url" | $hxclean | "$hxselect" -s '\n' -c 'title'
)"
echo "Done - $(pwd)/${title}.pdf"
done < <(echo "$lnx")
fi
unset -f _wkhtmltopdf
}
if [[ "$option" == "-h" ]]; then
:
elif [[ ! "$option" ]]; then
:
elif [[ "$srch" == "" ]]; then
echo "Error: No search term entered"
option="--help"
fi
case "$option" in
# help
--help | -h) _lnx_help ;;
"") _lnx_help ;;
# option to save the list as a file
--save | -s) _save ;;
# copy to clipboard
--copy | -c) _copy ;;
# print
--print | -p) links ;;
# print markdown formatted links
--markdown | -m) _markdown ;;
# add to instapaper
--instapaper | -i) _instapaper && _instapaper_add ;;
# save to pinboard
--pinboard | -b) _pinboard && _pinboard_add ;;
# save url (as webpage) to pdf
--pdf | -w) _wkhtmltopdf ;;
*) _lnx_help ;;
esac
# take only memories, leave no footprints
{
unset url
unset -f links; unset -f _lnx_help; unset -f _s
unset -f _c; unset -f _instapaper; unset -f _instapaper_add
unset -f _pinboard; unset -f _pinboard_add; unset -f _w
IFS="$OIFS"
unset -f erase
} >/dev/null 2>&1
}