/
sour
executable file
·283 lines (242 loc) · 7.25 KB
/
sour
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
#!/bin/bash
function usage {
cat <<EOF
sour: usage:
-h | --help help
-s | --select select area (does scrot -s)
-f | --forum also make thumbnail, output with BBCode
-k | --keep keep images when done, don't remove
-u | --url {URL} don't scrot; download {URL} instead
-e | --existing {PATH} don't scrot; use image in {PATH}
-p | --prefix "MSG: " result is "MSG: URL" (not compatible with -t or -f)
- options must be used separately (i.e.: "-s -f", not "-sf")
- when using an existing image, it won't be removed regardless of your
usage of --keep (thumbnails do get removed unless you use -k or --keep)
EOF
}
if [[ $1 == '-h' || $1 == '--help' || $1 == '-?' ]]; then
usage
exit 0
fi
function setDefaults {
TMPDIR=${TMPDIR:-/tmp}
location=$(mktemp -d "$TMPDIR/XXXXXXXXXXXXXX") ||\
{ echo "ERROR could not create temporary directory" >&2; exit 1; }
working_dir="$PWD"
logdestfile="$HOME/.sour/log/$(date +"%m-%d-%y").log"
keyfile="$HOME/.sour/key"
datestamp="$(date +"%F_%H-%M-%S")"
# For createNeededFiles
needed_files=("$logdestfile")
needed_dirs=("$HOME/.sour" "$HOME/.sour/log")
default_key='https://raw.github.com/wolfcore/sour/master/key'
thumbcommand=''
selectioncommand=''
forum=false
keep=false
download=false
existing=false
# this will be changed depending on if there is a download or use of an
# existing image
extension='png'
prefix=''
largeimageurl=''
largeimagedel=''
smallimageurl=''
smallimagedel=''
firstdone=false
}
# Files and directories needed for successful execution:
# - .sour/
# - .sour/log/
# - .sour/key
# - .sour/log/(datestamp).log
# Creating directories before populating with needed_files.
function createNeededFiles {
for directory in "${needed_dirs[@]}"; do
if [[ ! -d "$directory" ]]; then
if ! mkdir "$directory" &> /dev/null; then
echo "ERROR: could not create directory $directory" >&2
exit 1
fi
fi
done
for file in "${needed_files[@]}"; do
if [[ ! -e "$file" ]]; then
if ! touch "$file" &> /dev/null; then
echo "ERROR: could not create $file" >&2
exit 1
fi
fi
done
# File exists and is a regular file, file exists and isn't empty
if [[ ! -f "$keyfile" || ! -s "$keyfile" ]]; then
curl $default_key -o $keyfile > /dev/null 2>&1
fi
source $keyfile
}
function moveTo {
local file="$1"
local dest="$2"
# checking that the file exists before moving
if [[ -e $file ]]; then
if ! mv $file $dest &> /dev/null; then
# Try sending the file to home dir.
if [[ ! $dest == $HOME ]]; then
echo "failed to move $file to $dest...sending to $HOME" >&2
moveTo $file $HOME
else
die "ERROR: failed to move $file to $dest"
fi
fi
fi
}
function removeTemporaries {
if [[ $keep == true ]]; then
if [[ $existing == true ]]; then
if [[ $forum == true ]]; then
moveTo $smallimagelocation $working_dir
else
# If the if keep == true, existing == true, and forum ==
# false, that means that the only image that was used was not
# created by the program, but already existed, so we leave it.
# Do nothing
:
fi
# existing == false, but we need to keep whatever images we have.
else
if [[ $forum == true ]]; then
moveTo $smallimagelocation $working_dir
fi
# Regardless of the thumbnail, we still need to move the regular
# image
moveTo $largeimagelocation $working_dir
fi
# keep == false, remove everything
else
rm --force --recursive $location
fi
}
function setTraps {
trap 'removeTemporaries' EXIT
}
function die {
echo -e "ERROR: $@\a" >&2
echo '' | xclip
exit 1
}
function sendOutput {
echo "$datestamp: $largeimageurl $largeimagedel\
$smallimageurl $smallimagedel" >> "$logdestfile"
if [[ $forum == true ]]; then
echo "[url=$largeimageurl][img]$smallimageurl[/img][/url]"
else
# The prefix is equal to '' if it was not changed by the user.
echo "${prefix}${largeimageurl}"
fi
}
function uploadFile {
curl -s -F "image=@$1" -F "key=$key" http://imgur.com/api/upload.xml
}
function parseAndReturn {
local resp="$@"
if [[ $resp = *error* ]]; then
die "error found in response"
fi
url=${resp#*<original_image>}
url=${url%</original_image>*}
del=${resp#*<delete_page>}
del=${del%</delete_page>*}
# If correct, url will be length of 28
local url_length="${#url}"
(( url_length != 28)) && die "url too large or equal to zero"
if [[ $firstdone == false ]]; then
largeimageurl="$url"
largeimagedel="$del"
firstdone=true
else
smallimageurl="$url"
smallimagedel="$del"
fi
}
function makeImageUpload {
largeimageresponse=$(uploadFile "$largeimagelocation")
parseAndReturn "$largeimageresponse"
checkAndMakeThumb
}
function checkAndMakeThumb {
if [[ $forum == true ]]; then
if [[ $download == true || $existing == true ]]; then
cp "$largeimagelocation" "$smallimagelocation"
mogrify -path "$location" -resize 250x250 "$smallimagelocation"
fi
smallimageresponse=$(uploadFile "$smallimagelocation")
parseAndReturn "$smallimageresponse"
fi
}
function takeShot {
# These have to be defined after $extension has been changed by the option
# parsing.
largeimagelocation="$location/$datestamp.$extension"
smallimagelocation="$location/$datestamp-thumb.$extension"
if [[ $download == false && $existing == false ]]; then
scrot $selectioncommand "$largeimagelocation" $thumbcommand
elif [[ $download == true ]]; then
curl "$dl_source" -o "$largeimagelocation" > /dev/null 2>&1
elif [[ $existing == true ]]; then
largeimagelocation="$ex_source"
fi
setTraps
}
function setExtension {
extension="${1##*.}"
}
setDefaults
while :
do
case $1 in
-f | --forum)
forum=true
thumbcommand='--thumb 20'
shift
;;
-s | --select)
selectioncommand='--select'
shift
;;
-k | --keep)
keep=true
shift
;;
-u | --url)
download=true
dl_source="$2"
setExtension "$dl_source"
shift 2
;;
-e | --existing)
existing=true
ex_source="$2"
setExtension "$ex_source"
shift 2
;;
-p | --prefix)
prefix="$2"
shift
;;
--) shift
break
;;
-*) echo -e "unknown option: $1\n" >&2
usage
exit 1
shift
;;
*) break
;;
esac
done
createNeededFiles
takeShot
makeImageUpload
sendOutput