Last active
July 9, 2020 20:34
-
-
Save davidmintz/f6abe90583a2cf8965426faecdf59dc0 to your computer and use it in GitHub Desktop.
bash script for downloading from the [redacted] internal website a PDF whose URL changes regularly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# download the current [redacted] judges' directory (PDF) from the internal website | |
# (even though they change the filename with each new update) | |
# where to put the PDF | |
dir="/opt/www/interpreters/public/files/judges"; | |
# our bin directory | |
bindir='/opt/www/interpreters/bin'; | |
# base url to look for file | |
base_url="http://intranet.nysd.circ2.dcn/" | |
# get into our bin directory if need be | |
if [ $PWD != $bindir ] ; then cd $bindir; fi; | |
# parse out the url to pdf file | |
path=$(curl -s $base_url | perl -n -e '/href="(.+directories\/Chambers\/[^"]+pdf)"/ && print $1,qq(\n)' |head -n 1) | |
url="${base_url}/${path}" | |
# our local filename | |
basename=$(basename "$url") | |
# minus the spaces! oh, how I hate spaces in file names. | |
filename=$(echo $basename | sed -e 's/ /_/g') | |
# download the file | |
wget -q "$url" -O $filename | |
if [ $? -ne 0 ]; then | |
echo "download failed with error code $?" | |
exit 1 | |
fi; | |
# do we already have this file? | |
if [ ! -e "${dir}/${filename}" ]; then | |
# does not exist, so move it | |
mv "${filename}" "${dir}/"; | |
# ...and symlink it to a sensible name | |
ln -s --force "${dir}/${filename}" ${dir}/directory.pdf | |
else | |
# if we do have this file, has it changed since last time? | |
cmp -s "${filename}" "${dir}/${filename}"; | |
if [ $? -ne 0 ]; then | |
# change detected, move $filename; | |
mv "${filename}" "${dir}/${filename}"; | |
else | |
# nothing to do; delete downloaded file | |
rm "${filename}"; | |
fi; | |
fi; | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment