Ejemplo n.º 1
0
def download_landsat_scene(url, directory, filename):
    '''
    This method downloads a scene directly from usgs. In order to do so, it
    pretends to be a browser to build a request that is accepted by the server.
    We added the headers so we don't get banned when the server detects that we
    are doing lots of requests. This idea is based on the landsat downloader:
    https://github.com/olivierhagolle/LANDSAT-Download
    '''
    cookies = urllib2.HTTPCookieProcessor()
    opener = urllib2.build_opener(cookies)
    urllib2.install_opener(opener)
    data = urllib2.urlopen("https://ers.cr.usgs.gov").read()
    token_group = re.search(r'<input .*?name="csrf_token".*?value="(.*?)"',
                            data)
    if token_group:
        token = token_group.group(1)
    else:
        LOGGER.error('The cross site request forgery token was not found.')
        sys.exit(1)
    usgs = {
        'account': getattr(SETTINGS, 'USGS_USER'),
        'passwd': getattr(SETTINGS, 'USGS_PASSWORD')
    }
    params = urllib.urlencode(
        dict(username=usgs['account'],
             password=usgs['passwd'],
             csrf_token=token))
    request = urllib2.Request(
        "https://ers.cr.usgs.gov/login",
        params,
        headers={
            'User-Agent':
            'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
        })
    f = urllib2.urlopen(request)
    data = f.read()
    f.close()
    download_chunks(url, directory, filename)