Created
May 26, 2013 21:41
-
-
Save elliotttf/5654099 to your computer and use it in GitHub Desktop.
Helper function to pull data from flickr for 4k node.js training.
This file contains 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 request = require('superagent'); | |
var Stream = require('stream'); | |
/** | |
* Get Images function | |
* | |
*/ | |
module.exports = function nodes (url, fn) { | |
var data = ''; | |
// Set up a stream to pipe data to. | |
// @see https://github.com/substack/stream-handbook#writable | |
var stream = new Stream(); | |
stream.writable = true; | |
/** | |
* The stream's write behavior. | |
*/ | |
stream.write = function(buf) { | |
if (buf && typeof buf.toString !== 'undefined') { | |
data += buf.toString(); | |
} | |
}; | |
/** | |
* The stream's end behavior. | |
*/ | |
stream.end = function(buf) { | |
stream.write(buf); | |
stream.writable = false; | |
fn(null, data); | |
}; | |
/** | |
* The stream's destroy behavior. | |
*/ | |
stream.destroy = function() { | |
stream.writable = false; | |
}; | |
// Request the URL. The accept header was lifted from a Google | |
// Chrome request. The user agent is custom. | |
var req = request | |
.get(url) | |
.set('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8') | |
.set('User-Agent', 'node.js training ❤ Four Kitchens'); | |
// Pipe the result to our stream. | |
req.pipe(stream); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment