Created
September 23, 2013 19:38
-
-
Save mikerudolph/6675799 to your computer and use it in GitHub Desktop.
Simple way to pull images from and RSS feed and download them to local storage with Node.js
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
#!/usr/local/bin/node | |
/** | |
* Dependencies | |
*/ | |
var app = require('commander') | |
, FeedParser = require('feedparser') | |
, fs = require('fs') | |
, request = require('request') | |
, _ = require('lodash'); | |
app | |
.version('0.0.1') | |
.option('-d, --directory [path]', 'Target directory for downloading files into.', './imgs/') | |
.option('-s, --source [source]', 'Source Feed', 'http://feeds.nationalgeographic.com/ng/photography/photo-of-the-day/') | |
.parse(process.argv); | |
feedPull(app.source, function(err, items) { | |
if (err) throw err; | |
_.forEach(items, function(item) { | |
var fileName = item.title.replace(/\W/g, ''); | |
var filePath = app.directory + fileName + '.jpg'; | |
request(item.link).pipe(fs.createWriteStream(filePath)); | |
}); | |
}); | |
function feedPull(url, cb) { | |
var items = []; | |
request(url) | |
.on('error', function(err) { | |
return cb(err); | |
}) | |
.pipe(new FeedParser()) | |
.on('meta', function(meta) { | |
// | |
}) | |
.on('readable', function() { | |
var stream = this; | |
var item = stream.read(); | |
items.push({ title: item.title, link: item.enclosures[0].url }); | |
}) | |
.on('end', function() { | |
return cb(null, items); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment