forked from gregkh/bti
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bti-shrink-urls
executable file
·114 lines (101 loc) · 3.86 KB
/
bti-shrink-urls
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
#!/bin/bash
# Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
needs_escape=true
shrink_host=2tu.us
test -f ~/.bti && source ~/.bti || test -f ~/.config/bti && source ~/.config/bti
while test -n "$1" ; do
word="$1"
shift
case "$word" in
--escaped)
needs_escape=
;;
--help|-h)
cat <<END
bti-shrink-urls - convert URLs to a shorter form using a web service
$0 [--escaped] [<url>]
Currently supported: 2tu.us (default), bit.ly, j.mp.
END
exit 0
;;
*)
URL=$word
;;
esac
done
function convert_url() {
local url=$1
test -n "$url" || return 1
test "${url%%:*}" = 'http' || return 1
local urllen="${#url}"
# http://en.wikipedia.org/wiki/Percent-encoding
if test -n "$needs_escape" ; then
url=$(echo "$url" | sed -e 's/\%/%25/g' \
-e 's/!/%21/g' \
-e 's/*/%2A/g' \
-e "s/'/%27/g" \
-e 's/(/%28/g' \
-e 's/)/%29/g' \
-e 's/;/%3B/g' \
-e 's/:/%3A/g' \
-e 's/@/%40/g' \
-e 's/&/%26/g' \
-e 's/=/%3D/g' \
-e 's/+/%2B/g' \
-e 's/\$/%24/g' \
-e 's/,/%2C/g' \
-e 's,/,%2F,g' \
-e 's/?/%3F/g' \
-e 's/#/%23/g' \
-e 's/\[/%5B/g' \
-e 's/]/%5D/g')
fi
case $shrink_host in
2tu.us)
local submit="http://2tu.us/?save=y&url=$url"
local res=$(wget -q -O - "$submit" | awk -F"'" '/Your tight URL is:/ { print $2 }')
;;
bit.ly|j.mp)
if [ -z "$shrink_bitly_login" -o -z "$shrink_bitly_key" ]; then
echo "To use $shrink_host you must set 'shrink_bitly_login' and 'shrink_bitly_key' in ~/.bti" >&2
exit 1
fi
local submit="http://api.bit.ly/v3/shorten?format=txt&login=$shrink_bitly_login&apiKey=$shrink_bitly_key&domain=$shrink_host&longUrl=$url"
local res=$(wget -q -O - "$submit")
;;
*)
echo "Shrinking with $shrink_host is not supported." >&2
exit 1
;;
esac
if test "${res%%:*}" = 'http' -a "${#res}" -lt "$urllen" ; then
echo $res
return 0
fi
return 1
}
function die() {
echo >&2 $@
exit 1
}
if test -n "$URL" ; then
convert_url "$URL" || die "Failed to shrink '$URL'"
exit $?
fi
test -t 0 && echo >&2 "Type in some urls and I'll try to shrink them for you..."
while read line ; do
convert_url "$line" || echo $line
done