Skip to content

Commit

Permalink
Add enclosure test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
lwindolf committed Jun 16, 2024
1 parent 3b593d1 commit 2edbcdf
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
54 changes: 53 additions & 1 deletion tests/namespace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,56 @@ test('content:encoded', () => {
expect(feed.items.length).toBe(1);
expect(feed.items[0].description).toBe(`<p>What a <em>beautiful</em> day!</p>`);
});


test('media:content', () => {
let feed = RDFParser.parse(`<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns="http://purl.org/rss/1.0/"
>
<channel rdf:about="http://example.org/rss.rdf">
<title>Example Feed</title>
<link>http://www.example.org</link>
<description>Simply for the purpose of demonstration.</description>
<items>
<rdf:Seq>
<rdf:li resource="http://example.org/item/" />
</rdf:Seq>
</items>
</channel>
<item rdf:about="http://example.org/item/">
<title>The Example Item</title>
<link>http://example.org/item/</link>
<media:content
url="http://www.foo.com/movie.mov"
fileSize="12216320"
type="video/quicktime"
medium="video"
isDefault="true"
expression="full"
bitrate="128"
framerate="25"
samplingrate="44.1"
channels="2"
duration="185"
height="200"
width="300"
lang="en" />
</item>
</rdf:RDF>`);

expect(feed.error).toBe(undefined);
expect(feed.items.length).toBe(1);
expect(feed.items[0].media.length).toBe(1);
expect(feed.items[0].media[0].url).toBe("http://www.foo.com/movie.mov");
expect(feed.items[0].media[0].mime).toBe("video/quicktime");
expect(feed.items[0].media[0].length).toBe(185);
});

7 changes: 7 additions & 0 deletions tests/rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ test('rss 2.0 parse', () => {
<description>As part of the state's first Earth-to-space call...</description>
<pubDate>Fri, 21 Jul 2023 09:04 EDT</pubDate>
<guid>http://www.nasa.gov/press-release/louisiana-students-to-hear-from-nasa-astronauts-aboard-space-station</guid>
<enclosure url="https://example.com/mp3s/podcast1_part1.mp3" length="1500000" type="audio/mpeg" />
<enclosure url="https://example.com/mp3s/podcast1_part2.mp3" length="1450000" type="audio/mpeg" />
</item>
<item>
<description>NASA has selected KBR Wyle Services, LLC, of Fulton, Maryland, to provide mission and flight crew operations support for the International Space Station and future human space exploration.</description>
Expand All @@ -77,4 +79,9 @@ test('rss 2.0 parse', () => {
expect(feed.items[0].source).toBe('http://www.nasa.gov/press-release/louisiana-students-to-hear-from-nasa-astronauts-aboard-space-station');
expect(feed.items[0].sourceId).toBe('http://www.nasa.gov/press-release/louisiana-students-to-hear-from-nasa-astronauts-aboard-space-station');
expect(feed.items[0].time).toBe(1689944640)
expect(feed.items[0].media.length).toBe(2)
expect(feed.items[0].media[0].url).toBe('https://example.com/mp3s/podcast1_part1.mp3')
expect(feed.items[0].media[0].mime).toBe('audio/mpeg')
expect(feed.items[0].media[0].length).toBe(NaN)
expect(feed.items[0].media[1].url).toBe('https://example.com/mp3s/podcast1_part2.mp3')
});
23 changes: 16 additions & 7 deletions www/assets/js/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ export class Item {
}
}

addMedia(url, mime, length) {
let l = parseInt(length, 10);

if (Number.isNaN(l))
l = undefined;
/**
* Add a media enclosure to the item
*
* @param {*} url valid URL
* @param {*} mime MIME type or 'audio' or 'video'
* @param {*} length (optional) duration in [s]
*/
addMedia(url, mime, length = NaN) {
let l = NaN;

try {
l = parseInt(length, 10);
// eslint-disable-next-line no-empty
} catch { }

if(!url || !mime)
return;
Expand All @@ -47,9 +56,9 @@ export class Item {
return;

/* Never add enclosures for images already contained in the description */
if (-1 !== this.description.indexOf(url))
if (this.description && -1 !== this.description.indexOf(url))
return;

this.media.push({ url, mime, l });
this.media.push({ url, mime, length: l });
}
}
2 changes: 1 addition & 1 deletion www/assets/js/parsers/atom.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AtomParser {
NamespaceParser.parseItem(node, ['dc', 'content', 'media'], feed, item);

XPath.foreach(node, 'ns:link', AtomParser.parseEntryLink, item);
console.log(feed)

feed.addItem(item);
}

Expand Down
10 changes: 5 additions & 5 deletions www/assets/js/parsers/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export class NamespaceParser {
(example quoted from specification)
*/
XPath.foreach(node, '//media:content', (n) => {
item.addMedia(
n.lookup('@url'),
n.lookup('@type') || n.lookup('@medium'),
n.lookup('@duration')
);
item.addMedia(
XPath.lookup(n, '@url'),
XPath.lookup(n, '@type') || XPath.lookup(n, '@medium'),
XPath.lookup(n, '@duration')
);
});
}
}
Expand Down
9 changes: 4 additions & 5 deletions www/assets/js/parsers/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ class RSSParser {
time: DateParser.parse(XPath.lookup(node, 'pubDate'))
});

XPath.foreach(node, 'enclosure', (n) => {
XPath.foreach(node, 'enclosure', (n) =>
item.addMedia(
XPath.lookup(n, '@url'),
XPath.lookup(n, '@type'),
XPath.lookup(n, '@length')
);
});
XPath.lookup(n, '@type')
)
);

NamespaceParser.parseItem(node, ['dc', 'content', 'media'], feed, item);

Expand Down

0 comments on commit 2edbcdf

Please sign in to comment.