From 9cb5d6eec8facd872b3fbe24e73d8d9a53c12769 Mon Sep 17 00:00:00 2001 From: Waldo Jaquith Date: Sun, 11 Mar 2018 21:16:38 -0400 Subject: [PATCH] Turn this into an API This satisfies most of the requirements of #1. It remains to run this on a cron job. --- .gitignore | 1 + api.sh => bin/api.sh | 2 ++ bin/csv.sh | 15 +++++++++++++++ bin/get_file.sh | 18 ++++++++++++++++++ bin/upload.sh | 10 ++++++++++ config.sh | 13 +++++++++++++ run.sh | 8 ++++---- 7 files changed, 63 insertions(+), 4 deletions(-) rename api.sh => bin/api.sh (94%) create mode 100755 bin/csv.sh create mode 100755 bin/get_file.sh create mode 100755 bin/upload.sh create mode 100755 config.sh diff --git a/.gitignore b/.gitignore index 8c6492c..3c14011 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ api/ +NationalFedCodes.txt diff --git a/api.sh b/bin/api.sh similarity index 94% rename from api.sh rename to bin/api.sh index 2617147..dfef044 100755 --- a/api.sh +++ b/bin/api.sh @@ -9,6 +9,8 @@ OUTPUT_DIR=./api # Create the JSON directory. mkdir -p "$OUTPUT_DIR" +echo "Creating JSON files" + # Iterate through each line in the CSV file. while read -r p; do FIPS_VALUE=$(echo "$p" | cut -d "," -f 1) diff --git a/bin/csv.sh b/bin/csv.sh new file mode 100755 index 0000000..d7449fa --- /dev/null +++ b/bin/csv.sh @@ -0,0 +1,15 @@ +#/bin/bash + +# Import variables +source config.sh + +echo "Creating CSV file" + +# Create the headers for the CSV file. +echo "GNIS_ID,FIPS_CODE" > "$CSV_FILE" + +# Skip the first line, pipe the 1st and 4th columns into the CSV file. +sed 1d "$SOURCE_FILE" | cut -d "|" -f 1,4 >> "$CSV_FILE" + +# Turn every pipe symbol into a comma. +sed -i -e "s/|/,/g" "$CSV_FILE" diff --git a/bin/get_file.sh b/bin/get_file.sh new file mode 100755 index 0000000..a87bf08 --- /dev/null +++ b/bin/get_file.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Get the current filename from the USGS download page. +FILENAME=$(curl -s https://geonames.usgs.gov/domestic/download_data.htm |egrep -o "\/docs\/federalcodes\/NationalFedCodes_[0-9]{8}.zip") + +# Put together the URL for the file +URL=https://geonames.usgs.gov"$FILENAME" + +echo Retreiving "$URL" + +# Get the file. +curl -s "$URL" -o NationalFedCodes.zip + +# Extract the file's contents. +unzip -p NationalFedCodes.zip > NationalFedCodes.txt + +# Get rid of the ZIP file. +rm NationalFedCodes.zip diff --git a/bin/upload.sh b/bin/upload.sh new file mode 100755 index 0000000..0758adb --- /dev/null +++ b/bin/upload.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Import variables +source config.sh + +echo "Syncing files with S3" + +# Sync all recent changes +cd "$OUTPUT_DIR" +aws s3 sync . s3://"$BUCKET" diff --git a/config.sh b/config.sh new file mode 100755 index 0000000..9b4b928 --- /dev/null +++ b/config.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# The file from the U.S. Board of Geographic Names' website. +SOURCE_FILE=NationalFedCodes.txt + +# The CSV file. +CSV_FILE=united-states.csv + +# The local directory to store the JSON files. +OUTPUT_DIR=./api + +# The S3 bucket where we want to host the JSON files. +BUCKET=fipsgnis.com diff --git a/run.sh b/run.sh index 7a4135d..443a30d 100755 --- a/run.sh +++ b/run.sh @@ -1,4 +1,4 @@ -#/bin/bash -echo "GNIS_ID,FIPS_CODE" > united-states.csv -sed 1d NationalFedCodes.txt | cut -d "|" -f 1,4 >> united-states.csv -sed -i -e "s/|/,/g" ./united-states.csv +#!/bin/bash + +# Run each step in turn. +bin/get_file.sh && bin/csv.sh && bin/api.sh && bin/upload.sh