-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuguu.sh
executable file
·164 lines (135 loc) · 4.7 KB
/
uguu.sh
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
#!/bin/bash
# uguu.se screenshotter and file uploader
# >>> Options <<<
savedir="$HOME/Pictures/Screenshots"
filename="$(date '+%Y-%m-%d_%H-%M-%S')"
maxsize=1048576 # Max filesize before going with jpg (in bytes)
icon="$HOME/Pictures/uguu.png"
# Available options are: spectacle,scrot
screenshot_tool="spectacle"
# >>> Requirements <<<
# - Spectacle/scrot
# - curl
# - imagemagick
# - xclip / wl-clipboard
# - xdotool (opt. for getting windows' process name) (xorg only)
# >>> The fun begins! <<<
help() {
echo 'Usage:
-a area screenshot
-w window screenshot
-f full screenshot
-u upload file(s)
-l upload list of files (one file per line)
-r re-upload URL to uguu.se'
exit
}
reupload() {
# Gotta store that shit somewhere
tempfile=$(mktemp --dry-run --quiet)
# Download file
curl --silent --location --fail --output "$tempfile" "$1"
# Check filetype and grab only first example `file` spits out
fileext=$(file --extension --brief "$tempfile")
fileext=${fileext%%/*}
# Skip adding extension if `file` can't figure it out
if [ "$fileext" = "???" ]; then
unset fileext
tempfilewithext=$tempfile
else
# Move file to have (hopefully) proper extension
tempfilewithext=$(mktemp --dry-run --quiet --suffix=."$fileext")
mv "$tempfile" "$tempfilewithext"
fi
# Upload file
uploader "$tempfilewithext"
# Remove temporary file
rm "$tempfilewithext"
}
uploader() {
for file in "$@"; do
output=$(curl --form "files[]=@$file" --progress-bar https://uguu.se/upload.php)
# If upload isn't successful, tell user
if ! echo "$output" | grep -q 'success": true'; then
echo "$output"
exit 1
fi
echo "Link: $(echo "$output" | grep -Po '"url": "\K[^"]*' | sed 's/\\//g')"
done
}
screenshotter() (
# The file needs to go *somewhere* before processing
tempfile=$(mktemp --dry-run --quiet --suffix=.png)
# Pick screenshoot tool
if [ "$screenshot_tool" = "spectacle" ]; then
# Check spectacle version for changed clipboard command
spectaclever="$(spectacle -v | awk '{print $2}')"
if (( $(echo "$spectaclever" "21.07.70" | awk '{print ($1 < $2)}') )); then
cliparg="--clipboard"
else
cliparg="--copy-image"
fi
screenshot_base_command() {
spectacle "$@" --background --nonotify $cliparg --output ${tempfile}
}
if [ "$1" = fullscreen ]; then
screenshot_base_command --fullscreen
elif [ "$1" = activewindow ]; then
screenshot_base_command --activewindow
elif [ "$1" = region ]; then
screenshot_base_command --region
fi
elif [ "$screenshot_tool" = "scrot" ]; then
screenshot_base_command() {
scrot "$@" --quality 100 --silent --pointer "${tempfile}" -e 'xclip -selection clipboard -t image/png $f'
}
if [ "$1" = fullscreen ]; then
screenshot_base_command
elif [ "$1" = activewindow ]; then
screenshot_base_command --focused --border --stack
elif [ "$1" = region ]; then
screenshot_base_command --select --freeze --stack
fi
else
echo "Invalid screenshot tool selected!"
exit 1
fi
# Exit if file is empty (no screenshot taken)
[ ! -f "$tempfile" ] && exit
# Create directory if it doesn't exist
[ ! -d "$savedir" ] && mkdir -p "$savedir"
# Check filesize and convert if too big
filesize=$(stat -c%s "${tempfile}")
if (("$filesize" > "$maxsize")); then
screenshot="${savedir}/${currentwindow}${filename}.jpg"
convert -format jpg "${tempfile}" "${screenshot}"
rm "${tempfile}"
else
screenshot="${savedir}/${currentwindow}${filename}.png"
mv "${tempfile}" "$screenshot"
fi
# Upload file
uploader "$screenshot"
# Add to clipboard
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
clipboard_command="wl-copy --trim-newline"
else
clipboard_command="xclip -selection clipboard -rmlastnl"
fi
echo "$output" | grep -Po '"url": "\K[^"]*' | sed 's/\\//g' | $clipboard_command
# Send out desktop notifcation
notify-send --urgency=low --expire-time=2000 --category=transfer.complete --icon "$icon" "$filename uploaded!"
)
while getopts awfulr options; do
case $options in
a) screenshotter region ;;
w) screenshotter activewindow ;;
f) screenshotter fullscreen ;;
u) uploader "${@:2}" ;;
l) while IFS=$'\n' read -r line; do uploader "$line"; done <"$2" ;;
r) reupload "${@:2}" ;;
*) help ;;
esac
done
# Display help if no argument passed
[ $# -eq 0 ] && help