forked from whats-this/owo.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
executable file
·340 lines (284 loc) · 9.28 KB
/
script.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
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
#!/bin/bash
# _ _
# | | | |
# _____ _____ _ _ _ __ | | ___ __ _ __| | ___ _ __
# / _ \ \ /\ / / _ \ | | | | '_ \| |/ _ \ / _` |/ _` |/ _ \ '__|
# |(_) \ V V / (_)| | |_| | |_) | | (_) | (_| | (_| | __/ |
# \___/ \_/\_/ \___/ \__,_| .__/|_|\___/ \__,_|\__,_|\___|_|
# | |
# |_|
#
# OWOUPLOADER.SH SCRIPT.
# ----------------------
#
# This script allows for native support to upload to the image server
# and url shortener component of whats-th.is. Through this you can do
# plethora of actions.
#
# A big thank you to jomo/imgur-screenshot to which I've edited parts
# of his script into my own.
if [ ! $(id -u) -ne 0 ]; then
echo "ERROR : This script cannot be run as sudo."
echo "ERROR : You need to remove the sudo from \"sudo ./setup.sh\"."
exit 1
fi
owodir="$HOME/.config/owo"
current_version="v0.0.17"
##################################
if [ ! -d $owodir ]; then
echo "INFO : Could not find config directory. Please run setup.sh"
exit 1
fi
if [ ! -d $path ]; then
mkdir -p $path
fi
source $owodir/conf.cfg
key=$userkey >&2
output_url=$finished_url >&2
directoryname=$scr_directory >&2
filename=$scr_filename >&2
path=$scr_path >&2
print_debug=$debug >&2
##################################
function is_mac() {
uname | grep -q "Darwin"
}
function check_key() {
if [ -z "$key" ]; then
echo "INFO : \$key not found, please set \$userkey in your config file."
echo "INFO : You can find the key in $owodir/conf.cfg"
exit 1
fi
}
function notify() {
if is_mac; then
/usr/local/bin/terminal-notifier -title owo.whats-th.is -message "${1}" -appIcon $owodir/icon.icns
else
notify-send owo.whats-th.is "${1}" -i $owodir/icon.png
fi
}
function delete_scr() {
if [ "$keep_scr" != "true" ]; then
rm "$path$filename"
fi
}
function clipboard() {
if is_mac; then
echo "${1}" | tr -d "\n\r" | pbcopy
else
echo "${1}" | tr -d "\n\r" | xclip -i -sel c -f | xclip -i -sel p
fi
}
function shorten() {
check_key
if [ -z "${2}" ]; then
notify "Please enter the URL you wish to shorten."
echo "INFO : Please enter the URL you wish to shorten."
read url
else
url=${2}
fi
#Check if the URL entered is valid.
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
if [[ $url =~ $regex ]]; then
result=$(curl -s "https://api.awau.moe/shorten/polr?action=shorten&key=$key&url=$url")
#Check if the URL got sucessfully shortened.
if grep -q "https://" <<< "${result}"; then
d=$1
if [ "$d" = "true" ]; then
clipboard $result
echo $result
notify "Copied the link to the keyboard."
else
echo $result
fi
else
notify "Shortening failed!"
fi
else
notify "URL is not valid!"
fi
}
function screenshot() {
check_key
# Alert the user that the upload has begun.
notify "Select an area to begin the upload."
# Begin our screen capture.
if is_mac; then
screencapture -o -i $path$filename
else
maim -s $path$filename
fi
# Make a directory for our user if it doesnt already exsist.
mkdir -p $path
# Open our new entry to use it!
entry=$path$filename
upload=$(curl -s -F "files[]=@"$entry";type=image/png" https://api.awau.moe/upload/pomf?key="$key")
if [ "$print_debug" = true ] ; then
echo $upload
fi
if egrep -q '"success":\s*true' <<< "${upload}"; then
item="$(egrep -o '"url":\s*"[^"]+"' <<<"${upload}" | cut -d "\"" -f 4)"
d=$1
if [ "$d" = true ]; then
if [ "$scr_copy" = true ]; then
clipboard "https://$output_url/$item"
notify "Upload complete! Copied the link to your clipboard."
else
echo "https://$output_url/$item"
fi
else
output="https://$output_url/$item"
fi
else
notify "Upload failed! Please check your logs ($owodir/log.txt) for details."
echo "UPLOAD FAILED" > $owodir/log.txt
echo "The server left the following response" >> $owodir/log.txt
echo "--------------------------------------" >> $owodir/log.txt
echo " " >> $owodir/log.txt
echo " " $upload >> $owodir/log.txt
fi
delete_scr
}
function upload() {
check_key
entry=$1
mimetype=$(file -b --mime-type $entry)
filesize=$(wc -c <"$entry")
if [[ $filesize -le 83886081 ]]; then
upload=$(curl -s -F "files[]=@"$entry";type=$mimetype" https://api.awau.moe/upload/pomf?key="$key")
item="$(egrep -o '"url":\s*"[^"]+"' <<<"${upload}" | cut -d "\"" -f 4)"
else
echo "ERROR : File size too large or another error occured!"
exit 1
fi
if egrep -q '"success":\s*true' <<< "${upload}"; then
d=$2
if [ "$d" = true ]; then
clipboard "https://$output_url/$item"
echo "https://$output_url/$item"
else
output="https://$output_url/$item"
fi
else
notify "Upload failed! Please check your logs ($owodir/log.txt) for details."
echo "UPLOAD FAILED" > $owodir/log.txt
echo "The server left the following response" >> $owodir/log.txt
echo "--------------------------------------" >> $owodir/log.txt
echo " " >> $owodir/log.txt
echo " " $upload >> $owodir/log.txt
fi
}
function runupdate() {
cp $owodir/conf.cfg $owodir/conf_backup_$current_version.cfg
if [ ! -d $owodir/.git ]; then
git -C $owodir init
git -C $owodir remote add origin https://github.com/whats-this/owo.sh.git
git -C $owodir fetch --all
git -C $owodir checkout -t origin/stable
git -C $owodir reset --hard origin/stable
else
git -C $owodir pull origin stable
fi
}
##################################
if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]; then
echo "usage: ${0} [-h | --check | -v]"
echo ""
echo " -h --help Show this help screen to you."
echo " -v --version Show current application version."
echo " -c --check Checks if dependencies are installed."
echo " --update Checks if theres an update available."
echo " -l --shorten Begins the url shortening process."
echo " -s --screenshot Begins the screenshot uploading process."
echo " -sl Takes a screenshot and shortens the URL."
echo " -ul Uploads file and shortens URL."
echo ""
exit 0
fi
##################################
if [ "${1}" = "-v" ] || [ "${1}" = "--version" ]; then
echo "INFO : You are on version $current_version"
exit 0
fi
##################################
if [ "${1}" = "-c" ] || [ "${1}" = "--check" ]; then
if is_mac; then
(which terminal-notifier &>/dev/null && echo "FOUND : found terminal-notifier") || echo "ERROR : terminal-notifier not found"
(which screencapture &>/dev/null && echo "FOUND : found screencapture") || echo "ERROR : screencapture not found"
(which pbcopy &>/dev/null && echo "FOUND : found pbcopy") || echo "ERROR : pbcopy not found"
else
(which notify-send &>/dev/null && echo "FOUND : found notify-send") || echo "ERROR : notify-send (from libnotify-bin) not found"
(which maim &>/dev/null && echo "FOUND : found maim") || echo "ERROR : maim not found"
(which xclip &>/dev/null && echo "FOUND : found xclip") || echo "ERROR : xclip not found"
fi
(which curl &>/dev/null && echo "FOUND : found curl") || echo "ERROR : curl not found"
(which grep &>/dev/null && echo "FOUND : found grep") || echo "ERROR : grep not found"
exit 0
fi
##################################
if [ "${1}" = "--update" ]; then
if [ "${1}" = "-C" ]; then
owodir="${4}"
fi
remote_version="$(curl --compressed -fsSL --stderr - "https://api.github.com/repos/whats-this/owo.sh/releases" | egrep -m 1 --color 'tag_name":\s*".*"' | cut -d '"' -f 4)"
if [ "${?}" -eq "0" ]; then
if [ ! "${current_version}" = "${remote_version}" ] && [ ! -z "${current_version}" ] && [ ! -z "${remote_version}" ]; then
echo "INFO : Update found!"
echo "INFO : Version ${remote_version} is available (You have ${current_version})"
echo "ALERT : You already have a configuration file in $owodir"
echo "ALERT : Updating might break this config, are you sure you want to update?"
read -p "INFO : Continue anyway? (Y/N)" choice
case "$choice" in
y|Y ) runupdate;;
n|N ) exit 0;;
* ) echo "ERROR : That is an invalid response, (Y)es/(N)o.";;
esac
elif [ -z "${current_version}" ] || [ -z "${remote_version}" ]; then
echo "ERROR : Version string is invalid."
echo "INFO : Current (local) version: '${current_version}'"
echo "INFO : Latest (remote) version: '${remote_version}'"
else
echo "INFO : Version ${current_version} is up to date."
fi
else
echo "ERROR : Failed to check for latest version: ${remote_version}"
fi
exit 0
fi
##################################
if [ "${1}" = "-l" ] || [ "${1}" = "--shorten" ]; then
shorten true
exit 0
fi
##################################
if [ "${1}" = "-s" ] || [ "${1}" = "--screenshot" ]; then
screenshot true
exit 0
fi
##################################
if [ "${1}" = "-sl" ]; then
screenshot false
shorten true $output
exit 0
fi
##################################
if [ "${1}" = "-ul" ]; then
if [ -z "$2" ]; then
echo "ERROR : Sorry, but thats incorrect syntax."
echo "ERROR : Please use \"owo file.png\""
exit 1
fi
upload ${2} false
shorten true $output
echo $result
if is_mac; then
echo $result | pbcopy
else
echo $result | xclip -i -sel c -f | xclip -i -sel p
fi
notify "Copied link to keyboard."
exit 0
fi
upload ${1} true
notify "Copied link to keyboard."