Created
March 30, 2018 18:23
-
-
Save scripting/85d9853e04255666482fc30e0da6117c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
exports.parseString = parseFeedString; | |
var myProductName = "daveReadFeed"; myVersion = "0.4.0"; | |
const request = require ("request"); | |
const feedParser = require ("feedparser"); | |
const utils = require ("daveutils"); | |
const stream = require ("stream"); | |
const Iconv = require ("iconv").Iconv; | |
const metaNames = { | |
title: true, | |
link: true, | |
description: true, | |
pubDate: true, | |
language: true, | |
copyright: true, | |
generator: true, | |
cloud: true, | |
image: true, | |
categories: true | |
}; | |
function parseFeedString (theString, charset, callback) { | |
var feedparser = new feedParser (); | |
var theFeed = { | |
head: new Object (), | |
items: new Array () | |
}; | |
var theStream = new stream.Readable; | |
theStream.push (theString); | |
theStream.push (null); | |
feedparser.on ("readable", function () { | |
try { | |
var item = this.read (); | |
if (item !== null) { | |
theFeed.items.push (item); | |
for (var x in item.meta) { | |
if (metaNames [x] !== undefined) { | |
theFeed.head [x] = item.meta [x]; | |
} | |
} | |
} | |
} | |
catch (err) { | |
console.log ("parseFeedString: err.message == " + err.message); | |
} | |
}); | |
feedparser.on ("error", function (err) { | |
console.log ("parseFeedString: err.message == " + err.message); | |
callback (err); | |
}); | |
feedparser.on ("end", function () { | |
callback (undefined, theFeed); | |
}); | |
if (charset !== undefined) { | |
var iconv = new Iconv (charset, "UTF-8"); | |
theStream = theStream.pipe (iconv); | |
} | |
theStream.pipe (feedparser); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment