Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Showing with 130 additions and 3 deletions.
  1. +3 −0 README.md
  2. +29 −2 main.js
  3. +2 −1 package.json
  4. +75 −0 test/feeds/rss2sample-odddatetime.xml
  5. +21 −0 test/parseString-simple-options.js
View
3 README.md
@@ -76,6 +76,9 @@ about what that `request` options object might look like.
- `addmeta` - Set to `false` to override Feedparser's default behavior, which
is to add the feed's `meta` information to each `article`.
+- `dateformat` - Optionally set a specific format for parsing of the dates (e.g.
+ for different locales). The format is [moment style](http://momentjs.com/docs/#/parsing/string-format/).
+
- `feedurl` - The url (string) of the feed. FeedParser is very good at
resolving relative urls in feeds. But some feeds use relative urls without
declaring the `xml:base` attribute any place in the feed. This is perfectly
View
31 main.js
@@ -19,6 +19,7 @@ var sax = require('sax')
, EventEmitter = require('events').EventEmitter
, STATUS_CODES = require('http').STATUS_CODES
, utils = require('./utils')
+ , moment = require('moment')
;
/**
@@ -145,6 +146,7 @@ FeedParser.prototype.parseString = function(string, options, callback) {
if (options) {
if ('normalize' in options) this.options.normalize = options.normalize;
if ('addmeta' in options) this.options.addmeta = options.addmeta;
+ if ('dateformat' in options) this.options.dateformat = options.dateformat;
if (options.feedurl) this.xmlbase.unshift({ '#name': 'xml', '#': options.feedurl});
}
this._setCallback(callback);
@@ -175,6 +177,7 @@ FeedParser.prototype.parseFile = function(file, options, callback) {
if (options) {
if ('normalize' in options) this.options.normalize = options.normalize;
if ('addmeta' in options) this.options.addmeta = options.addmeta;
+ if ('dateformat' in options) this.options.dateformat = options.dateformat;
if (options.feedurl) this.xmlbase.unshift({ '#name': 'xml', '#': options.feedurl});
}
this._setCallback(callback);
@@ -205,6 +208,7 @@ FeedParser.prototype.parseUrl = function(url, options, callback) {
if (options) {
if ('normalize' in options) this.options.normalize = options.normalize;
if ('addmeta' in options) this.options.addmeta = options.addmeta;
+ if ('dateformat' in options) this.options.dateformat = options.dateformat;
}
if (!this.xmlbase.length) { // #parseFile may have already populated this value
if (/^https?:/.test(url)) {
@@ -244,6 +248,7 @@ FeedParser.prototype.parseStream = function(stream, options, callback) {
if (options) {
if ('normalize' in options) this.options.normalize = options.normalize;
if ('addmeta' in options) this.options.addmeta = options.addmeta;
+ if ('dateformat' in options) this.options.dateformat = options.dateformat;
if (options.feedurl) this.xmlbase.unshift({ '#name': 'xml', '#': options.feedurl});
}
this._setCallback(callback);
@@ -555,8 +560,19 @@ FeedParser.prototype.handleMeta = function handleMeta (node, type, options) {
var meta = {}
, normalize = !options || (options && options.normalize)
+ , dateparser
;
+ if (options && options.dateformat) {
+ dateparser = function(datestr) {
+ return moment(datestr, options.dateformat).toDate();
+ };
+ } else {
+ dateparser = function(datestr) {
+ return moment(datestr).toDate();
+ };
+ }
+
if (normalize) {
['title','description','date', 'pubdate', 'pubDate','link', 'xmlurl', 'xmlUrl','author','language','favicon','copyright','generator'].forEach(function (property){
meta[property] = null;
@@ -583,7 +599,7 @@ FeedParser.prototype.handleMeta = function handleMeta (node, type, options) {
case('modified'):
case('updated'):
case('dc:date'):
- var date = utils.get(el) ? new Date(el['#']) : null;
+ var date = utils.get(el) ? dateparser(el['#']) : null;
if (!date) break;
if (meta.pubdate === null || name == 'pubdate' || name == 'published')
meta.pubdate = meta.pubDate = date;
@@ -804,8 +820,19 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){
var item = {}
, normalize = !options || (options && options.normalize)
+ , dateparser
;
+ if (options && options.dateformat) {
+ dateparser = function(datestr) {
+ return moment(datestr, options.dateformat).toDate();
+ };
+ } else {
+ dateparser = function(datestr) {
+ return moment(datestr).toDate();
+ };
+ }
+
if (normalize) {
['title','description','summary','date','pubdate','pubDate','link','guid','author','comments', 'origlink'].forEach(function (property){
item[property] = null;
@@ -839,7 +866,7 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){
case('modified'):
case('updated'):
case('dc:date'):
- var date = utils.get(el) ? new Date(el['#']) : null;
+ var date = utils.get(el) ? dateparser(el['#']) : null;
if (!date) break;
if (item.pubdate === null || name == 'pubdate' || name == 'published' || name == 'issued')
item.pubdate = item.pubDate = date;
View
3 package.json
@@ -27,7 +27,8 @@
"sax": "0.5.x",
"request": "2.9.x",
"addressparser": "~0.1.3",
- "array-indexofobject": "0.0.1"
+ "array-indexofobject": "0.0.1",
+ "moment": "2.x"
},
"devDependencies": {
"mocha": "1.x",
View
75 test/feeds/rss2sample-odddatetime.xml
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="../../templates/rssformatting2.xsl"?>
+<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
+<channel>
+<atom:link href="http://www.esinet.norfolk.gov.uk/cadmin/ecourier/newsnorfolkschools2.xml" rel="self" type="application/rss+xml" />
+<title>Norfolk Schools - News and MI Sheets</title>
+<link>http://www.schools.norfolk.gov.uk</link>
+<description>The MI sheets released to governors.</description>
+<language>en</language>
+<copyright>Copyright 2013, Norfolk County Council</copyright>
+<ttl>60</ttl>
+<lastBuildDate>09/02/2013</lastBuildDate>
+<image>
+<url>http://www.esinet.norfolk.gov.uk/images/esinetrss.gif</url>
+<title>Norfolk Schools - Governor MI Sheets</title>
+<link>http://www.schools.norfolk.gov.uk</link>
+</image>
+
+
+<item>
+<title>Inspection of Private Fostering Service</title>
+<link>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19176</link>
+<guid>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19176</guid>
+
+<description><![CDATA[<table width="100%" border="0"><tr><td valign="top" width="50"><img src="http://schools.norfolk.gov.uk/myportal/custom/files_uploaded/livery_library/Action.gif"></td><td valign="middle"><a href='https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19176'>Inspection of Private Fostering Service</a></td></tr></table>]]></description>
+<pubDate>08/02/2013 12:00:00</pubDate>
+<category>news</category>
+</item>
+
+
+<item>
+<title>Services to Schools contract 2013-14</title>
+<link>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19170</link>
+<guid>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19170</guid>
+
+<description><![CDATA[<table width="100%" border="0"><tr><td valign="top" width="50"><img src="http://schools.norfolk.gov.uk/myportal/custom/files_uploaded/livery_library/Information.gif"></td><td valign="middle"><a href='https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19170'>Services to Schools contract 2013-14</a></td></tr></table>]]></description>
+<pubDate>08/02/2013 12:00:00</pubDate>
+<category>news</category>
+</item>
+
+
+<item>
+<title>Statutory training for EYFS schools and settings</title>
+<link>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19167</link>
+<guid>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19167</guid>
+
+<description><![CDATA[<table width="100%" border="0"><tr><td valign="top" width="50"><img src="http://schools.norfolk.gov.uk/myportal/custom/files_uploaded/livery_library/Information.gif"></td><td valign="middle"><a href='https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19167'>Statutory training for EYFS schools and settings</a></td></tr></table>]]></description>
+<pubDate>08/02/2013 12:00:00</pubDate>
+<category>news</category>
+</item>
+
+
+<item>
+<title>2013-14 Budget Planner Software</title>
+<link>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19165</link>
+<guid>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19165</guid>
+
+<description><![CDATA[<table width="100%" border="0"><tr><td valign="top" width="50"><img src="http://schools.norfolk.gov.uk/myportal/custom/files_uploaded/livery_library/Action.gif"></td><td valign="middle"><a href='https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19165'>2013-14 Budget Planner Software</a></td></tr></table>]]></description>
+<pubDate>08/02/2013 12:00:00</pubDate>
+<category>news</category>
+</item>
+
+
+<item>
+<title>Teachers Pensions - Employee Contributions</title>
+<link>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19149</link>
+<guid>https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19149</guid>
+
+<description><![CDATA[<table width="100%" border="0"><tr><td valign="top" width="50"><img src="http://schools.norfolk.gov.uk/myportal/custom/files_uploaded/livery_library/Flyer.gif"></td><td valign="middle"><a href='https://csapps.norfolk.gov.uk/csshared/ecourier2/misheet.asp?misheetid=19149'>Teachers Pensions - Employee Contributions</a></td></tr></table>]]></description>
+<pubDate>08/02/2013 12:00:00</pubDate>
+<category>news</category>
+</item>
+
+</channel>
+</rss>
View
21 test/parseString-simple-options.js
@@ -42,5 +42,26 @@ describe('feedparser', function(){
});
});
});
+ describe('parseString with custom date format', function() {
+ before(function(done){
+ var str = require('fs').readFileSync(__dirname + '/feeds/rss2sample-odddatetime.xml');
+ FeedParser.parseString(str, {dateformat: 'DD/MM/YYYY HH:mm:ss'}, function (err, _meta, _articles){
+ assert.ifError(err);
+ meta = _meta;
+ articles = _articles;
+ done();
+ });
+ });
+ describe('meta', function(){
+ it('should have a last build date of feb 9th 2013', function() {
+ assert.equal(new Date('Feb 9 2013').getTime(), meta.date.getTime());
+ });
+ });
+ describe('article 1', function(){
+ it('should have a pubdate time of feb 8 2013, 12:00', function(){
+ assert.equal(new Date('Feb 8 2013, 12:00:00').getTime(), articles[0].pubdate.getTime());
+ });
+ });
+ });
});

No commit comments for this range

Something went wrong with that request. Please try again.