def get_photo_stream(config, photo):
    """Starts downloading the given photo."""
    cookie = config.get('webshots.cookie')
    if not cookie:
        cookie = get_cookie(
                config.get('webshots.username'),
                config.get('webshots.password'))
        config.set('webshots.cookie', cookie)
        config.save_config()

    headers = {'Cookie':
        'daily='+config.get('webshots.cookie')+
        ';desktop-client=unknown;site-visits=1',
    }

    url = photo['data']['high_res_link'].replace(
        'res=high',
        'res=%s' % config.get('webshots.quality'))

    opener = urllib.FancyURLopener()
    opener.addheader('Cookie', headers['Cookie'])
    conn = opener.open(url)
    content_type = conn.info().getheader('content-type')
    if content_type == 'application/x-webshots-package':
        return conn
    elif content_type.startswith('text/html'):
        resp = conn.read()
        if 'Credit Card Information' in resp:
            raise LeechPremiumOnlyPhotoError(
                "This photo can be downloaded at resolution '%s' only by "
                "Premium members." % config.get('webshots.quality'))
        if 'NO, THANK YOU.' in resp:
            match = re.search(
                r'<a href="(.*?)" class="no-btn">NO, THANK YOU.', resp)
            if not match:
                raise ValueError, "Unable to download photo %s" % photo['name']
            url = match.groups()[0]
            req = urllib2.Request(url, '', headers)
            resp = urllib2.urlopen(req).read()
            match = re.search(
                r'<a href="(.*\.wbz)">', resp)
        else:
            match = re.search(r'<a href="http://(.*?)">here</a>', resp)

        if not match:
            raise ValueError, "Unable to download photo %s" % photo['name']
        url = match.groups()[0]
        if not url.startswith('http://p.webshots.net/'):
            raise ValueError, "Unable to download photo %s" % photo['name']
        req = urllib2.Request(url, '', headers)
        resp = urllib2.urlopen(req)
        return resp
    else:
        raise ValueError("Unexpected content type header: '%s'" % content_type)
dataset/ETHPy150Open thesamet/webilder/src/webilder/webshots/utils.py/get_photo_stream/original