Last active
August 29, 2015 13:59
-
-
Save Sharpie/10608882 to your computer and use it in GitHub Desktop.
A Script to Download CSV Data from EPA ECHO
This file contains hidden or 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
| #!/usr/bin/env casperjs | |
| 'use strict'; | |
| // The sledgehammer approach: Use a JavaScript testing framework to simulate | |
| // web browsing. Slow as all hell, but gets the job done. | |
| // | |
| // Caveat Utilitor: This was hacked together on a musky Portland afternoon | |
| // under the influence of several cans of excellent Oakshire "Watershed" IPA. | |
| // | |
| // Installation | |
| // ============ | |
| // | |
| // Ensure script is executable: | |
| // | |
| // chmod +x download_epa.js | |
| // | |
| // On OS X: | |
| // | |
| // brew install casperjs --devel | |
| // | |
| // `--devel` is required because of the PhantomJS version Homebrew ships. This | |
| // will probably work on older versions of CasperJS. | |
| // | |
| // | |
| // Usage: | |
| // ====== | |
| // | |
| // ./download_epa.js [some] [permit] [ids] | xargs wget -O output.csv | |
| // | |
| // Released under the terms of WTFPL: http://www.wtfpl.net/about/ | |
| // Load up the CasperJS module and instantiate a driver. | |
| var casper = require('casper').create(); | |
| // Pass desired station IDs on the command line. | |
| if ( casper.cli.args.length == 0 ) { | |
| casper.die('You must pass at least one Permit ID to this script!'); | |
| } | |
| var permitList = casper.cli.args | |
| casper.start('http://echo.epa.gov/facility_search_results?permit=' + permitList.join(), function() { | |
| var queryID = this.evaluate(function() { | |
| return document.getElementById('QueryID').value; | |
| }); | |
| var trackColumns = this.evaluate(function() { | |
| return document.getElementById('trackColumns').value; | |
| }); | |
| // Can't figure out downloading at the moment, so just echo the CSV url to | |
| // stdout. It can then be piped to something like `xargs wget`. | |
| this.echo('https://ofmpub.epa.gov/echo/rest_services.get_download?qid=' + queryID + '&qcolumns=' + trackColumns); | |
| }); | |
| casper.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment