Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feedparser and firebase - option to save dates as strings #180

Open
dsl101 opened this issue Oct 6, 2016 · 2 comments
Open

feedparser and firebase - option to save dates as strings #180

dsl101 opened this issue Oct 6, 2016 · 2 comments

Comments

@dsl101
Copy link

dsl101 commented Oct 6, 2016

I'm using firebase as a backend for storage, and it has the interesting feature that it can't store javascript Date objects. Whilst I'm in the process of discussing this with firebase (I think it probably should, but who knows what they'll decide), I made the following local changes to feedparser to provide an option to return any dates in the item and metadata as strings. Sorry I didn't make a branch, so can't do this as a pull request, but in case it helps anyone else.

New option:

dateAsString: Defaults to false. Returns dates as strings rather than Date() objects

main.js Diff:

@@ -107,6 +107,7 @@
   if (!('strict' in this.options)) this.options.strict = false;
   if (!('normalize' in this.options)) this.options.normalize = true;
   if (!('addmeta' in this.options)) this.options.addmeta = true;
+  if (!('dateAsString' in this.options)) this.options.dateAsString = false;
   if (!('resume_saxerror' in this.options)) this.options.resume_saxerror = true;
   if ('MAX_BUFFER_LENGTH' in this.options) {
     sax.MAX_BUFFER_LENGTH = this.options.MAX_BUFFER_LENGTH; // set to Infinity to have unlimited buffers
@@ -403,6 +404,7 @@

   var meta = {}
     , normalize = !options || (options && options.normalize)
+    , dateAsString = !options || (options && options.dateAsString)
     ;

   if (normalize) {
@@ -434,6 +436,7 @@
       case('dc:date'):
         var date = utils.get(el) ? new Date(utils.get(el)) : null;
         if (!date) break;
+        if (dateAsString) date = date.toString();
         if (meta.pubdate === null || name == 'pubdate' || name == 'published')
           meta.pubdate = meta.pubDate = date;
         if (meta.date === null || name == 'lastbuilddate' || name == 'modified' || name == 'updated')
@@ -738,6 +741,7 @@

   var item = {}
     , normalize = !options || (options && options.normalize)
+    , dateAsString = !options || (options && options.dateAsString)
     ;

   if (normalize) {
@@ -776,6 +780,7 @@
       case('dc:date'):
         var date = utils.get(el) ? new Date(utils.get(el)) : null;
         if (!date) break;
+        if (dateAsString) date = date.toString();
         if (item.pubdate === null || name == 'pubdate' || name == 'published' || name == 'issued')
           item.pubdate = item.pubDate = date;
         if (item.date === null || name == 'modified' || name == 'updated')
@danmactough
Copy link
Owner

I think an option to parse date fields as strings instead of dates is a useful enhancement. PR welcome!

I would prefer the name parseDate for the option (and it should default to true).

@danmactough
Copy link
Owner

I would prefer the name parseDate for the option (and it should default to true).

Both of these opinions of mine are terrible. Sorry, @dsl101.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants