Bash loop cURL+ wget API extraction
Script to automate a set of sequential downloads from bounded list of Ids.
Use cURL to get the JSON data from the API and then extract the relevant fields to create a nice CSV download and finally remove the unnecessary quote characters in the output.
Linux commands used
#!/bin/bash
timestamp() {
date +%Y-%m-%dT%H:%M:%S%z
}
printLine() {
printf "$(timestamp): $1\n"
}
while getopts s:u:k:e: option
do
case "${option}"
in
s) start=${OPTARG};;
u) api_url=${OPTARG};;
k) key=${OPTARG};;
e) end=${OPTARG};;
esac
done
if test -z "$start"
then
printLine "No start id specified"
exit 1
fi
if test -z "$end"
then
printLine "No end id specified"
exit 2
fi
if test -z "$api_url"
then
printLine "No API url specified"
api_url="<DEFAULT_API_URL>"
fi
printLine "API url to use $api_url"
if test -z "$key"
then
printLine "No API key specified"
exit 4
fi
if (( start > end ));
then
printLine "$start id > $end id"
exit 3
fi
while (( start <= end )); do
name=`curl $api_url?Id=$start\&api_key=$key\&format=json | jq '.queries | .[] | .notes + "_" + (.latitude|tostring) + "," + (.longitude|tostring) + ".csv"' | tr -d '"'`
echo $name
get=`wget -O $name $api_url/$start?api_key=$key`
echo "$get"
start=$[start+1]
done