1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
var fs = require('fs') var request = require('request') var htmlparser = require('htmlparser')
var configFilename = './rss_feeds.txt'
function checkForRSSFile() { fs.exists(configFilename, function(exists) { if (!exists) return next(new Error('Missing RSS file: ' + configFilename)) next(null, configFilename) }) }
function readRSSFile(configFilename) { fs.readFile(configFilename, function(err, feedList) { if (err) return next(err)
feedList = feedList .toString() .replace('/^s+|s+$/g', '') .split('\n') var random = Math.floor(Math.random() * feedList.length) next(null, feedList[random]) }) }
function downloadRSSFeed(feedUrl) { request({ uri: feedUrl }, function(err, res, body) { if (err) return next(err) if (res.statusCode != 200) { return next(new Error('Abnormal response status code')) } next(null, body) }) }
function parseRSSFeed(rss) { var handler = new htmlparser.RssHandler() var parser = new htmlparser.Parser(handler) parser.parseComplete(rss) if (!handler.dom.items.length) return next(new Error('No RSS items found')) var item = handler.dom.items.shift() console.log(item.title) console.log(item.link) }
tasks = [checkForRSSFile, readRSSFile, downloadRSSFeed, parseRSSFeed]
function next(err, result) { if (err) throw err
var currentTask = tasks.shift()
if (currentTask) { currentTask(result) } }
next()
|