Created
July 30, 2012 03:51
-
-
Save heldr/3204209 to your computer and use it in GitHub Desktop.
file utils
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('request'), | |
fs = require('fs'); | |
module.exports = { | |
getRemoteContent: function( url, cb, browserUserAgent){ | |
var requestConfig = { | |
uri : url | |
} | |
if ( browserUserAgent ) { | |
requestConfig.headers = { | |
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11' | |
} | |
} | |
request( requestConfig, function (err, res, content) { | |
if ( err ) throw err; | |
if ( res.statusCode == 200 ) { | |
cb( res, content); | |
} else { | |
throw "Error " + res.statusCode; | |
} | |
}); | |
}, | |
getRemoteFile: function( url, output, cb ) { | |
this.getRemoteContent( url, function( res, content ) { | |
fs.writeFile( output, function(err) { | |
if ( err ) throw err; | |
cb({ | |
status: res.statusCode, | |
output: output | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment