Beispiel #1
0
def _get_tokens(body):
    """Get `csrf_token` and `__ncforminfo`."""
    csrf = re.findall(r'name="csrf_token" value="(.+?)"', body)
    ncform = re.findall(r'name="__ncforminfo" value="(.+?)"', body)

    if not csrf:
        raise EarthExplorerError('EE: login failed (csrf token not found).')
    if not ncform:
        raise EarthExplorerError('EE: login failed (ncforminfo not found).')

    return csrf, ncform
 def login(self, username, password):
     """Get an API key."""
     data = to_json(username=username, password=password, catalogID='EE')
     response = requests.post(self.endpoint + 'login?', data=data).json()
     if response['error']:
         raise EarthExplorerError('EE: {}'.format(response['error']))
     return response['data']
 def _download(self, url, output_dir, timeout, chunk_size=1024):
     """Download remote file given its URL."""
     try:
         with self.session.get(url,
                               stream=True,
                               allow_redirects=True,
                               timeout=timeout) as r:
             file_size = int(r.headers.get("Content-Length"))
             with tqdm(total=file_size,
                       unit_scale=True,
                       unit='B',
                       unit_divisor=1024) as pbar:
                 local_filename = r.headers['Content-Disposition'].split(
                     '=')[-1]
                 local_filename = local_filename.replace("\"", "")
                 local_filename = os.path.join(output_dir, local_filename)
                 with open(local_filename, 'wb') as f:
                     for chunk in r.iter_content(chunk_size=chunk_size):
                         if chunk:
                             f.write(chunk)
                             pbar.update(chunk_size)
     except requests.exceptions.Timeout:
         raise EarthExplorerError(
             'Connection timeout after {} seconds.'.format(timeout))
     return local_filename
 def request(self, request_code, **kwargs):
     """Perform a request to the EE API.
     Possible request codes are listed here:
     https://earthexplorer.usgs.gov/inventory/documentation/json-api
     """
     url = self.endpoint + request_code
     if 'apiKey' not in kwargs:
         kwargs.update(apiKey=self.key)
     params = to_json(**kwargs)
     response = requests.get(url, params=params).json()
     if response['error']:
         raise EarthExplorerError('EE: {}'.format(response['error']))
     else:
         return response['data']
Beispiel #5
0
    def login(self, username, password):
        """Login to Earth Explorer."""
        rsp = self.session.get(EE_LOGIN_URL)
        csrf, ncform = _get_tokens(rsp.text)
        payload = {
            'username': username,
            'password': password,
            'csrf_token': csrf,
            '__ncforminfo': ncform
        }
        rsp = self.session.post(EE_LOGIN_URL,
                                data=payload,
                                allow_redirects=False)

        if not self.logged_in():
            raise EarthExplorerError('EE: login failed.')
Beispiel #6
0
 def request(self, request_code, kwargs):
     """Perform a request to the EE API.
     Possible request codes are listed here:
     https://earthexplorer.usgs.gov/inventory/documentation/json-api
     """
     url = self.endpoint + request_code
     # if 'apiKey' not in kwargs:
     #     kwargs.update(apiKey=self.key)
     params = json.dumps(kwargs)
     headers = {'X-Auth-Token': self.key}
     response = requests.post(url, params, headers=headers).text
     response = json.loads(response)
     if response['errorMessage']:
         raise EarthExplorerError('EE: {}'.format(response['error']))
     else:
         output = response
     return output['data']