-
Notifications
You must be signed in to change notification settings - Fork 3
/
rugmi
executable file
·496 lines (406 loc) · 14.5 KB
/
rugmi
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/bash
# Copyright (C) 2019 Rany Albeg Wein - [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
shopt -s extglob
# Tokens map, access_token and refresh_token are read from tokens_file.
declare -A codes_map
# Base imgur api URL
base_api_url=https://api.imgur.com
# Base OAuth2 url.
base_oauth2_api_url="$base_api_url/oauth2"
# Register application url.
register_app_url="$base_oauth2_api_url/addclient"
# PIN Request URL template.
pin_request_url_template="$base_oauth2_api_url/authorize?client_id=%s&response_type=pin"
# Token request URL.
token_request_url="$base_oauth2_api_url/token"
# Upload URL.
url_upload="$base_api_url/3/upload"
# Delete request URL template.
declare -x url_delete_request_template="$base_api_url/3/image/%s"
# Contains access_token and refresh_token.
tokens_file="$HOME/.rugmi_tokens"
# Files to upload.
files=()
# Number of images already uploaded.
uploaded=0
# Spawn deletion.
spawn_del=0
# Number of seconds to wait before deletion.
spawn_del_sec=0
# Read files from stdin. 1=read,0=don't.
read_stdin=0
# Options string.
optstring=d:ht:s:f:0
# Options array.
options=()
# Link type, should be one of:
# Image, Direct ( Default ), Markdown, HTML, BBCode, LBBCode (Linked BBCode).
link_type=Direct
# Display size, should one of:
# O (Original), SS (Small square), BS (Big square)
# ST (Small thumbnail), MT (Medium thumbnail)
# LT (Large thumbnail), HT (Huge thumbnail)
display_size=O
# Display size codes.
declare -A display_codes=( ["O"]= ["SS"]=s ["BS"]=b ["ST"]=t ["MT"]=m ["LT"]=l ["HT"]=h )
usage() {
local bold=$(tput bold)
local reset=$(tput sgr0)
cat<<-EOF
${bold}NAME${reset}
rugmi - Upload images to imgur.com and print their links.
${bold}SYNOPSIS${reset}
rugmi [-dts0h] -f FILE
${bold}DESCRIPTION${reset}
Upload images to imgur.com and print their links.
On first execution, rugmi will ask you to authorize by providing Client ID, Client Secret and a PIN code.
Once Client ID and Client Secret are provided, if available, xdg-open will be used to open up a browser to log you in imgur.com and get the PIN code.
Otherwise, you'll be requested to manually copy-paste an authorization link into your browser.
After providing the PIN code, an access token and refresh token will be requested from imgur.com
and will be stored in ~/.rugmi_tokens (077 umask) along with Client ID and Client Secret.
After setup is complete, rugmi will automatically upload FILES to imgur.com.
Rugmi will ask imgur.com for new access token once expired.
${bold}OPTIONS${reset}
-f,--file FILE Select a FILE to upload.
-0,--null Read null terminated file names from stdin.
-d,--delete SECONDS Delete images SECONDS seconds after they've been uploaded.
-t,--link-type TYPE Choose what type of link to print after upload.
-s,--display-size SIZE Select the display size of the uploaded image.
-- FILES Ignore any options to come.
Everything after this option is considered a file.
-h,--help Show this message and exit successfully.
Link types:
${bold}*${reset} Direct (email & IM) - Default
${bold}*${reset} Image (email & IM)
The uploaded image as it is hosted in imgur.com, with all the fun stuff around.
This type of link will ignore any display size ( if provided via -s,--display-size )
${bold}*${reset} Markdown (reddit comments)
${bold}*${reset} HTML (website/blogs)
${bold}*${reset} BBCode (message boards & forums)
${bold}*${reset} LBBCode (Linked BBCode suitable for message boards)
Display size options:
${bold}*${reset} O (Original) - Default
${bold}*${reset} SS (Small square)
${bold}*${reset} BS (Big square)
${bold}*${reset} ST (Small thumbnail)
${bold}*${reset} MT (Medium thumbnail)
${bold}*${reset} LT (Large thumbnail)
${bold}*${reset} HT (Huge thumbnail)
${bold}EXAMPLES${reset}
Upload foo.png, bar.png, baz.png and print the result links to stdout.
${bold}$ rugmi -f foo.png -f bar.png -f baz.png${reset}
or
${bold}$ rugmi -- foo.png bar.png baz.png${reset}
Upload foo.png, print the result link to stdout
and delete the file from imgur.com servers after 15 seconds.
${bold}$ rugmi -d 15 -f foo.png${reset}
Upload all JPEG files in a directory and print the result links to stdout.
${bold}$ find dir/ -type f -name '*.jpg' -print0 | rugmi -0${reset}
Upload foo.png and print an HTML type of link to be displayed as a medium thumbnail.
${bold}$ rugmi -f foo.png -t HTML -s MT${reset}
Written by Rany Albeg Wein - [email protected]
21/5/2016
Edit 11/1/2019 - Auto Oauth2 setup.
Edit 17/1/2019 - Auto delete.
Edit 21/1/12019 - Dependencies check.
EOF
}
# Check for dependencies, abort if a dependency is missing.
# Args: dependencies.
deps_check(){
for prog; do
type -P "$prog" >/dev/null 2>&1 || die 10 "Missing dependency: $prog"
done
}
# Die.
# Args: Exit status (optional), failure message.
die() {
local r=$?
if [[ $1 != *[!0-9]* ]]; then
r=$1
shift
fi
printf '%s\n' "$@" >&2
exit $r
}
# Opens a given url in a browser, via xdg-open.
# Ignore all output by 'xdg-open' and 'type'.
open_url_in_browser(){
local url=$1
if type -P xdg-open; then
xdg-open "$url"
return 0;
fi
return 1;
} >/dev/null 2>&1
# OAuth2 and initial setup.
# All output goes to stderr.
setup(){
local pin
printf '%s\n' "Starting authorization."
if open_url_in_browser "$register_app_url"; then
printf 'Browser is opened at %s\n' "$register_app_url"
else
printf 'Register an application at %s\n' "$register_app_url"
fi
printf 'Get your Client ID and Client Secret and provide them here.\n'
read -rp "Client ID: " codes_map[client_id]
read -rp "Client Secret: " codes_map[client_secret]
[[ ${codes_map[client_id]} ]] || die "Aborting Setup. Empty Client ID."
[[ ${codes_map[client_secret]} ]] || die "Aborting Setup. Empty Client Secret."
printf -v pin_request_url "$pin_request_url_template" "${codes_map[client_id]}"
printf 'To complete setup please:\n'
if ! open_url_in_browser "$pin_request_url"; then
printf 'Copy-paste the following authorization link to your browser: %s\n' "$pin_request_url"
fi
read -rp "Insert PIN code here: " pin
[[ $pin ]] || die 7 "Aborting setup. Empty pin."
if res=$(curl --silent -X POST -F "client_id="${codes_map[client_id]} \
-F "client_secret=${codes_map[client_secret]}" \
-F "pin=$pin" \
-F "grant_type=pin" "$token_request_url"); then
parse_tokens_to_codes_map <<< "$res"
if [[ ! ${codes_map[access_token]} || ! ${codes_map[refresh_token]} ]]; then
die 8 "Unable to fetch tokens. Probably an invalid PIN was entered."
fi
write_tokens_file
printf '%s %s\n' "$tokens_file" "created."
printf '%s\n' "You're now authorized!"
return 0
fi
return 1
} >&2
# Print a link based on selected type.
print_link() {
local link=${1%.*}
local ext=${1##*.}
case $link_type in
Image)
printf '%s\n' "$link";;
Direct)
printf '%s%s.%s\n' "$link" "${display_codes[$display_size]}" "$ext";;
Markdown)
printf '[Imgur](%s%s.%s)\n' "$link" "${display_codes[$display_size]}" "$ext";;
HTML)
printf '<a href="%s"><img src="%s%s.%s" title="source: imgur.com" /></a>\n' "$link" "$link" "${display_codes[$display_size]}" "$ext";;
BBCode)
printf '[img]%s%s.%s[/img]\n' "$link" "${display_codes[$display_size]}" "$ext";;
LBBCode)
printf '[url=%s][img]%s%s.%s[/img][/url]\n' "$link" "$link" "${display_codes[$display_size]}" "$ext";;
esac
}
# Ping imgur.
ping_imgur() {
ping -c1 -w2 imgur.com >/dev/null 2>&1
}
# Check if the requested link type is valid.
is_valid_link_type() {
[[ $1 = @(Image|Direct|Markdown|HTML|BBCode|LBBCode) ]]
}
# Check if the requested display size is valid.
is_valid_display_size() {
[[ $1 = @(O|SS|BS|ST|MT|LT|HT) ]]
}
# Upload images to imgur and print their links.
# Args: Images to upload.
upload() {
#grep -Eo 'https?:[^"]+')
local link
local dhash
local exit_code=0
for f; do
if res=$(curl --silent -X POST -H "Authorization: Bearer ${codes_map[access_token]}" \
-F "image=@$f" "$url_upload"); then
link=$(printf '%s' "$res" | jq --raw-output '.data.link')
dhash=$(printf '%s' "$res" | jq --raw-output '.data.deletehash')
print_link "$link"
delete_hashes+=("$dhash")
((++uploaded))
else
exit_code=1
break
fi
done
if (( spawn_del && ${#delete_hashes[@]})); then
printf 'All uploads will be automatically deleted after %d seconds.\n' "$spawn_del_sec" >&2
bash -c 'sleep $1 && delete_uploaded $2 "${@:2}"' _ "$spawn_del_sec" "${codes_map[client_id]}" "${delete_hashes[@]}" \
>/dev/null 2>&1 & disown
fi
return "$exit_code"
}
# Request imgur.com to delete several images.
# Args: All hashes of uploaded images.
delete_uploaded(){
local client_id=$1
shift
local hashes=("$@")
for h in "${hashes[@]}"; do
request_deletion "$client_id" "$h"
done
}
# Request imgur.com to delete a single image.
# Args: Access token and delete hash.
request_deletion(){
local client_id=$1
local dhash=$2
local delete_url
printf -v delete_url "$url_delete_request_template" "$dhash"
curl -X DELETE -H "Authorization: Client-ID $client_id" "$delete_url"
}
# Request and read new tokens from imgur.com.
request_tokens_refresh() {
if res=$(curl -s -X POST -H "Authorization: Bearer ${codes_map[access_token]}" \
-F "refresh_token=${codes_map[refresh_token]}" \
-F "client_id=${codes_map[client_id]}" \
-F "client_secret=${codes_map[client_secret]}" \
-F "grant_type=refresh_token" "$token_request_url"); then
parse_tokens_to_codes_map <<< "$res"
write_tokens_file
return 0
fi
return 1
}
# Parse imgur response and store:
# access_token.
# refresh_token.
parse_tokens_to_codes_map(){
while IFS=\" read -rd \, _ k _ v; do
if [[ "$k" = @(access_token|refresh_token) ]]; then
codes_map["$k"]="$v"
fi
done
}
# Read tokens file to codes_map.
read_tokens_file() {
while IFS=: read -r k v; do
codes_map["$k"]="$v"
done < "$tokens_file"
}
# Write codes_map to tokens_file
write_tokens_file() {
umask 077
printf '%s:%s\n' access_token "${codes_map[access_token]}" \
refresh_token "${codes_map[refresh_token]}" \
client_id "${codes_map[client_id]}" \
client_secret "${codes_map[client_secret]}" > "$tokens_file"
}
## EXECUTION STARTS HERE ##
deps_check curl jq
export -f delete_uploaded
export -f request_deletion
# Parse cmd line.
# The following loop was written by e36freak.
# Check out his great projects at: https://github.com/e36freak
while (($#)); do
case $1 in
# if option is of type -ab
-[!-]?*)
# loop over each character starting with the second
for ((i=1; i<${#1}; i++)); do
c=${1:i:1}
# add current char to options
options+=("-$c")
# if option takes a required argument, and it's not the last char
# make the rest of the string its argument
if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then
options+=("${1:i+1}")
break
fi
done
;;
# if option is of type --foo=bar, split on first '='
--?*=*) options+=("${1%%=*}" "${1#*=}");;
# end of options, stop breaking them up
--)
options+=(--endopts)
shift
options+=("$@")
break
;;
# otherwise, nothing special
*) options+=("$1");;
esac
shift
done
# set new positional parameters to altered options
set -- "${options[@]}"
# actually parse the options and do stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; exit 0;;
-0|--null) read_stdin=1;;
-d|--delete)
spawn_del=1
[[ $2 != *[!0-9]* ]] || die 9 "Invalid time: $2"
spawn_del_sec=$2
shift
;;
-t|--link-type)
is_valid_link_type "$2" || die 1 "Invalid link type: $2"
link_type="$2"
shift
;;
-s|--display-size)
is_valid_display_size "$2" || die 2 "Inavlid display size: $2"
display_size="$2"
shift
;;
-f|--file)
if [[ $2 ]] ; then
[[ -f $2 ]] || die "$2 : No such file."
files+=("$2")
shift
else
die "$1 missing an argument."
fi
;;
--endopts)
shift
files+=("$@")
for f in "${files[@]}"; do
[[ -f $f ]] || die "$f : No such file."
done
break;;
*) die "Unknown option $1"
esac
shift
done
if (( read_stdin )); then
# Read null terminated file names from stdin.
while read -rd '' f; do
files+=("$f")
done
fi
if (( ${#files[@]} )); then
# Tokens file existance validation.
[[ -f "$tokens_file" ]] || setup
read_tokens_file
# imgur.com is reachable.
if ping_imgur; then
# Upload files to imgur.com
if ! upload "${files[@]}"; then
request_tokens_refresh || die 3 "Request for new tokens failed."
# Try again with remaining files.
upload "${files[@]:uploaded}" || die 4 "Unable to upload images to imgur.com"
fi
else
die 5 "Unable to reach imgur.com"
fi
else
die 6 "Nothing to do."
fi