import requests import json # from pprint import pprint from tqdm import tqdm import re import sys
def get_keycloak_token(username, password): h = { 'Content-Type': 'application/x-www-form-urlencoded' } d = { 'client_id': 'CLOUDFERRO_PUBLIC', 'password': password, 'username': username, 'grant_type': 'password' } resp = requests.post('https://auth.creodias.eu/auth/realms/dias/protocol/openid-connect/token', data=d, headers=h) # print(resp.status_code) try: token = json.loads(resp.content.decode('utf-8'))['access_token'] except KeyError: print("Can't obtain a token (check username/password), exiting.") sys.exit() # print(token) return token
finder_api_url = '''https://finder.creodias.eu/resto/api/collections/Sentinel1/search.json?maxRecords=5&startDate=2018-09-01T00:00:00Z&completionDate=2019-04-01T23:59:59Z&productType=GRD&sensorMode=IW&sortParam=startDate&sortOrder=descending&status=all&geometry=POLYGON((25.471802+58.031372000000005,25.471802+58.81658600000003,26.872559000000003+58.81658600000003,26.872559000000003+58.031372000000005,25.471802+58.031372000000005))&dataset=ESA-DATASET''' username = 'creodias_username_here' password = 'creodias_password_here'
response = requests.get(finder_api_url) for feature in json.loads(response.content.decode('utf-8'))['features']: token = get_keycloak_token(username, password) download_url = feature['properties']['services']['download']['url'] download_url = download_url + '?token=' + token total_size = feature['properties']['services']['download']['size'] title = feature['properties']['title'] filename = title + '.zip' r = requests.get(download_url, stream=True) if "Content-Disposition" in r.headers.keys(): filename = re.findall("filename=(.+)", r.headers["Content-Disposition"])[0] # Total size in bytes. total_size = int(r.headers.get('content-length', 0)) if total_size <= 100: print(r.text) sys.exit("Please try again in few moments.") block_size = 1024 #1 Kibibyte print('downloading:', filename) t=tqdm(total=total_size, unit='iB', unit_scale=True) with open(filename, 'wb') as f: for data in r.iter_content(block_size): t.update(len(data)) f.write(data) t.close() if total_size != 0 and t.n != total_size: print("ERROR, something went wrong") |