mirror of
https://github.com/LightAir/turbo-rss.git
synced 2026-02-04 03:56:19 +00:00
added support for enclosure
Author: Victor Jonsson <kontakt@victorjonsson.se>
This commit is contained in:
33
lib/rss.js
33
lib/rss.js
@@ -2,7 +2,9 @@
|
||||
Documentation coming soon.
|
||||
*/
|
||||
|
||||
var XML = require('xml');
|
||||
var XML = require('xml'),
|
||||
mime = require('mime'),
|
||||
fs = require('fs');
|
||||
|
||||
function RSS (options, items) {
|
||||
options = options || {};
|
||||
@@ -24,7 +26,8 @@ function RSS (options, items) {
|
||||
guid: options.guid,
|
||||
categories: options.categories || [],
|
||||
author: options.author,
|
||||
date: options.date
|
||||
date: options.date,
|
||||
enclosure: options.enclosure || false
|
||||
};
|
||||
|
||||
this.items.push(item);
|
||||
@@ -72,7 +75,33 @@ function generateXML (data){
|
||||
|
||||
ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } });
|
||||
ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() });
|
||||
|
||||
if( item.enclosure && item.enclosure.url ) {
|
||||
if( item.enclosure.file ) {
|
||||
item_values.push({
|
||||
enclosure : {
|
||||
_attr : {
|
||||
url : item.enclosure.url,
|
||||
length : fs.statSync(item.enclosure.file).size,
|
||||
type : mime.lookup(item.enclosure.file)
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
item_values.push({
|
||||
enclosure : {
|
||||
_attr : {
|
||||
url : item.enclosure.url,
|
||||
length : item.enclosure.size || 0,
|
||||
type : mime.lookup(item.enclosure.url)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
channel.push({ item: item_values });
|
||||
|
||||
});
|
||||
|
||||
return { rss: [
|
||||
|
||||
@@ -21,7 +21,8 @@
|
||||
},
|
||||
"dependencies":
|
||||
{
|
||||
"xml": ">= 0.0.4"
|
||||
"xml": ">= 0.0.4",
|
||||
"mime": ">= 1.2.9"
|
||||
},
|
||||
"main": "lib/rss.js",
|
||||
"engines": { "node": ">=0.4.0" },
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
guid: '1123', // optional - defaults to url
|
||||
author: 'Guest Author', // optional - defaults to feed author property
|
||||
date: 'May 27, 2012' // any format that js Date can parse.
|
||||
enclosure : {url:'...', file:'path-to-file'} // optional
|
||||
});
|
||||
|
||||
// cache the xml
|
||||
@@ -97,7 +98,7 @@ especially when tests are included.
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011 Dylan Greene <dylang@gmail.com>
|
||||
Copyright (c) 2011-2013 Dylan Greene <dylang@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
||||
BIN
test/image.png
Normal file
BIN
test/image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
45
test/test.js
45
test/test.js
@@ -99,6 +99,51 @@ module.exports = {
|
||||
var expectedResult = '<?xml version="1.0" encoding="UTF-8"?>\n<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[title]]></title><description><![CDATA[description]]></description><link>http://example.com</link><generator>NodeJS RSS Module</generator><lastBuildDate>' + new Date().toUTCString() +'</lastBuildDate><atom:link href="http://example.com/rss.xml" rel="self" type="application/rss+xml"/><item><title><![CDATA[item 1]]></title><description><![CDATA[description 1]]></description><link>http://example.com/article1</link><guid isPermaLink="true">http://example.com/article1</guid><dc:creator><![CDATA[Dylan Greene]]></dc:creator><pubDate>Thu, 24 May 2012 04:00:00 GMT</pubDate></item><item><title><![CDATA[item 2]]></title><description><![CDATA[description 2]]></description><link>http://example.com/article2</link><guid isPermaLink="true">http://example.com/article2</guid><dc:creator><![CDATA[Dylan Greene]]></dc:creator><pubDate>Fri, 25 May 2012 04:00:00 GMT</pubDate></item><item><title><![CDATA[item 3]]></title><description><![CDATA[description 3]]></description><link>http://example.com/article3</link><guid isPermaLink="false">item3</guid><dc:creator><![CDATA[Dylan Greene]]></dc:creator><pubDate>Sat, 26 May 2012 04:00:00 GMT</pubDate></item><item><title><![CDATA[item 4 & html test with <strong>]]></title><description><![CDATA[description 4 uses some <strong>html</strong>]]></description><link>http://example.com/article4?this&that</link><guid isPermaLink="true">http://example.com/article4?this&that</guid><dc:creator><![CDATA[Guest Author]]></dc:creator><pubDate>Sun, 27 May 2012 04:00:00 GMT</pubDate></item></channel></rss>';
|
||||
var result = feed.xml();
|
||||
|
||||
test.equal(result.length, expectedResult.length);
|
||||
test.equal(result, expectedResult);
|
||||
test.done();
|
||||
},
|
||||
|
||||
'test with enclosure' : function(test) {
|
||||
var feed = new RSS({
|
||||
title: 'title',
|
||||
description: 'description',
|
||||
feed_url: 'http://example.com/rss.xml',
|
||||
site_url: 'http://example.com',
|
||||
author: 'Dylan Greene'
|
||||
});
|
||||
|
||||
feed.item({
|
||||
title: 'item 1',
|
||||
description: 'description 1',
|
||||
url: 'http://example.com/article1',
|
||||
date: 'May 24, 2012 04:00:00 GMT',
|
||||
enclosure : 'incorrect value'
|
||||
});
|
||||
|
||||
feed.item({
|
||||
title: 'item 2',
|
||||
description: 'description 2',
|
||||
url: 'http://example.com/article1',
|
||||
date: 'May 24, 2012 04:00:00 GMT',
|
||||
enclosure : {url: '/media/some-file.flv'}
|
||||
});
|
||||
|
||||
feed.item({
|
||||
title: 'item 3',
|
||||
description: 'description 3',
|
||||
url: 'http://example.com/article1',
|
||||
date: 'May 24, 2012 04:00:00 GMT',
|
||||
enclosure : {url: '/media/image.png', file : __dirname+'/image.png'}
|
||||
});
|
||||
|
||||
var expectedResult = '<?xml version="1.0" encoding="UTF-8"?>\n<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[title]]></title><description><![CDATA[description]]></description><link>http://example.com</link><generator>NodeJS RSS Module</generator><lastBuildDate>' + new Date().toUTCString() +'</lastBuildDate><atom:link href="http://example.com/rss.xml" rel="self" type="application/rss+xml"/>'+
|
||||
'<item><title><![CDATA[item 1]]></title><description><![CDATA[description 1]]></description><link>http://example.com/article1</link><guid isPermaLink="true">http://example.com/article1</guid><dc:creator><![CDATA[Dylan Greene]]></dc:creator><pubDate>Thu, 24 May 2012 04:00:00 GMT</pubDate></item>'+
|
||||
'<item><title><![CDATA[item 2]]></title><description><![CDATA[description 2]]></description><link>http://example.com/article1</link><guid isPermaLink="true">http://example.com/article1</guid><dc:creator><![CDATA[Dylan Greene]]></dc:creator><pubDate>Thu, 24 May 2012 04:00:00 GMT</pubDate><enclosure url="/media/some-file.flv" length="0" type="video/x-flv"/></item>'+
|
||||
'<item><title><![CDATA[item 3]]></title><description><![CDATA[description 3]]></description><link>http://example.com/article1</link><guid isPermaLink="true">http://example.com/article1</guid><dc:creator><![CDATA[Dylan Greene]]></dc:creator><pubDate>Thu, 24 May 2012 04:00:00 GMT</pubDate><enclosure url="/media/image.png" length="16650" type="image/png"/></item>'+
|
||||
'</channel></rss>';
|
||||
var result = feed.xml();
|
||||
|
||||
test.equal(result.length, expectedResult.length);
|
||||
test.equal(result, expectedResult);
|
||||
test.done();
|
||||
|
||||
Reference in New Issue
Block a user