Skip to content

Instantly share code, notes, and snippets.

@runeh
Created March 3, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runeh/9e7eafa7b6ec6c3591d8 to your computer and use it in GitHub Desktop.
Save runeh/9e7eafa7b6ec6c3591d8 to your computer and use it in GitHub Desktop.
const highland = require('highland');
const request = require('request-promise')
const promiseToStream = highland;
const arrayToStream = highland;
const pollerToStream = highland;
function cowPageGenerator(url) {
let page = 1;
return (push, next) => {
push(null, request(url, {qs: {page: page++}}));
next();
};
}
function cowStream(url) {
return pollerToStream(cowPageGenerator(url))
.flatMap(promiseToStream)
.map(JSON.parse)
.map(e => e.entries)
.flatMap(arrayToStream)
}
const url = "http://hotell.difi.no/api/json/mattilsynet/husdyrregisteret/storfeindivider";
cowStream(url)
.ratelimit(1, 1000);
.each(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment