Skip to content

Commit 515dc74

Browse files
author
Adriana Saroz
committed
Keep optional media:content attributes in the enclosures default property
1 parent 10161d2 commit 515dc74

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

lib/feedparser/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,23 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){
914914
}
915915
break;
916916
case('media:content'):
917+
var optionalAttributes = ['bitrate', 'framerate', 'samplingrate', 'duration', 'height', 'width'];
917918
if (Array.isArray(el)) {
918919
el.forEach(function (enc){
919920
enclosure = {};
920921
enclosure.url = _.get(enc['@'], 'url');
921922
enclosure.type = _.get(enc['@'], 'type') || _.get(enc['@'], 'medium');
922923
enclosure.length = _.get(enc['@'], 'filesize');
923-
if (indexOfObject(item.enclosures, enclosure, ['url', 'type']) === -1) {
924+
var index = indexOfObject(item.enclosures, enclosure, ['url', 'type']);
925+
if (index !== -1) {
926+
enclosure = item.enclosures[index];
927+
}
928+
optionalAttributes.forEach(function (attribute) {
929+
if (!enclosure[attribute] && _.get(enc['@'], attribute)) {
930+
enclosure[attribute] = _.get(enc['@'], attribute);
931+
}
932+
});
933+
if (index === -1) {
924934
item.enclosures.push(enclosure);
925935
}
926936
});
@@ -929,7 +939,16 @@ FeedParser.prototype.handleItem = function handleItem (node, type, options){
929939
enclosure.url = _.get(el['@'], 'url');
930940
enclosure.type = _.get(el['@'], 'type') || _.get(el['@'], 'medium');
931941
enclosure.length = _.get(el['@'], 'filesize');
932-
if (indexOfObject(item.enclosures, enclosure, ['url', 'type']) === -1) {
942+
var index = indexOfObject(item.enclosures, enclosure, ['url', 'type']);
943+
if (index !== -1) {
944+
enclosure = item.enclosures[index];
945+
}
946+
optionalAttributes.forEach(function (attribute) {
947+
if (!enclosure[attribute] && _.get(el['@'], attribute)) {
948+
enclosure[attribute] = _.get(el['@'], attribute);
949+
}
950+
});
951+
if (index === -1) {
933952
item.enclosures.push(enclosure);
934953
}
935954
}

test/duplicate-enclosures.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,26 @@ describe('duplicate enclosures', function(){
66
fs.createReadStream(feed).pipe(new FeedParser())
77
.once('readable', function () {
88
var stream = this;
9-
assert.strictEqual(stream.read().enclosures.length, 2);
9+
var enclosures = stream.read().enclosures;
10+
assert.strictEqual(enclosures.length, 3);
11+
assert.deepEqual(enclosures, [{
12+
url: 'http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697_154x115.jpg',
13+
type: 'image/jpeg',
14+
length: '4114',
15+
height: '115',
16+
width: '154'
17+
}, {
18+
url: 'http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697_154x115.mp4',
19+
type: 'video/mp4',
20+
length: '4114'
21+
}, {
22+
url: 'http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697.mp4',
23+
type: 'video/mp4',
24+
length: null,
25+
bitrate: '3000',
26+
height: '115',
27+
width: '154'
28+
}]);
1029
done();
1130
})
1231
.on('error', function (err) {

test/feeds/mediacontent-dupes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
<media:credit scheme="urn:ebu">© Bettmann/CORBIS</media:credit>
2828
<media:description type="html">Black Dahlia: A head shot of Elizabeth Short who had been an aspiring actress until her untimely murder</media:description>
2929
<media:thumbnail url="http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697_154x115.jpg" width="154" height="115" />
30-
<media:content type="image/jpeg" url="http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697_154x115.jpg" />
30+
<media:content type="image/jpeg" url="http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697_154x115.jpg" width="154" height="115"/>
31+
<media:content type="video/mp4" url="http://i.mol.im/i/pix/2013/02/03/article-2272640-174FCEE2000005DC-697.mp4" width="154" height="115" bitrate="3000"/>
3132
</item>
3233
</channel>
3334
</rss>

0 commit comments

Comments
 (0)