Last active
December 11, 2015 07:48
-
-
Save robtuley/4568642 to your computer and use it in GitHub Desktop.
node snippet to create a local river data cache from Rainchasers API
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
| var rivers = []; | |
| function parseUrl(url){ | |
| var parsed = require('url').parse(url); | |
| return { | |
| host: parsed.host, | |
| path: parsed.pathname+(parsed.search||'') | |
| }; | |
| } | |
| function onRiverDataComplete(resumeUrl){ | |
| process.stdout.write(' retrieved '+rivers.length+ | |
| " rivers\nresume from "+resumeUrl+"\n"); | |
| } | |
| function getRiverData(url){ | |
| process.stdout.write('.'); | |
| require('http').get(parseUrl(url),function(response){ | |
| var json = ''; | |
| response.on('data',function(data){ | |
| json += data; | |
| }); | |
| response.on('end',function(){ | |
| var result = JSON.parse(json); | |
| rivers = rivers.concat(result.data); | |
| if(result.meta.link.next){ | |
| getRiverData(result.meta.link.next); | |
| }else{ | |
| onRiverDataComplete(result.meta.link.resume); | |
| } | |
| }); | |
| }); | |
| }; | |
| getRiverData('http://api.rainchasers.com/v1/river'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment