Permalink
Browse files

added getImage

  • Loading branch information...
Andris Reinman
Andris Reinman committed May 26, 2012
1 parent f5753ea commit 2333a50cac021350e200819242b4197f6d1f02a5
Showing with 66 additions and 26 deletions.
  1. +57 −23 lib/nodepie.js
  2. +6 −2 package.json
  3. +3 −1 test/test.js
View
@@ -1,37 +1,35 @@
var xmlparser = require('xml2json'),
+ fetch = require('fetch'),
Iconv = require("iconv").Iconv;
module.exports = NodePie;
/************* FEED *************/
/**
- * new NodePie(xml [, options])
- * - xml (String | Buffer): Feed XML data
- * - options (Object): options object
+ * <p>Generates a NodePie object for parsing RSS/Atom feed data</p>
*
- * Generates a NodePie object for parsing RSS/Atom feed data
+ * <p>Usage:</p>
*
- * Usage:
+ * <pre>
+ * var np = new NodePie(feed);
+ * np.init();
+ * console.log(np.getTitle());
*
- * var np = new NodePie(feed);
- * np.init();
- * console.log(np.getTitle());
+ * np.getItems().forEach(function(item){
+ * console.log(item.getTitle());
+ * });
+ * </pre>
*
- * np.getItems().forEach(function(item){
- * console.log(item.getTitle());
- * });
- **/
+ * @constructor
+ * @param {String|Buffer} [xml] Feed XML data
+ * @param {Object} [options] options object
+ */
function NodePie(xml, options){
this.encoding = "UTF-8";
this.options = options || {};
-
- if(xml instanceof Buffer){
- this._checkEncoding(xml);
- }else{
- this.xml = (xml && xml.toString("utf-8") || "").trim();
- }
+ this.xml = xml || "";
this.feed = undefined;
this.rootElement = undefined;
@@ -57,16 +55,19 @@ NodePie.NS = {
WFW: 'http://wellformedweb.org/CommentAPI/',
DC: 'http://purl.org/dc/elements/1.1/',
CONTENT: 'http://purl.org/rss/1.0/modules/content/',
- ATOM10: 'http://www.w3.org/2005/Atom'
+ ATOM10: 'http://www.w3.org/2005/Atom',
+ GD: 'http://schemas.google.com/g/2005'
};
/**
* NodePie.HTMLEntities -> Object
*
+ * Does not convert &gt; and &lt;
+ *
* Hash map to convert HTML entities into unicode symbols
**/
NodePie.HTMLEntities = {
- apos:0x0027,quot:0x0022,amp:0x0026,lt:0x003C,gt:0x003E,nbsp:0x00A0,iexcl:0x00A1,cent:0x00A2,pound:0x00A3,
+ apos:0x0027,quot:0x0022,amp:0x0026,nbsp:0x00A0,iexcl:0x00A1,cent:0x00A2,pound:0x00A3,
curren:0x00A4,yen:0x00A5,brvbar:0x00A6,sect:0x00A7,uml:0x00A8,copy:0x00A9,ordf:0x00AA,laquo:0x00AB,
not:0x00AC,shy:0x00AD,reg:0x00AE,macr:0x00AF,deg:0x00B0,plusmn:0x00B1,sup2:0x00B2,sup3:0x00B3,
acute:0x00B4,micro:0x00B5,para:0x00B6,middot:0x00B7,cedil:0x00B8,sup1:0x00B9,ordm:0x00BA,raquo:0x00BB,
@@ -93,7 +94,7 @@ NodePie.HTMLEntities = {
larr:0x2190,uarr:0x2191,rarr:0x2192,darr:0x2193,harr:0x2194,crarr:0x21B5,lArr:0x21D0,uArr:0x21D1,
rArr:0x21D2,dArr:0x21D3,hArr:0x21D4,forall:0x2200,part:0x2202,exist:0x2203,empty:0x2205,nabla:0x2207,
isin:0x2208,notin:0x2209,ni:0x220B,prod:0x220F,sum:0x2211,minus:0x2212,lowast:0x2217,radic:0x221A,
- prop:0x221D,infin:0x221E,ang:0x2220,and:0x2227,or:0x2228,cap:0x2229,cup:0x222A,int:0x222B,
+ prop:0x221D,infin:0x221E,ang:0x2220,and:0x2227,or:0x2228,cap:0x2229,cup:0x222A,"int":0x222B,
there4:0x2234,sim:0x223C,cong:0x2245,asymp:0x2248,ne:0x2260,equiv:0x2261,le:0x2264,ge:0x2265,
sub:0x2282,sup:0x2283,nsub:0x2284,sube:0x2286,supe:0x2287,oplus:0x2295,otimes:0x2297,perp:0x22A5,
sdot:0x22C5,lceil:0x2308,rceil:0x2309,lfloor:0x230A,rfloor:0x230B,lang:0x2329,rang:0x232A,loz:0x25CA,
@@ -108,7 +109,11 @@ NodePie.HTMLEntities = {
**/
NodePie._decodeHTMLEntities = function(text){
return text.replace(/&(.+?);/g, function(str, ent){
- return String.fromCharCode( ent[0] !== '#' ? NodePie.HTMLEntities[ent] : ent[1] === 'x' ? parseInt(ent.substr(2),16) : parseInt(ent.substr(1), 10) );
+ if(ent[0] !== '#'){
+ return NodePie.HTMLEntities[ent] || str;
+ }else{
+ return ent[1] === 'x' ? parseInt(ent.substr(2),16) : parseInt(ent.substr(1), 10);
+ }
}
);
};
@@ -185,7 +190,7 @@ NodePie.prototype._checkEncoding = function(xml){
xml = iconv.convert(xml);
}
- this.xml = xml.toString("utf-8").trim();
+ return xml.toString("utf-8").trim();
};
/**
@@ -308,6 +313,12 @@ NodePie.prototype._processDate = function(date){
**/
NodePie.prototype.init = function(){
+ if(this.xml instanceof Buffer){
+ this.xml = this._checkEncoding(this.xml);
+ }else{
+ this.xml = (this.xml || "").toString("utf-8").trim();
+ }
+
this._applyXMLPatches();
this.feed = xmlparser.toJson(this.xml, {object: true});
@@ -396,6 +407,29 @@ NodePie.prototype.getHub = function(){
return this.getLink("hub");
};
+
+/**
+ * NodePie#getImage() -> String | False
+ *
+ * Fetches the logo url of the feed
+ *
+ * Usage:
+ *
+ * var np = new NodePie(feed);
+ * np.init();
+ * logo = np.getImage();
+ **/
+NodePie.prototype.getImage = function(){
+ var gdns = this.namespaces[NodePie.NS.GD];
+ return this._parseContents(
+ (this.channelElement.image && this.channelElement.image.url) ||
+ (this.channelElement.author && this.channelElement.author[gdns+":image"] &&
+ this.channelElement.author[gdns+":image"].src) ||
+ this.channelElement.logo ||
+ this.channelElement.icon
+ );
+};
+
/**
* NodePie#getLink([rel="alternate"[, type="text/html"]]) -> String | False
* - rel (String): link rel
View
@@ -1,7 +1,7 @@
{
"name": "nodepie",
"description": "RSS/Atom parser for Node.JS",
- "version": "0.4.0",
+ "version": "0.5.0",
"author" : "Andris Reinman",
"maintainers":[
{
@@ -21,9 +21,13 @@
"url": "http://github.com/andris9/nodepie/blob/master/LICENSE"
}
],
+ "devDependencies": {
+ "nodeunit": "*"
+ },
"dependencies": {
"xml2json":"*",
- "iconv": "*"
+ "iconv": "*",
+ "fetch": "*"
},
"engine": [ "node >=0.3.0" ],
"keywords": ["rss", "feed", "atom"]
View
@@ -2,7 +2,7 @@ var http = require("http"),
urllib = require("url"),
NodePie = require("../lib/nodepie");
-var feed_url = "http://techcrunch.com/feed/";
+var feed_url = "http://feeds.feedburner.com/blogspot/MKuf";
request(feed_url, function(error, xml){
if(error){
@@ -19,6 +19,7 @@ request(feed_url, function(error, xml){
console.log(np.getPermalink());
console.log("Source encoding: " + np.getEncoding());
console.log("Updated", np.getDate());
+ console.log("Image", np.getImage());
for(var i=0, len = np.getItemQuantity(3); i<len; i++){
item = np.getItem(i);
@@ -29,6 +30,7 @@ request(feed_url, function(error, xml){
console.log(item.getPermalink());
console.log("Created", item.getDate(), "\n");
console.log(item.getDescription());
+
}
});

0 comments on commit 2333a50

Please sign in to comment.