Skip to content

Commit

Permalink
added support for torrent via arrays/objects/MRSS
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelk committed Apr 14, 2018
1 parent 687fae9 commit 83b3e5a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pfeed",
"version": "1.1.2",
"version": "1.1.3",
"description": "Feed is a RSS 2.0, Atom 1.0 and Json Feed 1.0 generator for Node.js, based on work from Jean-Philippe Monette and initiated by a need of the PeerTube project",
"homepage": "http://github.com/rigelk/feed",
"author": "Rigel Kent <sendmemail@rigelk.eu>",
Expand Down
62 changes: 55 additions & 7 deletions src/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ class Feed {
}

if(entry.guid) {
item.push({ guid: entry.guid });
if (entry.guid.indexOf('http') === -1) {
item.push({ guid: { _cdata: entry.guid, _attr: {isPermaLink: 'false'} } });
} else {
item.push({ guid: entry.guid });
}
} else if (entry.link) {
item.push({ guid: entry.link });
}
Expand All @@ -370,22 +374,52 @@ class Feed {
if (author.email && author.name) {
item.push({ author: author.email + ' (' + author.name + ')' })
return true
} else {
return false
}
} else if (author.name) {
item.push({ 'dc:creator': author.name });
return true;
} return false
})
}

// rss feed only supports 1 enclosure per item
if (entry.torrent) {
item.push({ enclosure: [{ _attr: { url: entry.torrent, type: 'application/x-bittorrent' } }] });
} else if(entry.image) {
let metainfo = entry.torrent;
if (!Array.isArray(metainfo)) metainfo = [ metainfo ];

metainfo.forEach((i, index) => {
let i_metainfo = i
if (!(i instanceof Object)) i_metainfo = { url: i };

if (index == 0)
item.push({ enclosure: [{ _attr: { url: i_metainfo.url, type: 'application/x-bittorrent' } }] });
else {
if (index == 1) {
rss[0]._attr['xmlns:media'] = 'http://search.yahoo.com/mrss/';
let previous_metainfo = (!(metainfo[0] instanceof Object))? { url: metainfo[0] } : metainfo[0];
item.push({
'media:peerLink': [{
_attr: { type: 'application/x-bittorrent', href: previous_metainfo.url }
}]
});
}
item.push({
'media:peerLink': [{
_attr: { type: 'application/x-bittorrent', href: i_metainfo.url }
}]
});
}
});


} else if(entry.image) {
item.push({ enclosure: [{ _attr: { url: entry.image } }] });
}

channel.push({ item });
})

if(isContent) {
rss[0]._attr['xmlns:dc'] = 'http://purl.org/dc/elements/1.1/';
rss[0]._attr['xmlns:content'] = 'http://purl.org/rss/1.0/modules/content/';
}

Expand Down Expand Up @@ -415,7 +449,6 @@ class Feed {
feed.description = options.description;
}


if (options.image) {
feed.icon = options.image;
}
Expand Down Expand Up @@ -451,6 +484,21 @@ class Feed {
feedItem.summary = item.description;
}

if (item.torrent) {
let metainfo = item.torrent
if (!Array.isArray(metainfo)) metainfo = [ metainfo ]
if (!feedItem.attachments) feedItem.attachments = []

metainfo.forEach(i => {
let i_metainfo = i
if (!(i instanceof Object)) i_metainfo = { url: i }
feedItem.attachments.push({
...i_metainfo,
mime_type: 'application/x-bittorrent'
})
});
}

if (item.image) {
feedItem.image = item.image
}
Expand Down
80 changes: 78 additions & 2 deletions src/feed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ feed.addItem({
}
}],
date: sampleDate,
torrent: 'https://example.com/hello-world.torrent'
// torrent: 'https://example.com/hello-world.torrent'
torrent: {
url: 'https://example.com/hello-world.torrent'
}
})

feed.addExtension({
Expand Down Expand Up @@ -200,7 +203,13 @@ test('it should generate a JSON v1 feed', () => {
"_item_extension_2": {
"about": "just a second item extension example",
"dummy1": "example"
}
},
"attachments": [
{
"mime_type": "application/x-bittorrent",
"url": "https://example.com/hello-world.torrent"
}
]
}],
"_example_extension": {
"about": "just an extension example",
Expand All @@ -214,3 +223,70 @@ test('it should generate a JSON v1 feed', () => {

expect(actual).toEqual(expected)
});


test('it should generate a Media RSS 1.5 feed', () => {
feed.addItem({
title: 'Hello World',
id: 'https://example.com/hello-world',
link: 'https://example.com/hello-world',
description: 'This is an article about Hello World.',
date: sampleDate,
torrent: [
{
url: 'https://example.com/hello-world-vp8-ogg.torrent'
},
{
url: 'https://example.com/hello-world-vp9-opus.torrent'
}
]
});

let expected = `<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss/\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>
<title>Feed Title</title>
<link>http://example.com/</link>
<description>This is my personnal feed!</description>
<lastBuildDate>Sat, 13 Jul 2013 23:00:00 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>awesome</generator>
<image>
<title>Feed Title</title>
<url>http://example.com/image.png</url>
<link>http://example.com/</link>
</image>
<copyright>All rights reserved 2013, John Doe</copyright>
<category>Technology</category>
<atom:link href=\"http://example.com/feed.rss\" rel=\"self\" type=\"application/rss+xml\"/>
<item>
<geo:lat>23</geo:lat>
<title><![CDATA[Hello World]]></title>
<link>https://example.com/hello-world</link>
<guid>https://example.com/hello-world</guid>
<pubDate>Sat, 13 Jul 2013 23:00:00 GMT</pubDate>
<description><![CDATA[This is an article about Hello World.]]></description>
<author>janedoe@example.com (Jane Doe)</author>
<enclosure url="https://example.com/hello-world.torrent" type="application/x-bittorrent">
</enclosure>
</item>
<item>
<title><![CDATA[Hello World]]></title>
<link>https://example.com/hello-world</link>
<guid>https://example.com/hello-world</guid>
<pubDate>Sat, 13 Jul 2013 23:00:00 GMT</pubDate>
<description><![CDATA[This is an article about Hello World.]]></description>
<enclosure url="https://example.com/hello-world-vp8-ogg.torrent" type="application/x-bittorrent">
</enclosure>
<media:peerLink type="application/x-bittorrent" href="https://example.com/hello-world-vp8-ogg.torrent">
</media:peerLink>
<media:peerLink type="application/x-bittorrent" href="https://example.com/hello-world-vp9-opus.torrent">
</media:peerLink>
</item>
</channel>
</rss>`;

let actual = feed.rss2()

expect(actual).toBe(expected)
});
49 changes: 0 additions & 49 deletions test/feed.js

This file was deleted.

0 comments on commit 83b3e5a

Please sign in to comment.