#!/bin/bash
# Script to perform search requests on Creodias finder and automatically # download the results. # # @author: <sylvain.herledan@oceandatalab.com> # @date: 2021-05-21 # # # Usage: # # ./creodias_search_and_download.sh 'SEARCH_URL' # # where: # - SEARCH_URL is the URL containing all the search parameters # # The command will ask for a Creodias username and password (required for # downloading data from Creodias). # # Note that the max number of results will be automatically increased from 10 # (default on Creodias finder) to 500 (max supported value) so that it is not # required to edit this field manually when copying the URL from Creodias # finder web interface.
set -o nounset set -o errexit set -o pipefail
readonly PROGDIR="$(readlink -f "$(dirname "${0}")")"; readonly PROGNAME="$(basename "${0}")"; readonly ARGS="${@}";
main() { local username; local password; local search_url; local keycloak_token;
search_url="${1}"; shift; read -p "Creodias username: " -r username; read -p "Creodias password: " -r -s password; echo
curl -XGET --silent "$(echo "${search_url}" \ | sed 's/maxRecords=10/maxRecords=500/')" \ | python -mjson.tool \ | grep '"url": ' \ | sed -e 's/^.*"url": "//g' -e 's/",.*$//g' \ | while read -r download_url; do # Get a new token for each download (maybe seem unefficient but tokens # expire after some time, so using a single token for all downloads # will work for the first few downloads and then fail... keycloak_token="$(curl -d 'client_id=CLOUDFERRO_PUBLIC' \ -d "username=${username}" \ -d "password=${password}" \ -d 'grant_type=password' \ --silent \ 'https://auth.creodias.eu/auth/realms/DIAS/protocol/openid-connect/token' \ | python -m json.tool \ | grep "access_token" \ | awk -F\" '{print $4}')";
echo "Downloading ${download_url} ..." wget --quiet --content-disposition "${download_url}?token=${keycloak_token}" done }
main ${ARGS} exit 0 |