2 # Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation version 2 of the License.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 while test -n "$1" ; do
28 bti-shrink-urls - convert URLs to a shorter form using a web service
30 $0 [--escaped] [<url>]
32 Currently only http://2tu.us/ is supported.
42 function convert_url() {
44 test -n "$url" || return 1
45 test "${url%%:*}" = 'http' || return 1
47 local urllen="${#url}"
49 # http://en.wikipedia.org/wiki/Percent-encoding
50 if test -n "$needs_escape" ; then
51 url=$(echo "$url" | sed -e 's/\%/%25/g' \
73 local submit="http://2tu.us/?save=y&url=$url"
75 local res=$(wget -q -O - "$submit" | awk -F"'" '/Your tight URL is:/ { print $2 }')
76 if test "${res%%:*}" = 'http' -a "${#res}" -lt "$urllen" ; then
88 if test -n "$URL" ; then
89 convert_url "$URL" || die "Failed to shrink '$URL'"
93 test -t 0 && echo >&2 "Type in some urls and I'll try to shrink them for you..."
95 convert_url "$line" || echo $line