twitter/util.py
def follow_redirects(link, sites= None):
    """Follow directs for the link as long as the redirects are on the given
    sites and return the resolved link."""
    def follow(url):
        return sites == None or urlparse.urlparse(url).hostname in sites

    class RedirectHandler(urllib2.HTTPRedirectHandler):
        def __init__(self):
            self.last_url = None
        def redirect_request(self, req, fp, code, msg, hdrs, newurl):
            self.last_url = newurl
            if not follow(newurl):
                return None
            r = urllib2.HTTPRedirectHandler.redirect_request(
                self, req, fp, code, msg, hdrs, newurl)
            r.get_method = lambda : 'HEAD'
            return r

    if not follow(link):
        return link
    redirect_handler = RedirectHandler()
    opener = urllib2.build_opener(redirect_handler)
    req = urllib2.Request(link)
    req.get_method = lambda : 'HEAD'
    try:
        with contextlib.closing(opener.open(req,timeout=1)) as site:
            return site.url
    except:
        return redirect_handler.last_url if redirect_handler.last_url else link
Follow directs for the link as long as the redirects are on the given
    sites and return the resolved link.
Follow directs for the link as long as the redirects are on the given
sites and return the resolved link.
Follow directs for the link as long as the redirects are on the given sites and return the resolved link .
def follow_redirects(link, sites= None):
    def follow(url):
        return sites == None or urlparse.urlparse(url).hostname in sites
    class RedirectHandler(urllib2.HTTPRedirectHandler):
        def __init__(self):
            self.last_url = None
        def redirect_request(self, req, fp, code, msg, hdrs, newurl):
            self.last_url = newurl
            if not follow(newurl):
                return None
            r = urllib2.HTTPRedirectHandler.redirect_request(
                self, req, fp, code, msg, hdrs, newurl)
            r.get_method = lambda : 'HEAD'
            return r
    if not follow(link):
        return link
    redirect_handler = RedirectHandler()
    opener = urllib2.build_opener(redirect_handler)
    req = urllib2.Request(link)
    req.get_method = lambda : 'HEAD'
    try:
        with contextlib.closing(opener.open(req,timeout=1)) as site:
            return site.url
    except:
        return redirect_handler.last_url if redirect_handler.last_url else link
def follow_redirects ( link , sites = None ) : def follow ( url ) : return sites == None or urlparse . urlparse ( url ) . hostname in sites class RedirectHandler ( urllib2 . HTTPRedirectHandler ) : def __init__ ( self ) : self . last_url = None def redirect_request ( self , req , fp , code , msg , hdrs , newurl ) : self . last_url = newurl if not follow ( newurl ) : return None r = urllib2 . HTTPRedirectHandler . redirect_request ( self , req , fp , code , msg , hdrs , newurl ) r . get_method = lambda : 'HEAD' return r if not follow ( link ) : return link redirect_handler = RedirectHandler ( ) opener = urllib2 . build_opener ( redirect_handler ) req = urllib2 . Request ( link ) req . get_method = lambda : 'HEAD' try : with contextlib . closing ( opener . open ( req , timeout = 1 ) ) as site : return site . url except : return redirect_handler . last_url if redirect_handler . last_url else link
Follow directs for the link as long as the redirects are on the given
sites and return the resolved link.
Follow directs for the link as long as the redirects are on the given sites and return the resolved link .
"""Follow directs for the link as long as the redirects are on the given
    sites and return the resolved link."""
link
sites