forked from jschx/uguush
/
uguush
executable file
·449 lines (400 loc) · 11.8 KB
/
uguush
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
#!/usr/bin/env bash
#
# uguush - command-line uploader for uguu.se and others
#
## CONFIGURATION
# colors
if [ -x "$(command -v tput)" ]; then
c0="$(tput sgr0)"
c1="$(tput setaf 1)"
c2="$(tput setaf 2)"
fi
# filename to use for screenshots
localTemp="$(mktemp)"
# screenshot functions
capFullscreen() {
localFile="${localTemp}.png"
mv "${localTemp}" "${localFile}"
$screenshotFullscreen "${localFile}"
}
capSelection() {
localFile="${localTemp}.png"
mv "${localTemp}" "${localFile}"
$screenshotSelection "${localFile}"
}
capWindow() {
localFile="${localTemp}.png"
mv "${localTemp}" "${localFile}"
$screenshotWindow "${localFile}"
}
# delay
delaySeconds='0'
# hosts and shorteners
host='uguu'
shortener=
hosts='uguu teknik 0x0 fiery'
shorteners='waaai 0x0'
## FUNCTIONS
depends() {
if [ ! -x "$(command -v curl)" ]; then
echo "Checking for curl... [${c1}FAILED${c0}]" >&2
echo 'curl not found.' >&2
exit 1
fi
}
usage() {
cat << EOF
uguush - upload to various file hosts
Usage:
$(basename "${0}") [options]
Options:
-d Delay the screenshot by the specified number of seconds.
-f Take a fullscreen screenshot.
-h Show this help message.
-o <host> Select which host to use (uguu, teknik, 0x0 or fiery).
-p <path> Custom path to save the image to (saves as "%Y-%m-%d %H-%M-%S.png").
-c Copy image instead of URL to clipboard.
-n Enable save notification (if used alongside -p option).
-s Take a selection screenshot.
-u <file> Upload a file.
-x Do not notify dbus, update the log, or modify the clipboard.
-X Skip upload.
-w Take a screenshot of the current window.
-S <shortener> Select a shortener to use (waaai or 0x0).
-l <url> Upload the file at the provided URL.
-g Use gnome-screenshot (with -p saves as "Screenshot from %Y-%m-%d %H-%M-%S.png").
-k Use KDE/Spectacle (with -p saves as "Screenshot_%Y%m%d_%H%M%S.png").
-t <token> Set token (for fiery host).
-a <id> Set numerical ID of an album (for fiery host).
-D <domain> (DEPRECATED) Set safe.fiery.me file domain.
-F <domain> Set fiery host domain.
EOF
}
# id for notification
if [ "$(command -v notify-send)" ]; then
notifyId="uguush-$(date +%s)"
fi
notify() {
if [ "${notifyId}" ]; then
notify-send \
-a "uguush" \
-h "string:x-canonical-private-synchronous:${notifyId}" \
-h "string:private-synchronous:${notifyId}" \
-i "${2}" \
"uguu~" "${1}"
fi
}
delay() {
i="$delaySeconds"
while [ "$i" -gt 0 ]; do
echo "${i}..."
sleep 1
i="$((i-1))"
done
}
screenshot() {
if [ "${useGnomeScreenshot}" ] && [ -x "$(command -v gnome-screenshot)" ]; then
screenshotFullscreen="gnome-screenshot -f"
screenshotSelection="gnome-screenshot -af"
screenshotWindow="gnome-screenshot -we shadow -f"
elif [ "${useSpectacle}" ] && [ -x "$(command -v spectacle)" ]; then
screenshotFullscreen="spectacle -bno"
screenshotSelection="spectacle -rbno"
screenshotWindow="spectacle -abno"
elif [ -x "$(command -v maim)" ]; then
screenshotFullscreen="maim -u"
screenshotSelection="maim -us"
screenshotWindow="maim -ui $(xdotool getactivewindow)"
elif [ -x "$(command -v scrot)" ]; then
screenshotFullscreen="scrot"
screenshotSelection="scrot -s"
screenshotWindow="scrot -u"
elif [ "${doFullscreen}" ] || [ "${doSelection}" ] || [ "${doWindow}" ]; then
echo "Checking for a screenshot utility... [${c1}FAILED${c0}]" >&2
echo "Could not find a screenshot utility, please install 'maim' or 'scrot'" >&2
if [ -f "${localTemp}" ]; then rm "${localTemp}"; fi
exit 1
fi
if [ "${doFullscreen}" ]; then
capFullscreen
elif [ "${doSelection}" ]; then
capSelection > /dev/null 2>&1
if [ ! "${useSpectacle}" ] && [ ! -s "${localFile}" ]; then
$(rm "${localFile}" 2> /dev/null)
exit
fi
elif [ "${doWindow}" ]; then
capWindow
elif [ "${doURL}" ]; then
if [ -f "/usr/share/mime/globs" ]; then
urlExtension="$(curl -sf --head "${remoteURL}" | grep -i 'Content-Type: ' | head -1 | grep -Po '(?<=\ )[^\;]*')"
urlExtension="$(echo "${urlExtension}" | sed -e "s/\\r//")"
urlExtension="$(cat /usr/share/mime/globs | grep "${urlExtension}" | sort -r | head -1 | grep -Po '(?<=\.)[^\n]*')"
else
urlExtension="$(basename ${remoteURL})"
urlExtension=${urlExtension#*.}
fi
localFile="${localTemp}.${urlExtension}"
mv "${localTemp}" "${localFile}"
$(curl -sf "${remoteURL}" > "${localFile}")
fi
# patch for Spectacle not deleting zero bytes temp file in time when cancelling screenshot
fileSize=$(stat --printf="%s" "${localFile}")
if [ "${fileSize}" -eq 0 ]; then
$(rm "${localFile}" 2> /dev/null)
exit
fi
}
upload() {
i="1"
while [ "$i" -le 3 ]; do
printf "Try #${i}... "
# only show attempt notify for #2 attempt and onwards
if [ ! "${noComms}" ] && [ "$i" -ge 2 ]; then
notify "Uploading attempt #${i}..." "cloud-upload"
fi
i="$((i+1))"
case "${host}" in
uguu) hostURL='https://uguu.se/api.php?d=upload-tool' ;;
teknik) hostURL='https://api.teknik.io/v1/Upload' ;;
0x0) hostURL='https://0x0.st/' ;;
fiery)
if [ "${fieryHostDomain}" ]; then
hostURL="${fieryHostDomain}/api/upload"
else
hostURL='https://safe.fiery.me/api/upload'
fi
;;
esac
case "${shortener}" in
waaai) shortenerURL='https://api.waa.ai/shorten' ;;
0x0) shortenerURL='http://0x0.st/' ;;
esac
if [ "${host}" = 'uguu' ]; then
uploadResult="$(curl -sf -F file="@${localFile}" "${hostURL}")"
elif [ "${host}" = 'teknik' ]; then
uploadResult="$(curl -sf -F file="@${localFile}" "${hostURL}")"
uploadResult="${uploadResult##*url\":\"}"
uploadResult="${uploadResult%%\"*}"
elif [ "${host}" = '0x0' ]; then
uploadResult="$(curl -sf -F file="@${localFile}" "${hostURL}")"
elif [ "${host}" = 'fiery' ]; then
uploadResult="$(curl -H "token: ${fieryToken}" -H "albumid: ${fieryAlbumId}" -sf -F files[]="@${localFile}" "${hostURL}")"
uploadResult="$(echo "${uploadResult}" | grep -Po '"url":"[A-Za-z0-9]+.*?"' | sed 's/"url":"//;s/"//')"
if [ ! "${fieryHostDomain}" ] && [ "${fieryMeDomain}" ]; then
uploadResult="$(echo "${uploadResult}" | sed "s/https:\/\/i.fiery.me\//${fieryMeDomain}\//")"
fi
fi
if [ "${shortener}" = 'waaai' ]; then
tempResult="$(curl -sf -F url="${uploadResult}" "${shortenerURL}")"
shortCode="${tempResult##*short_code\":\"}"
shortCode="${shortCode%%\"*}"
shortenerResult="https://waa.ai/${shortCode}"
shortenerExtension="${tempResult##*extension\":\"}"
shortenerExtension="${shortenerExtension%%\"*}"
echo "${shortenerExtension}"
if [ "${shortenerExtension}" ]; then
shortenerExtension=${shortenerExtension##\"}
shortenerExtension=${shortenerExtension%%\"}
shortenerResult="${shortenerResult}.${shortenerExtension}"
fi
elif [ "${shortener}" = '0x0' ]; then
shortenerResult="$(curl -sf -F shorten="${uploadResult}" "${shortenerURL}")"
fi
if [ "${shortener}" ]; then
finalResult="${shortenerResult}"
else
finalResult="${uploadResult}"
fi
if [ "${finalResult}" ]; then
# copy to clipboard, log, and notify (unless $noComms is set)
if [ ! "${noComms}" ]; then
if [ "${copyImage}" ]; then
notify "Link to screenshot:\n${finalResult}" "link"
else
echo -n "${finalResult}" | xclip -selection primary
echo -n "${finalResult}" | xclip -selection clipboard
notify "Link copied to clipboard:\n${finalResult}" "url-copy"
fi
fi
echo "$(date +"%D %R") | "${localFile}" | "${finalResult}"" >> ~/.uguush.log
echo "[${c2}OK${c0}]"
echo "File has been uploaded: ${finalResult}"
# if we took a screenshot, remove the temporary file
if [ ! "${doFile}" ]; then
rm "${localFile}"
fi
exit
else
continue
fi
done
}
copyimage() {
if [ ! "${noComms}" ] && [ "${copyImage}" ]; then
mimeType=$(file -b --mime-type "$localFile")
xclip -selection primary -t "$mimeType" < "$localFile"
xclip -selection clipboard -t "$mimeType" < "$localFile"
notify "Screenshot copied to clipboard" "edit-copy"
fi
}
path() {
if [ "${saveToPath}" ]; then
if [ "${useGnomeScreenshot}" ]; then
# this is gnome-screenshot's default naming format
localFilename="$(date '+Screenshot from %Y-%m-%d %H-%M-%S')";
elif [ "${useSpectacle}" ]; then
# this is Spectacle's default naming format
localFilename="$(date '+Screenshot_%Y%m%d_%H%M%S')";
else
localFilename="$(date '+%Y-%m-%d %H-%M-%S')";
fi
cp ${localFile} "${pathToSave}/${localFilename}.png"
if [ $? -eq 0 ] && [ "${notifySave}" ]; then
notify "A screenshot was saved as '${localFilename}.png' to '${pathToSave}'" "document-save"
fi
fi
}
## PARSE OPTIONS
httpProtocolRegex='^https?:\/\/'
while getopts :a:cd:fhigkl:no:p:st:u:wxD:F:S:X opt ;do
case "${opt}" in
a)
# set fiery album id
fieryAlbumId="${OPTARG}" ;;
c)
# copy image to clipboard
copyImage=true ;;
d)
# set delay value
delaySeconds="${OPTARG}"
case "${delaySeconds}" in
*[!0-9]*)
echo "Checking if delay is a number... [${c1}FAILED${c0}]" >&2
echo 'delay is not a number.' >&2
exit 1 ;;
esac ;;
f)
# take fullscreen shot
doFullscreen=true ;;
h)
# print help
usage
exit 0 ;;
g)
# use gnome-screenshot
useGnomeScreenshot=true ;;
k)
# use Spectacle of KDE Plasma
useSpectacle=true ;;
l)
# set url to upload
doURL=true
remoteURL="${OPTARG}" ;;
n)
# enable save notification
notifySave=true ;;
o)
# set host
for available in ${hosts}; do
if [ "${OPTARG}" = "${available}" ]; then
host="${OPTARG}"
hostcheck=true
break
fi
done
[ "${hostcheck}" ] || exit 1 ;;
p)
# set path to save file
saveToPath=true
pathToSave="${OPTARG}"
if [ ! -d "${pathToSave}" ]; then
echo "Path ${pathToSave} not found, creating it..."
mkdir -p "${pathToSave}" || exit 1
fi ;;
s)
# take shot of selection
doSelection=true ;;
t)
# use fiery token
fieryToken="${OPTARG}" ;;
u)
# change $file to the specified file with -u
doFile=true
localFile="${OPTARG}"
if [ ! -f "${localFile}" ]; then
echo "Checking for file... [${c1}FAILED${c0}]" >&2
echo 'File not found.' >&2
exit 1
fi ;;
w)
# take shot of current window
doWindow=true ;;
x)
# do not notify dbus, update log, or modify clipboard
noComms=true ;;
D)
# set safe.fiery.me file domain
fieryMeDomain="${OPTARG}"
if [[ ! "${fieryMeDomain}" =~ $httpProtocolRegex ]]; then
fieryMeDomain="https:\/\/${fieryMeDomain}"
fi ;;
F)
# set fiery host domain
fieryHostDomain="${OPTARG}"
if [[ ! "${fieryHostDomain}" =~ $httpProtocolRegex ]]; then
fieryHostDomain="https://${fieryHostDomain}"
fi ;;
S)
# set shortener
for available in ${shorteners}; do
if [ "${OPTARG}" = "${available}" ]; then
shortener="${OPTARG}"
shortenercheck=true
break
fi
done
[ "${shortenercheck}" ] || exit 1 ;;
X)
# skip upload
skipUpload=true ;;
*)
# print help and EXIT_FAILURE
usage
exit 1 ;;
esac
done
# show usage if no action is chosen
if [ ! "${doFullscreen}" ] && [ ! "${doURL}" ] && [ ! "${doSelection}" ] && [ ! "${doFile}" ] && [ ! "${doWindow}" ]; then
if [ "${fieryAlbumId}" ] || [ "${fieryToken}" ] || [ "${fieryMeDomain}" ] || [ "${fieryHostDomain}" ]; then
echo "Fiery-specific options must be used during screenshots.
Example: $(basename "${0}") -t TOKEN_HERE -s
to take a selection screenshot and upload to fiery host using the token." >&2
else
usage
fi
exit 1
fi
## EXECUTE FUNCTIONS
depends
# if not uploading a local file
if [ ! "${doFile}" ]; then
delay
screenshot
copyimage
path
fi
if [ "${skipUpload}" ]; then
if [ ! "${noComms}" ]; then
notify "Screenshot taken, but skipped upload." "document-save"
fi
# if we took a screenshot, remove the temporary file
if [ ! "${doFile}" ]; then
rm "${localFile}"
fi
exit
else
upload
fi
# if the program doesn't exit at the for-loop, the upload failed
echo 'File was not uploaded, did you specify a valid filename?'