-
Notifications
You must be signed in to change notification settings - Fork 21
/
thunar-upload-to-imgur.sh
executable file
·151 lines (132 loc) · 3.85 KB
/
thunar-upload-to-imgur.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
#!/usr/bin/env bash
#
# Upload a Picture to imgur.
#
# * Put this file into your home binary dir: ~/bin/
# * Make it executable: chmod +x
#
#
# Required Software:
# -------------------------
# * zenity
# * curl
# * gawk
#
#
# Thunar Integration
# ------------------------
#
# Command: ~/bin/thunar-upload-to-imgur.sh -f %f
# File Pattern: *
# Appear On: Image Files
#
#
# Usage:
# -------------------------
# thunar-upload-to-imgur.sh -f <filename> [-w width(int)] [-h height(int)] [-t window-title]
#
# required:
# -f input filename
#
# optional:
#
# -w (gui) width of window (e.g.: -w 800)
# default is 800
#
# -h (gui) height of window (e.g.: -h 240)
# default is 240
#
# -t (gui) window title
# default is filename
#
# Note:
# -------------------------
#
# Feel free to adjust/improve and commit back to:
# https://github.com/cytopia/thunar-custom-actions
#
usage() {
echo "$0 -f <filename> [-w width(int)] [-h height(int)] [-t window-title]"
echo
echo " required:"
echo " -f input filename"
echo
echo " optional:"
echo " -w (gui) width of window (e.g.: -w 800)"
echo " default is 800"
echo
echo " -h (gui) height of window (e.g.: -h 240)"
echo " default is 240"
echo
echo " -t (gui) window title"
echo " default is filename"
echo
}
while getopts ":f:cw:h:t:" i; do
case "${i}" in
f)
f="${OPTARG}"
;;
w)
w="${OPTARG}"
;;
h)
h="${OPTARG}"
;;
t)
t="${OPTARG}"
;;
*)
echo "Error - unrecognized option $1" 1>&2;
usage
;;
esac
done
shift $((OPTIND-1))
# Check if file is specified
if [ -z "${f}" ]; then
echo "Error - no file specified" 1>&2;
usage
exit 1
fi
# Check if zenity exists
if ! command -v zenity >/dev/null 2>&1 ; then
echo "Error - 'zenity' not found." 1>&2
exit 1
fi
# Check if curl exists
if ! command -v curl >/dev/null 2>&1 ; then
echo "Error - 'curl' not found." 1>&2
exit 1
fi
# Check if gawk exists
if ! command -v gawk >/dev/null 2>&1 ; then
echo "Error - 'gawk' not found." 1>&2
exit 1
fi
TITLE='Uploading to Imgur...'$(basename "${f}")
IMGUR_CLIENT_ID="3e7a4deb7ac67da"
TMPFILE=$(mktemp)
[ ! -z "${w##*[!0-9]*}" ] && WIDTH=$w || WIDTH=350
[ ! -z "${h##*[!0-9]*}" ] && HEIGHT=$h || HEIGHT=140
[ -n "${t}" ] && TITLE="${t}" || TITLE="Uploading to imgur: $(basename "${f}")"
curl -# -F "image"=@"$f" -o "${TMPFILE}" -F title="${TITLE}" -H "Authorization: Client-ID ${IMGUR_CLIENT_ID}" https://api.imgur.com/3/upload.xml 2>&1 | gawk -v RS='\r' '{print $2; fflush("") }' | zenity --width="${WIDTH}" --height="${HEIGHT}" --progress --title="${TITLE}" --text="${TITLE}" --auto-close --time-remaining
#curl -# -F "image"=@"$f" -F "key"="4907fcd89e761c6b07eeb8292d5a9b2a" http://imgur.com/api/upload.xml | grep -Eo "[0-9]{1,3}" | zenity --width=${WIDTH} --height=${HEIGHT} --progress --title="${TITLE}" --text="${TITLE}" --auto-close --time-remaining
#stdbuf -oL tr $'\r' $'\n' | stdbuf -oL grep --line-buffered -Eo '([0-9]+)\.[0-9]%$' | zenity --width=${WIDTH} --height=${HEIGHT} --progress --title="${TITLE}" --text="${TITLE}" --auto-close --time-remaining
########################## gui output ###############################
[ -n "${t}" ] && TITLE=$t || TITLE="Uploaded to imgur: $(basename "${f}")"
TEXT=$(cat "${TMPFILE}")
rm "${TMPFILE}"
#TAG="$(echo "${TEXT}" | grep -Eo '<[a-z_]+>http' | sed -e "s/http//" | sed -e "s/<//" | sed -e "s/>//")"
#URL="$(echo "${TEXT}" | grep -Eo 'http[^<]+')"
#ZTEXT=""
#urls=($URL)
#tags=($TAG)
URL="$(echo "${TEXT}" | grep -E -m 1 -o "<link>(.*)</link>" | sed -e 's,.*<link>\([^<]*\)</link>.*,\1,g')"
ZTEXT="Direct URL: <a href=\"$URL\">$URL</a>"
#for ((i = 0; i < ${#urls[@]}; i++))
#do
# ZTEXT="$ZTEXT${tags[$i]}' <a href=\"${urls[$i]}\">${urls[$i]}</a>\n"
#done
zenity --width=${WIDTH} --height=${HEIGHT} --info --title "${TITLE}" --text="${ZTEXT}"
exit $?