#!/bin/sh ##################### # # name: turl (turl.sh) # vers: 0.5a # date: 051806 # first: 040806 # auth: "Keith Beckman" # site: http://alphahelical.com/code/scripts/turl # desc: turl reads a fully-qualified url from stdin or as an argument, # and queries the tinyurl server, returning a tinyurl. It also exports # input and output urls in $turl_input and $turl_output. # # todo: test mode verifying proper turl-ing # ##################### ####configuration#### verstring='turl v0.5a 051806 Keith Beckman' default_agent='curl' ua_string='turl(http://alphahelical.com/code?turl)/0.4a' ##end configuration## turl_usage () { echo "Usage: turl -[a user_agent|h|v] | [url] turl (a tinyurl.com client) reads a URL and makes it tiny. URL may be given via stdin or at the command-line. No validity- checking of URLs is performed. Default user-agent may be edited in the configuration portion of this script. -a|--agent [curl|lynx|links|wget] Select user-agent (default: curl) -v|--version displays the version string -h|--help displays this help text Note: If you don't have any of the supported user-agents, 'telnet' is available as an alternative, and may be selected or set as default." } turl_telnet_ua () { length=$(echo ${#1}) expect <<-EOF set timeout 20 spawn telnet tinyurl.com 80 expect "Escape character is '^]'." # send "POST /create.php HTTP/1.0\nHost: tinyurl.com\nUser-Agent: $ua_string-telnet\nContent-Type: application/x-www-form-urlencoded\nContent-length: $length\n\n$1\n\n" send "GET /create.php?$1 HTTP/1.0\nHost: tinyurl.com\nUser-Agent: $ua_string-telnet\n\n" expect "type=\"hidden\"\ name=\"tinyurl\"" EOF } case $1 in '--usage'|'--help'|'-h') #display helptext turl_usage exit ;; '--version'|'-v') #version string echo $verstring exit ;; '-a'|'--agent') #select user-agent if [[ $2 == 'curl' || $2 == 'lynx' || \ $2 == 'wget' || $2 == 'links' || $2 == 'telnet' ]]; then user_agent=$2 fi ;; esac if [ $3 ]; then big_url="$3" elif [ $1 ]; then big_url="$1" # Read url from command line if present. else big_url="$(cat)" # Otherwise, grab from stdin fi if [ "$user_agent" == '' ]; then user_agent=$default_agent fi turl_fetch () { case $1 in 'curl') # curl -sA $ua_string-cURL http://tinyurl.com/create.php --data url="$big_url" curl -sA $ua_string-cURL http://tinyurl.com/create.php?url="$big_url" ;; 'lynx') # echo "url=$big_url" | lynx --source --useragent=$ua_string-Lynx http://tinyurl.com/create.php \ #-post_data lynx --source --useragent=$ua_string-Lynx http://tinyurl.com/create.php?url="$big_url" ;; 'wget') # wget -qO /dev/stdout -U $ua_string-wget http://tinyurl.com/create.php \ #--post-data="url\=$big_url" wget -qO /dev/stdout -U $ua_string-wget http://tinyurl.com/create.php?url="$big_url" ;; 'links') links -source http://tinyurl.com/create.php?url="$big_url" ;; 'telnet') turl_telnet_ua "url=$big_url" ;; esac } turl_fetch $user_agent | grep type=hidden\ name=tinyurl | cut -d \" -f 2