Merge branch 'master' of git://github.com/psykoyiko/bti
[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 while test -n "$1" ; do
20         word="$1"
21         shift
22         case "$word" in
23             --escaped)
24                 needs_escape=
25                 ;;
26             --help|-h)
27                 cat <<END
28 bti-shrink-urls - convert URLs to a shorter form using a web service
29
30     $0 [--escaped] [<url>]
31
32 Currently only http://2tu.us/ is supported.
33 END
34                 exit 0
35                 ;;
36             *)
37                 URL=$word
38                 ;;
39         esac
40 done
41
42 function convert_url() {
43         local url=$1
44         test -n "$url" || return 1
45         test "${url%%:*}" = 'http' || return 1
46
47         local urllen="${#url}"
48
49         # http://en.wikipedia.org/wiki/Percent-encoding
50         if test -n "$needs_escape" ; then
51                 url=$(echo "$url" | sed -e 's/\%/%25/g' \
52                                         -e 's/!/%21/g' \
53                                         -e 's/*/%2A/g' \
54                                         -e "s/'/%27/g" \
55                                         -e 's/(/%28/g' \
56                                         -e 's/)/%29/g' \
57                                         -e 's/;/%3B/g' \
58                                         -e 's/:/%3A/g' \
59                                         -e 's/@/%40/g' \
60                                         -e 's/&/%26/g' \
61                                         -e 's/=/%3D/g' \
62                                         -e 's/+/%2B/g' \
63                                         -e 's/\$/%24/g' \
64                                         -e 's/,/%2C/g' \
65                                         -e 's,/,%2F,g' \
66                                         -e 's/?/%3F/g' \
67                                         -e 's/#/%23/g' \
68                                         -e 's/\[/%5B/g' \
69                                         -e 's/]/%5D/g')
70         fi
71
72         # http://2tu.us/
73         local submit="http://2tu.us/?save=y&url=$url"
74
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
77                 echo $res
78                 return 0
79         fi
80         return 1
81 }
82
83 function die() {
84         echo >&2 $@
85         exit 1
86 }
87
88 if test -n "$URL" ; then
89         convert_url "$URL" || die "Failed to shrink '$URL'"
90         exit $?
91 fi
92
93 test -t 0 && echo >&2 "Type in some urls and I'll try to shrink them for you..."
94 while read line ; do
95         convert_url "$line" || echo $line
96 done