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.
20 test -f ~/.bti && source ~/.bti || test -f ~/.config/bti && source ~/.config/bti
22 while test -n "$1" ; do
31 bti-shrink-urls - convert URLs to a shorter form using a web service
33 $0 [--escaped] [<url>]
35 Currently supported: 2tu.us (default), bit.ly, j.mp.
45 function convert_url() {
47 test -n "$url" || return 1
48 test "${url%%:*}" = 'http' || return 1
50 local urllen="${#url}"
52 # http://en.wikipedia.org/wiki/Percent-encoding
53 if test -n "$needs_escape" ; then
54 url=$(echo "$url" | sed -e 's/\%/%25/g' \
77 local submit="http://2tu.us/?save=y&url=$url"
78 local res=$(wget -q -O - "$submit" | awk -F"'" '/Your tight URL is:/ { print $2 }')
81 if [ -z "$shrink_bitly_login" -o -z "$shrink_bitly_key" ]; then
82 echo "To use $shrink_host you must set 'shrink_bitly_login' and 'shrink_bitly_key' in ~/.bti" >&2
85 local submit="http://api.bit.ly/v3/shorten?format=txt&login=$shrink_bitly_login&apiKey=$shrink_bitly_key&domain=$shrink_host&longUrl=$url"
86 local res=$(wget -q -O - "$submit")
89 echo "Shrinking with $shrink_host is not supported." >&2
94 if test "${res%%:*}" = 'http' -a "${#res}" -lt "$urllen" ; then
106 if test -n "$URL" ; then
107 convert_url "$URL" || die "Failed to shrink '$URL'"
111 test -t 0 && echo >&2 "Type in some urls and I'll try to shrink them for you..."
113 convert_url "$line" || echo $line