Skip to content

Commit

Permalink
Turn this into an API
Browse files Browse the repository at this point in the history
This satisfies most of the requirements of #1. It remains to run this
on a cron job.
  • Loading branch information
waldoj committed Mar 12, 2018
1 parent e92b06d commit 9cb5d6e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
api/
NationalFedCodes.txt
2 changes: 2 additions & 0 deletions api.sh → bin/api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions bin/csv.sh
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 18 additions & 0 deletions bin/get_file.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions bin/upload.sh
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9cb5d6e

Please sign in to comment.