Add argument parsing and quiet mode

This commit is contained in:
Lucidiot 2020-01-03 15:21:22 +01:00
parent 050143d2ab
commit 531e45bc98
Signed by: lucidiot
GPG Key ID: 3358C1CA6906FB8D
1 changed files with 32 additions and 14 deletions

View File

@ -1,28 +1,46 @@
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo Building to $DIR/feeds
function usage {
echo "Usage: $0 [-d BASE_DIR] [-q]"
echo " -d, --base-dir Set the base directory for the feed generator files and output."
echo " -q, --quiet Hide status logs - useful when running the script as a cronjob."
exit 1
}
while [[ "$#" > 0 ]]; do case $1 in
-d|--base-dir) DIR="$2"; shift;;
-q|--quiet) QUIET=yes;;
*) echo "Unknown parameter: $1"; usage;;
esac; shift; done
function log {
test -z "$QUIET" && echo "$@"
}
[ -z "$DIR" ] && DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
log Building to $DIR/feeds
mkdir -p $DIR/feeds
if ! command -v json2xml >/dev/null 2>&1; then
echo "Adding $DIR/bin to PATH"
log "Adding $DIR/bin to PATH"
PATH="$DIR/bin:$PATH"
fi
echo Building ATSB feed to $DIR/feeds/atsb.xml
log Building ATSB feed to $DIR/feeds/atsb.xml
curl -s 'https://www.atsb.gov.au/publications/safety-investigation-reports/?s=1&sort=OccurrenceReleaseDate&sortAscending=descending&investigationStatus=Completed,Discontinued&printAll=true' \
| pup 'table.selectable_grid tr:not(.header) json{}' \
| jq -f $DIR/jq/atsb.jq \
| json2xml > $DIR/feeds/atsb.xml.new \
&& mv $DIR/feeds/atsb.xml.new $DIR/feeds/atsb.xml
echo Building TAIC feed to $DIR/feeds/taic.xml
log Building TAIC feed to $DIR/feeds/taic.xml
curl -s 'https://www.taic.org.nz/inquiries?order=field_publication_date&sort=desc' \
| pup '#view-table-wrapper tbody tr json{}' \
| jq -f $DIR/jq/taic.jq \
| json2xml > $DIR/feeds/taic.xml.new \
&& mv $DIR/feeds/taic.xml.new $DIR/feeds/taic.xml
echo Building JTSB Aviation English feed to $DIR/feeds/jtsb/en/air.xml
log Building JTSB Aviation English feed to $DIR/feeds/jtsb/en/air.xml
mkdir -p $DIR/feeds/jtsb/en
curl -s 'http://www.mlit.go.jp/jtsb/airrep.html' \
| pup 'table.kankokuiken-en tr:not(:first-child) json{}' \
@ -30,21 +48,21 @@ curl -s 'http://www.mlit.go.jp/jtsb/airrep.html' \
| json2xml > $DIR/feeds/jtsb/en/air.xml.new \
&& mv $DIR/feeds/jtsb/en/air.xml.new $DIR/feeds/jtsb/en/air.xml
echo Building JTSB Rail English feed to $DIR/feeds/jtsb/en/rail.xml
log Building JTSB Rail English feed to $DIR/feeds/jtsb/en/rail.xml
curl -s 'http://www.mlit.go.jp/jtsb/railrep.html' \
| pup 'table.kankokuiken-en tr:not(:first-child) json{}' \
| jq -f $DIR/jq/jtsb/en/rail.jq \
| json2xml > $DIR/feeds/jtsb/en/rail.xml.new \
&& mv $DIR/feeds/jtsb/en/rail.xml.new $DIR/feeds/jtsb/en/rail.xml
echo Building JTSB Marine English feed to $DIR/feeds/jtsb/en/marine.xml
log Building JTSB Marine English feed to $DIR/feeds/jtsb/en/marine.xml
curl -s 'http://www.mlit.go.jp/jtsb/marrep.html' \
| pup 'table.kankokuiken-en tr:not(:first-child) json{}' \
| jq -f $DIR/jq/jtsb/en/marine.jq \
| json2xml > $DIR/feeds/jtsb/en/marine.xml.new \
&& mv $DIR/feeds/jtsb/en/marine.xml.new $DIR/feeds/jtsb/en/marine.xml
echo Building JTSB Aviation Japanese feed to $DIR/feeds/jtsb/jp/air.xml
log Building JTSB Aviation Japanese feed to $DIR/feeds/jtsb/jp/air.xml
mkdir -p $DIR/feeds/jtsb/jp
curl -s 'https://jtsb.mlit.go.jp/jtsb/aircraft/air-kensaku-list.php?sort=desc&by=p' \
| pup 'table.jiko-information tr:not(:first-child) json{}' \
@ -52,21 +70,21 @@ curl -s 'https://jtsb.mlit.go.jp/jtsb/aircraft/air-kensaku-list.php?sort=desc&by
| json2xml > $DIR/feeds/jtsb/jp/air.xml.new \
&& mv $DIR/feeds/jtsb/jp/air.xml.new $DIR/feeds/jtsb/jp/air.xml
echo Building JTSB Rail Japanese feed to $DIR/feeds/jtsb/jp/rail.xml
log Building JTSB Rail Japanese feed to $DIR/feeds/jtsb/jp/rail.xml
curl -s 'https://jtsb.mlit.go.jp/jtsb/railway/rail-kensaku-list.php?sort=desc&by=p' \
| pup 'table.jiko-information tr:not(:first-child) json{}' \
| jq -f $DIR/jq/jtsb/jp/rail.jq \
| json2xml > $DIR/feeds/jtsb/jp/rail.xml.new \
&& mv $DIR/feeds/jtsb/jp/rail.xml.new $DIR/feeds/jtsb/jp/rail.xml
echo Building JTSB Marine Japanese feed to $DIR/feeds/jtsb/jp/marine.xml
log Building JTSB Marine Japanese feed to $DIR/feeds/jtsb/jp/marine.xml
curl -s 'https://jtsb.mlit.go.jp/jtsb/ship/ship-kensaku-list.php?sort=desc&by=p' \
| pup 'table.jiko-information tr:not(:first-child) json{}' \
| jq -f $DIR/jq/jtsb/jp/marine.jq \
| json2xml > $DIR/feeds/jtsb/jp/marine.xml.new \
&& mv $DIR/feeds/jtsb/jp/marine.xml.new $DIR/feeds/jtsb/jp/marine.xml
echo Building AIBD Aviation feed to $DIR/feeds/aibd/aviation.xml
log Building AIBD Aviation feed to $DIR/feeds/aibd/aviation.xml
mkdir -p $DIR/feeds/aibd
curl -s 'https://en.havarikommissionen.dk/aviation-archive/' \
| pup 'ul.items > li[data-url] json{}' \
@ -77,7 +95,7 @@ curl -s 'https://en.havarikommissionen.dk/aviation-archive/' \
| json2xml -s -ns 'http://search.yahoo.com/mrss/' media > $DIR/feeds/aibd/aviation.xml.new \
&& mv $DIR/feeds/aibd/aviation.xml.new $DIR/feeds/aibd/aviation.xml
echo Building AIBD Rail feed to $DIR/feeds/aibd/rail.xml
log Building AIBD Rail feed to $DIR/feeds/aibd/rail.xml
curl -s 'https://en.havarikommissionen.dk/railway-archive/' \
| pup 'ul.items > li[data-url] json{}' \
| jq -f $DIR/jq/denmark.jq \
@ -87,7 +105,7 @@ curl -s 'https://en.havarikommissionen.dk/railway-archive/' \
| json2xml -s -ns 'http://search.yahoo.com/mrss/' media > $DIR/feeds/aibd/rail.xml.new \
&& mv $DIR/feeds/aibd/rail.xml.new $DIR/feeds/aibd/rail.xml
echo Building DMAIB feed to $DIR/feeds/dmaib.xml
log Building DMAIB feed to $DIR/feeds/dmaib.xml
curl -s 'https://dmaib.com/reports/?categorizations=9510' \
| pup 'ul.items > li[data-url] json{}' \
| jq -f $DIR/jq/denmark.jq \