feedr
Advanced tools
Comparing version 2.6.0 to 2.7.0
@@ -98,3 +98,3 @@ // Generated by CoffeeScript 1.6.3 | ||
Feedr.prototype.readFeed = function(feedDetails, next) { | ||
var cleanData, feedr, parseFile, requestOptions, useCache, viaCache, viaRequest, writeFeed, xml2jsOptions; | ||
var cleanData, extname, feedr, parseFile, readFile, requestOptions, useCache, viaCache, viaRequest, writeFeed, xml2jsOptions, _ref; | ||
feedr = this; | ||
@@ -139,2 +139,12 @@ if (!feedr.config.tmpPath) { | ||
useCache = feedDetails.cache; | ||
if (feedDetails.parse == null) { | ||
feedDetails.parse = true; | ||
} | ||
extname = pathUtil.extname(feedDetails.url.replace(/[?#].*/, '')); | ||
if (feedDetails.parse === true) { | ||
feedDetails.parse = extname === '.xml' || extname === '.atom' || extname === '.rss' || extname === '.rdf' || extname === '.html' || extname === '.html' ? 'xml' : extname === '.json' || extname === '.jsonp' || extname === '.js' ? 'json' : extname === '.yml' || extname === '.yaml' ? 'yaml' : false; | ||
} | ||
if (feedDetails.parse && ((_ref = feedDetails.parse) !== 'xml' && _ref !== 'json' && _ref !== 'yaml')) { | ||
return next(new Error("unrecognised parse value: " + feedDetails.parse)); | ||
} | ||
requestOptions = extendr.deepExtend({ | ||
@@ -170,4 +180,4 @@ url: feedDetails.url, | ||
}; | ||
parseFile = function(path, next) { | ||
feedr.log('debug', "Feedr is parsing [" + feedDetails.url + "] on [" + path + "]"); | ||
readFile = function(path, next) { | ||
feedr.log('debug', "Feedr is reading [" + feedDetails.url + "] on [" + path + "]"); | ||
return safefs.exists(path, function(exists) { | ||
@@ -177,12 +187,21 @@ if (!exists) { | ||
} | ||
return safefs.readFile(path, function(err, dataBuffer) { | ||
var data; | ||
return safefs.readFile(path, function(err, rawData) { | ||
if (err) { | ||
return next(err, null); | ||
} | ||
data = JSON.parse(dataBuffer.toString()); | ||
return next(null, data); | ||
return next(null, rawData); | ||
}); | ||
}); | ||
}; | ||
parseFile = function(path, next) { | ||
feedr.log('debug', "Feedr is parsing [" + feedDetails.url + "] on [" + path + "]"); | ||
return readFile(path, function(err, rawData) { | ||
var data; | ||
if (err || !rawData) { | ||
return next(err, null); | ||
} | ||
data = JSON.parse(rawData.toString()); | ||
return next(null, data); | ||
}); | ||
}; | ||
writeFeed = function(response, data, next) { | ||
@@ -200,3 +219,9 @@ var writeTasks; | ||
writeTasks.addTask(function(complete) { | ||
return safefs.writeFile(feedDetails.path, JSON.stringify(data), complete); | ||
var rawData; | ||
if (feedDetails.parse) { | ||
rawData = JSON.stringify(data); | ||
} else { | ||
rawData = data; | ||
} | ||
return safefs.writeFile(feedDetails.path, rawData, complete); | ||
}); | ||
@@ -225,7 +250,11 @@ return writeTasks.run(); | ||
readTasks.addTask(function(complete) { | ||
return parseFile(feedDetails.path, function(err, result) { | ||
return readFile(feedDetails.path, function(err, rawData) { | ||
if (err) { | ||
return complete(err); | ||
} | ||
data = result; | ||
if (feedDetails.parse) { | ||
data = JSON.parse(rawData.toString()); | ||
} else { | ||
data = rawData; | ||
} | ||
return complete(); | ||
@@ -237,5 +266,5 @@ }); | ||
viaRequest = function(next) { | ||
var _base, _ref; | ||
var _base, _ref1; | ||
feedr.log('debug', "Feedr is fetching [" + feedDetails.url + "] to [" + feedDetails.path + "]"); | ||
if (useCache && ((_ref = feedDetails.metaData) != null ? _ref.etag : void 0)) { | ||
if (useCache && ((_ref1 = feedDetails.metaData) != null ? _ref1.etag : void 0)) { | ||
if (requestOptions.headers == null) { | ||
@@ -249,3 +278,3 @@ requestOptions.headers = {}; | ||
return request(requestOptions, function(err, response, data) { | ||
var body, handleError, handleSuccess, parser, xml2js; | ||
var handleError, handleSuccess, parser, xml2js; | ||
handleError = function(err) { | ||
@@ -271,29 +300,45 @@ if (useCache) { | ||
} | ||
body = data.toString().trim(); | ||
if (/^</.test(body)) { | ||
xml2js = require('xml2js'); | ||
parser = new xml2js.Parser(xml2jsOptions); | ||
parser.on('end', function(data) { | ||
return handleSuccess(data); | ||
}); | ||
try { | ||
return parser.parseString(body); | ||
} catch (_error) { | ||
err = _error; | ||
if (err) { | ||
return handleError(err); | ||
switch (feedDetails.parse) { | ||
case 'xml': | ||
xml2js = require('xml2js'); | ||
parser = new xml2js.Parser(xml2jsOptions); | ||
parser.on('end', function(data) { | ||
return handleSuccess(data); | ||
}); | ||
try { | ||
return parser.parseString(data.toString().trim()); | ||
} catch (_error) { | ||
err = _error; | ||
if (err) { | ||
return handleError(err); | ||
} | ||
} | ||
} | ||
} else { | ||
body = body.replace(/(^([\s\;]|\/\*\*\/)+|[\s\;]+$)/g, ''); | ||
try { | ||
body = body.replace(/^[a-z0-9]+/gi, '').replace(/^\(|\)$/g, ''); | ||
data = JSON.parse(body); | ||
} catch (_error) { | ||
err = _error; | ||
break; | ||
case 'json': | ||
data = data.toString().trim().replace(/(^([\s\;]|\/\*\*\/)+|[\s\;]+$)/g, ''); | ||
data = data.replace(/^[a-z0-9]+/gi, '').replace(/^\(|\)$/g, ''); | ||
try { | ||
body = body.replace(/\\'/g, "'"); | ||
data = JSON.parse(body); | ||
data = JSON.parse(data); | ||
} catch (_error) { | ||
err = _error; | ||
data = data.replace(/\\'/g, "'"); | ||
try { | ||
data = JSON.parse(data); | ||
} catch (_error) { | ||
err = _error; | ||
if (err) { | ||
return handleError(err); | ||
} | ||
} | ||
} | ||
if (feedDetails.clean) { | ||
feedr.log('debug', "Feedr is cleaning data from [" + feedDetails.url + "]"); | ||
data = cleanData(data); | ||
} | ||
return handleSuccess(data); | ||
case 'yaml': | ||
try { | ||
data = require('yamljs').parse(data.toString().trim()); | ||
} catch (_error) { | ||
err = _error; | ||
if (err) { | ||
@@ -303,8 +348,5 @@ return handleError(err); | ||
} | ||
} | ||
if (feedDetails.clean) { | ||
feedr.log('debug', "Feedr is cleaning data from [" + feedDetails.url + "]"); | ||
data = cleanData(data); | ||
} | ||
return handleSuccess(data); | ||
return handleSuccess(data); | ||
default: | ||
return handleSuccess(data); | ||
} | ||
@@ -311,0 +353,0 @@ }); |
{ | ||
"name": "feedr", | ||
"version": "2.6.0", | ||
"version": "2.7.0", | ||
"description": "Feedr takes in a remote feed (regardless of format type) and converts it into JSON data", | ||
@@ -40,3 +40,4 @@ "homepage": "http://bevry.me/project/feedr", | ||
"xml2js": "~0.2.4", | ||
"request": "~2.27.0" | ||
"request": "~2.27.0", | ||
"yamljs": "~0.1.4" | ||
}, | ||
@@ -43,0 +44,0 @@ "devDependencies": { |
@@ -66,2 +66,3 @@ # Feedr | ||
- `path` defaults to tmp feed path, the path to save the file to | ||
- `parse` defaults to auto detection based on the extension of the url, whether or not to parse the data into a javascript object, can be set to `false`, `true`, `"xml"`, `"json"`, `"yaml"` | ||
- `checkResponse` defaults to `null`, a function accepting `response`, `data`, and `next` to check the response for errors | ||
@@ -68,0 +69,0 @@ - `xml2jsOptions` defaults to global value, the options to send to [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) |
Sorry, the diff of this file is not supported yet
360
79
16024
9
4
+ Addedyamljs@~0.1.4
+ Addedargparse@0.1.16(transitive)
+ Addedglob@3.1.21(transitive)
+ Addedgraceful-fs@1.2.3(transitive)
+ Addedinherits@1.0.2(transitive)
+ Addedlru-cache@2.7.3(transitive)
+ Addedminimatch@0.2.14(transitive)
+ Addedsigmund@1.0.1(transitive)
+ Addedunderscore@1.7.0(transitive)
+ Addedunderscore.string@2.4.0(transitive)
+ Addedyamljs@0.1.6(transitive)