|
|

楼主 |
发表于 2009-5-25 18:05:06
|
显示全部楼层
- #!/bin/bash
- #帐号和apiKey(注册后在‘account’里看)
- my_login=
- my_key=
- # my_URL="$1"
- # set default behaviour
- format=text
- action=shorten
- action_aim=longUrl
- history=0
- help() {
- echo
- echo "Script for bit.ly, a Url shorten/restore service."
- echo
- echo "USAGE:: `basename "$0"` [options] [urls]"
- echo "OPTIONs::"
- echo '-a {s|e|i|ss} :: choose action: shorten,expand,info,stats'
- echo '-l (1|0) :: set to 1 if you want it to be logged in your account history'
- echo '-f {t|x|j} :: choose output format: text ONLY,xml,json'
- echo '-h :: of course you know what it means'
- echo
- }
- OPT_TEMP=$(getopt --longoptions action:,log:,format:,help \
- --name "bit.ly_API" --options a:l:f:h \
- -- "$@")
- eval set -- "$OPT_TEMP"
- while : ;do
- case "$1" in
- -a|--action)
- case "$2" in
- s|shorten) action=shorten && action_aim=longUrl ; shift 2 ;;
- e|expand) action=expand && action_aim=shortUrl ; shift 2 ;; # hash
- i|info) action=info && action_aim=shortUrl ; shift 2 ;; # hash keys
- ss|stats) action=stats && action_aim=shortUrl ; shift 2 ;; # hash
- *) echo "** Wrong action, using $action" ; shift 2 ;;
- esac
- ;;
- -l|--log)
- case "$2" in
- 1|log) history=1 ; shift 2 ;;
- 0|nolog) history=0 ; shift 2 ;;
- *) echo "** Wrong log option, using $history" ; shift 2 ;;
- esac
- ;;
- -f|--format)
- case "$2" in
- t|text) format=text ; shift 2 ;;
- x|xml) format=xml ; shift 2 ;;
- j|json) format=json ; shift 2 ;;
- *) echo "** Wrong format, using $format" ; shift 2 ;;
- esac
- ;;
- -h|--help)
- help ; exit 0
- ;;
- --)
- shift ; break
- ;;
- esac
- done
- for arg; do
- echo "+ $action +-> $arg ::"
- # Long URLs should be URL-escaped
- arg=$( echo "$arg" | perl -MURI::Escape -lne 'print uri_escape($_)' )
- curl -s http://api.bit.ly/"$action"\?version\=2.0.1\&"$action_aim"\="$arg"\&login\="$my_login"\&apiKey\="$my_key"\&format\="$format"\&history\="$history"
- done
复制代码 |
|