Skip to content

Commit

Permalink
Add media content parsing to FeedHandler (#560)
Browse files Browse the repository at this point in the history
* Add media content parsing to FeedHandler
* parseInt with base 10
* Add media:contant to RSS fixture
  • Loading branch information
gcandal committed Sep 13, 2020
1 parent 79d3673 commit a85e4e0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/FeedHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,44 @@ import DomHandler, { DomHandlerOptions, Node, Element } from "domhandler";
import * as DomUtils from "domutils";
import { Parser, ParserOptions } from "./Parser";

enum FeedItemMediaMedium {
image,
audio,
video,
document,
executable,
}

enum FeedItemMediaExpression {
sample,
full,
nonstop,
}

interface FeedItemMedia {
url?: string;
fileSize?: number;
type?: string;
medium: FeedItemMediaMedium;
isDefault?: boolean;
expression?: FeedItemMediaExpression;
bitrate?: number;
framerate?: number;
samplingrate?: number;
channels?: number;
duration?: number;
height?: number;
width?: number;
lang?: string;
}

interface FeedItem {
id?: string;
title?: string;
link?: string;
description?: string;
pubDate?: Date;
media?: FeedItemMedia[];
}

interface Feed {
Expand Down Expand Up @@ -93,6 +125,8 @@ export class FeedHandler extends DomHandler {
entry.pubDate = new Date(pubDate);
}

entry.media = getMediaElements(children);

return entry;
});
} else {
Expand Down Expand Up @@ -133,6 +167,7 @@ export class FeedHandler extends DomHandler {
);
const pubDate = fetch("pubDate", children);
if (pubDate) entry.pubDate = new Date(pubDate);
entry.media = getMediaElements(children);
return entry;
}
);
Expand All @@ -147,6 +182,26 @@ export class FeedHandler extends DomHandler {
}
}

function getMediaElements(where: Node | Node[]): FeedItemMedia[] {
return getElements("media:content", where).map((media) => ({
url: media.attribs.url,
fileSize: parseInt(media.attribs.fileSize, 10),
type: media.attribs.type,
medium: (media.attribs.medium as unknown) as FeedItemMediaMedium,
isDefault: Boolean(media.attribs.isDefault),
expression: (media.attribs
.expression as unknown) as FeedItemMediaExpression,
bitrate: parseInt(media.attribs.bitrate, 10),
framerate: parseInt(media.attribs.framerate, 10),
samplingrate: parseInt(media.attribs.samplingrate, 10),
channels: parseInt(media.attribs.channels, 10),
duration: parseInt(media.attribs.duration, 10),
height: parseInt(media.attribs.height, 10),
width: parseInt(media.attribs.width, 10),
lang: media.attribs.lang,
}));
}

function getElements(tagName: string, where: Node | Node[]) {
return DomUtils.getElementsByTagName(tagName, where, true);
}
Expand Down
1 change: 1 addition & 0 deletions src/__fixtures__/Documents/RSS_Example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>

<media:content height="200" medium="image" url="https://picsum.photos/200" width="200"/>
</item>
</channel>
</rss>
42 changes: 41 additions & 1 deletion src/__fixtures__/Stream/02-RSS.json
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,47 @@
},
{
"event": "text",
"data": ["\n\n "]
"data": ["\n\n "]
},
{
"event": "opentagname",
"data": ["media:content"]
},
{
"event": "attribute",
"data": ["height", "200", "\""]
},
{
"event": "attribute",
"data": ["medium", "image", "\""]
},
{
"event": "attribute",
"data": ["url", "https://picsum.photos/200", "\""]
},
{
"event": "attribute",
"data": ["width", "200", "\""]
},
{
"event": "opentag",
"data": [
"media:content",
{
"height": "200",
"medium": "image",
"url": "https://picsum.photos/200",
"width": "200"
}
]
},
{
"event": "closetag",
"data": ["media:content"]
},
{
"event": "text",
"data": ["\n "]
},
{
"event": "closetag",
Expand Down

0 comments on commit a85e4e0

Please sign in to comment.