Skip to content

Instantly share code, notes, and snippets.

@musha68k
Forked from Syrup-tan/0 Himawari Scraper
Last active August 29, 2015 14:24
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 musha68k/fcd7da3a378ed8723538 to your computer and use it in GitHub Desktop.
Save musha68k/fcd7da3a378ed8723538 to your computer and use it in GitHub Desktop.
Example: https://denpa.moe/~syrup/himawari8.png
#!/bin/sh
## This script downloads an image of earth from Japan's Himawari8 satellite.
## It is configurable for 1,100^2, 2,200^2, or 8,800^2 pixel outputs.
## It requires;
## montage(1) from imagemagick (http://www.imagemagick.org/)
## date(1) from either BSD or GNU utils
## curl(1) from http://curl.haxx.se/
## jq(1) from https://stedolan.github.io/jq/
## Configuration
OUTPUT_DIRECTORY="./"
OUTPUT_FILENAME="himawari8.png";
URL_BASE='http://himawari8.nict.go.jp/img/D531106';
## The amount of tiles on one side
## This value should be either 1, 2, 4, or 16
## Each tile is 550px x 550px, so 4 (default) will produce an image 2,200px x 2,200px
TILE_COUNT='4';
DOWNLOAD_LOCK=false;
## Check requirements
if ! jq --version >/dev/null 2>&1; then
echo "jq(1) is not installed, but is required by this script.";
echo "Please see installation instructions at https://stedolan.github.io/jq/";
exit 1;
fi;
if ! curl --version >/dev/null 2>&1; then
echo "curl(1) is not installed, but is required by this script.";
echo "Please see installation instructions at http://curl.haxx.se/";
exit 1;
fi;
if ! montage --version >/dev/null 2>&1; then
echo "montage(1) from ImageMagick is not installed, but is required by this script.";
echo "Please see installation instructions at http://www.imagemagick.org/";
exit 1;
fi;
if ! date >/dev/null 2>&1; then
echo "date(1) is not installed, but is required by this script.";
echo "Please get a copy for your OS from either GNU or BSD utils.";
exit 1;
fi;
get_picture () {
## Set download lock
DOWNLOAD_LOCK=true;
## Get the latest picture
LATEST_FILE="$(curl -s "${URL_BASE}/latest.json" | jq -r -e .date)";
if [ "$?" -ne 0 ]; then
echo "Unable to get latest picture date";
exit 1;
fi;
## Parse the date into the filename
DATE_FORMAT='+%Y/%m/%d/%H%M%S';
### Check if we're using GNU or BSD
if date -j >/dev/null 2>&1; then
## Get the date for date(1) from BSD utils
IMAGE_URL="$(date -jf '%Y-%m-%d %H:%M:%S' "${LATEST_FILE}" "${DATE_FORMAT}")";
else
## Get the date for date(1) from GNU utils
IMAGE_URL="$(date -d "${LATEST_FILE}" "${DATE_FORMAT}")";
fi;
## Make the directory for the images
TMP_IMAGE_DIR="$(mktemp -d /tmp/himawari8.XXXXXXXX)";
## Download each of the files
echo "Downloading tiles...";
X=0;
while [ "${X}" -lt "${TILE_COUNT}" ]; do
Y=0;
while [ "${Y}" -lt "${TILE_COUNT}" ]; do
curl -so "${TMP_IMAGE_DIR}/${Y}_${X}.png" "${URL_BASE}/${TILE_COUNT}d/550/${IMAGE_URL}_${X}_${Y}.png";
printf "\rDownloaded ${X}, ${Y} ($((X * TILE_COUNT + Y + 1)) / $((TILE_COUNT * TILE_COUNT)))";
printf ' %0.s' {0..9};
Y="$((Y+1))";
done;
X="$((X+1))";
done;
## Beautiful code
printf "\rTiles downloaded."; printf ' %0.s' {0..9}; printf '\n\n';
## Create the image
echo "Creating image...";
montage "${TMP_IMAGE_DIR}/"*.png -geometry 550x550 "${OUTPUT_DIRECTORY}/${OUTPUT_FILENAME}" >/dev/null 2>&1;
echo "Image created: ${OUTPUT_DIRECTORY}/${OUTPUT_FILENAME} (${IMAGE_URL})";
## Remove the temporary directory
if [ -n "${TMP_IMAGE_DIR}" ]; then
rm "${TMP_IMAGE_DIR}/"*.png;
rmdir "${TMP_IMAGE_DIR}";
fi;
## Unset download lock
DOWNLOAD_LOCK=false;
}
download_is_ok () {
if ${DOWNLOAD_LOCK}; then
return 1;
fi
if [ $(networksetup -getcurrentlocation) = "Home" ]; then
return 0;
else
return 1;
fi
}
while true; do
if download_is_ok; then
get_picture;
fi
sleep 1200;
done
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment