Skip to content

Instantly share code, notes, and snippets.

@tsmsogn
Last active December 17, 2018 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsmsogn/4c6e2a32360a87844f266ebbbc822570 to your computer and use it in GitHub Desktop.
Save tsmsogn/4c6e2a32360a87844f266ebbbc822570 to your computer and use it in GitHub Desktop.
Fetch and cache the data
#!/bin/bash
set -e
cd $(dirname $0)
base=$(pwd)
url='' # INPUT URL
tmp_file=`date +%s`_shop.json # INPUT TEMPORARY FILE NAME
dest='shop.json' # INPUT OUTPUT FILE NAME
assert_same() {
if [ "$1" = "$2" ]; then
return 0;
fi
return 1
}
fetch() {
curl_out=`curl -s -k -w "%{http_code}" -o $tmp_file $url`
http_code=`echo "$curl_out" | cut -f1`
return 0
}
main() {
debug=false
args=""
local argc=0
local argv=()
while [ $# -gt 0 ]; do
case $1 in
--debug)
debug=true
;;
esac
shift
done
fetch
if $debug; then
echo "url: $url"
echo "tmp_file: $tmp_file"
echo "dest: $dest"
echo "http_code: $http_code"
fi
assert_same 200 $http_code && cp $tmp_file $dest && rm $tmp_file && exit 0
exit 1
}
main $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment