#!/usr/bin/env python

import urllib2
import sys
import socket
import re

socket.setdefaulttimeout(5)

class NoRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        infourl = urllib2.addinfourl(fp, headers, req.get_full_url())
        infourl.status = code
        infourl.code = code
        return infourl
    http_error_300 = http_error_302
    http_error_301 = http_error_302
    http_error_303 = http_error_302
    http_error_307 = http_error_302

opener = urllib2.build_opener(NoRedirectHandler())
urllib2.install_opener(opener)

class HeadRequest(urllib2.Request):
    def get_method(self):
        return "HEAD"

def follow(url):
    try:
        response = urllib2.urlopen(HeadRequest(url)).info()
        if 'Location' in response:
            return follow(response['Location'])
    except:
        pass
    return url

if len(sys.argv) > 1 and '-h' in sys.argv[1]:
    print "Expand a tinyurl"
    sys.exit(0)

def _untiny(match):
    return follow(match.group())

def untiny(string):
    return re.sub('http://[^ ]+', _untiny, string)

if len(sys.argv) > 1:
    print untiny(' '.join(sys.argv[1:]))
else:
    for line in sys.stdin:
        print untiny(line.strip())
