Skip to content

Instantly share code, notes, and snippets.

@andkirby
Last active August 29, 2019 14:44
Show Gist options
  • Save andkirby/c31238acefa1250d7be914841931aa2c to your computer and use it in GitHub Desktop.
Save andkirby/c31238acefa1250d7be914841931aa2c to your computer and use it in GitHub Desktop.
Download all Amazon MWS XSD files recursively.
#!/usr/bin/env bash
#############################################################################
# Download Amazon MWS XSD file with all included ones recursively
# It will download XSD files into the same directory of this script
#############################################################################
# USAGE
# $ bash fetch-all-xsd.sh URL_START_XSD_FILE
#
# EXAMPLE
# $ bash fetch-all-xsd.sh https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_4_1/amzn-envelope.xsd
#
# The value from the example is a default value for the first argument
# and can be omitted
# $ bash fetch-all-xsd.sh
#
# NOTE
# This script considers only XSD files from the same location,
# it does work with filename only.
#############################################################################
# Implemented in Bash version:
# GNU bash, version 4.4.19(3)-release (x86_64-pc-msys)
#############################################################################
set -o pipefail
set -o errexit
set -o nounset
#set -o xtrace
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
readonly __dir __file
input_url=${1:-https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_4_1/amzn-envelope.xsd}
echo "Considering base XSD file URL:"
echo " ${input_url}"
url_directory=$(dirname ${input_url})
found_included=0
recursive_download() {
local xsd_file=${1} \
url_file=${url_directory}/${1}
if [[ ! -f ${__dir}/${xsd_file} ]]; then
printf "Fetching ${xsd_file}..."
curl -Ls --output ${__dir}/${xsd_file} ${url_directory}/${xsd_file}
echo "OK"
fi
# Try to exclude all downloaded XSD files and current one
local include_xsd_list=$(\
cat ${__dir}/${xsd_file} \
| grep -Eo '(\w|-|_)+\.xsd' \
| grep -v "($(cd ${__dir}; find . -maxdepth 1 -name '*.xsd' | sed 's:./::' \
| tr "\n " '|' | sed 's:|$::g')|${xsd_file})")
local include_xsd
for include_xsd in ${include_xsd_list}; do
if [[ "${xsd_file}" != "${include_xsd}" ]]; then
recursive_download ${include_xsd}
fi
done
}
recursive_download $(basename ${input_url})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment