updated man pages generated from the docbook source
[gregoa/bti.git] / bti-shrink-urls
1 #!/bin/bash
2 # Copyright (C) 2009 Bart Trojanowski <bart@jukie.net>
3 #
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.
7 #
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.
12 #
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.
16
17 needs_escape=true
18
19 shrink_host=2tu.us
20 test -f ~/.bti && . ~/.bti
21
22 while test -n "$1" ; do
23         word="$1"
24         shift
25         case "$word" in
26             --escaped)
27                 needs_escape=
28                 ;;
29             --help|-h)
30                 cat <<END
31 bti-shrink-urls - convert URLs to a shorter form using a web service
32
33     $0 [--escaped] [<url>]
34
35 Currently supported: 2tu.us (default), bit.ly, j.mp.
36 END
37                 exit 0
38                 ;;
39             *)
40                 URL=$word
41                 ;;
42         esac
43 done
44
45 function convert_url() {
46         local url=$1
47         test -n "$url" || return 1
48         test "${url%%:*}" = 'http' || return 1
49
50         local urllen="${#url}"
51
52         # http://en.wikipedia.org/wiki/Percent-encoding
53         if test -n "$needs_escape" ; then
54                 url=$(echo "$url" | sed -e 's/\%/%25/g' \
55                                         -e 's/!/%21/g' \
56                                         -e 's/*/%2A/g' \
57                                         -e "s/'/%27/g" \
58                                         -e 's/(/%28/g' \
59                                         -e 's/)/%29/g' \
60                                         -e 's/;/%3B/g' \
61                                         -e 's/:/%3A/g' \
62                                         -e 's/@/%40/g' \
63                                         -e 's/&/%26/g' \
64                                         -e 's/=/%3D/g' \
65                                         -e 's/+/%2B/g' \
66                                         -e 's/\$/%24/g' \
67                                         -e 's/,/%2C/g' \
68                                         -e 's,/,%2F,g' \
69                                         -e 's/?/%3F/g' \
70                                         -e 's/#/%23/g' \
71                                         -e 's/\[/%5B/g' \
72                                         -e 's/]/%5D/g')
73         fi
74
75         case $shrink_host in
76         2tu.us)
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 }')
79             ;;
80         bit.ly|j.mp)
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
83                 exit 1
84             fi
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")
87             ;;
88         *)
89             echo "Shrinking with $shrink_host is not supported." >&2
90             exit 1
91             ;;
92         esac
93
94         if test "${res%%:*}" = 'http' -a "${#res}" -lt "$urllen" ; then
95                 echo $res
96                 return 0
97         fi
98         return 1
99 }
100
101 function die() {
102         echo >&2 $@
103         exit 1
104 }
105
106 if test -n "$URL" ; then
107         convert_url "$URL" || die "Failed to shrink '$URL'"
108         exit $?
109 fi
110
111 test -t 0 && echo >&2 "Type in some urls and I'll try to shrink them for you..."
112 while read line ; do
113         convert_url "$line" || echo $line
114 done