Skip to content

Commit 8228b2c

Browse files
committed
support media:content
1 parent b41f65c commit 8228b2c

File tree

5 files changed

+172
-6
lines changed

5 files changed

+172
-6
lines changed

lib/domain/media/content.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:xml/xml.dart';
2+
3+
class Content {
4+
final String url;
5+
final String type;
6+
final int fileSize;
7+
final String medium;
8+
final bool isDefault;
9+
final String expression;
10+
final int bitrate;
11+
final double framerate;
12+
final double samplingrate;
13+
final int channels;
14+
final int duration;
15+
final int height;
16+
final int width;
17+
final String lang;
18+
19+
Content({
20+
this.url,
21+
this.type,
22+
this.fileSize,
23+
this.medium,
24+
this.isDefault,
25+
this.expression,
26+
this.bitrate,
27+
this.framerate,
28+
this.samplingrate,
29+
this.channels,
30+
this.duration,
31+
this.height,
32+
this.width,
33+
this.lang,
34+
});
35+
36+
factory Content.parse(XmlElement element) {
37+
return new Content(
38+
url: element.getAttribute("url"),
39+
type: element.getAttribute("type"),
40+
fileSize: int.tryParse(element.getAttribute("fileSize") ?? "0"),
41+
medium: element.getAttribute("medium"),
42+
isDefault: element.getAttribute("isDefault") == "true",
43+
expression: element.getAttribute("expression"),
44+
bitrate: int.tryParse(element.getAttribute("bitrate") ?? "0"),
45+
framerate: double.tryParse(element.getAttribute("framerate") ?? "0"),
46+
samplingrate: double.tryParse(
47+
element.getAttribute("samplingrate") ?? "0",
48+
),
49+
channels: int.tryParse(element.getAttribute("channels") ?? "0"),
50+
duration: int.tryParse(element.getAttribute("duration") ?? "0"),
51+
height: int.tryParse(element.getAttribute("height") ?? "0"),
52+
width: int.tryParse(element.getAttribute("width") ?? "0"),
53+
lang: element.getAttribute("lang"),
54+
);
55+
}
56+
}

lib/domain/media/media.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:webfeed/domain/media/content.dart';
2+
import 'package:xml/xml.dart';
3+
4+
class Media {
5+
final List<Content> contents;
6+
7+
Media({this.contents});
8+
9+
factory Media.parse(XmlElement element) {
10+
var contents = element.findAllElements("media:content").map((e) {
11+
return new Content.parse(e);
12+
}).toList();
13+
return new Media(
14+
contents: contents,
15+
);
16+
}
17+
}

lib/domain/rss_item.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:webfeed/domain/media/media.dart';
12
import 'package:webfeed/domain/rss_category.dart';
23
import 'package:webfeed/domain/rss_content.dart';
34
import 'package:webfeed/domain/rss_source.dart';
@@ -16,22 +17,24 @@ class RssItem {
1617
final String comments;
1718
final RssSource source;
1819
final RssContent content;
20+
final Media media;
1921

20-
RssItem(
22+
RssItem({
2123
this.title,
2224
this.description,
23-
this.link, {
25+
this.link,
2426
this.categories,
2527
this.guid,
2628
this.pubDate,
2729
this.author,
2830
this.comments,
2931
this.source,
3032
this.content,
33+
this.media,
3134
});
3235

3336
factory RssItem.parse(XmlElement element) {
34-
var title = xmlGetString(element, "title");
37+
var title = xmlGetString(element, "title", strict: false);
3538
var description = xmlGetString(element, "description", strict: false);
3639
var link = xmlGetString(element, "link", strict: false);
3740

@@ -56,16 +59,17 @@ class RssItem {
5659
} on StateError {}
5760

5861
return new RssItem(
59-
title,
60-
description,
61-
link,
62+
title: title,
63+
description: description,
64+
link: link,
6265
categories: categories,
6366
guid: guid,
6467
pubDate: pubDate,
6568
author: author,
6669
comments: comments,
6770
source: source,
6871
content: content,
72+
media: Media.parse(element),
6973
);
7074
}
7175

test/rss_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,31 @@ void main() {
7676
expect(feed.items.first.content.value, "<img width=\"1000\" height=\"690\" src=\"https://test.com/image_link\"/> Test content<br />");
7777
expect(feed.items.first.content.images.first, "https://test.com/image_link");
7878
});
79+
test("parsing RSS-Media.xml", (){
80+
var xmlString = new File("test/xml/RSS-Media.xml").readAsStringSync();
81+
82+
var feed = new RssFeed.parse(xmlString);
83+
expect(feed.title, "Song Site");
84+
expect(feed.description, "Media RSS example with new fields added in v1.5.0");
85+
86+
expect(feed.items.length, 1);
87+
88+
var item = feed.items.first;
89+
expect(item.title, null);
90+
expect(item.link, "http://www.foo.com");
91+
expect(item.pubDate, "Mon, 27 Aug 2001 16:08:56 PST");
92+
93+
expect(item.media.contents.length, 2);
94+
var mediaContent = item.media.contents.first;
95+
expect(mediaContent.url, "http://www.foo.com/video.mov");
96+
expect(mediaContent.type, "video/quicktime");
97+
expect(mediaContent.fileSize, 2000);
98+
expect(mediaContent.medium, "video");
99+
expect(mediaContent.isDefault, true);
100+
expect(mediaContent.expression, "full");
101+
expect(mediaContent.bitrate, 128);
102+
expect(mediaContent.framerate, 25);
103+
expect(mediaContent.samplingrate, 44.1);
104+
expect(mediaContent.channels, 2);
105+
});
79106
}

test/xml/RSS-Media.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"
2+
xmlns:georss="http://www.georss.org/georss"
3+
xmlns:gml="http://www.opengis.net/gml">
4+
5+
<channel>
6+
<title>Song Site</title>
7+
<description>Media RSS example with new fields added in v1.5.0</description>
8+
<item>
9+
<link>http://www.foo.com</link>
10+
<pubDate>Mon, 27 Aug 2001 16:08:56 PST</pubDate>
11+
<media:content url="http://www.foo.com/video.mov" fileSize="2000" medium="video" isDefault="true" expression="full" bitrate="128" framerate="25" samplingrate="44.1" channels="2" type="video/quicktime" />
12+
<media:content url="http://www.foo.com/video2.mov" fileSize="2000" bitrate="128" type="video/quicktime" expression="full" />
13+
<media:community>
14+
<media:starRating average="3.5" count="20" min="1" max="10" />
15+
<media:statistics views="5" favorites="5" />
16+
<media:tags>news: 5, abc:3</media:tags>
17+
</media:community>
18+
<media:comments>
19+
<media:comment>comment1</media:comment>
20+
<media:comment>comment2</media:comment>
21+
</media:comments>
22+
<media:embed url="http://www.foo.com/player.swf" width="512" height="323">
23+
<media:param name="type">application/x-shockwave-flash</media:param>
24+
<media:param name="width">512</media:param>
25+
<media:param name="height">323</media:param>
26+
<media:param name="allowFullScreen">true</media:param>
27+
<media:param name="flashVars">
28+
id=12345&amp;vid=678912i&amp;lang=en-us&amp;intl=us&amp;thumbUrl=http://www.foo.com/thumbnail.jpg
29+
</media:param>
30+
</media:embed>
31+
<media:responses>
32+
<media:response>http://www.response1.com</media:response>
33+
<media:response>http://www.response2.com</media:response>
34+
</media:responses>
35+
<media:backLinks>
36+
<media:backLink>http://www.backlink1.com</media:backLink>
37+
<media:backLink>http://www.backlink2.com</media:backLink>
38+
</media:backLinks>
39+
<media:status state="active" />
40+
<media:price type="rent" price="19.99" currency="EUR" />
41+
<media:license type="text/html" href="http://www.licensehost.com/license"> Sample license for a video</media:license>
42+
<media:subTitle type="application/smil" lang="en-us" href="http://www.foo.org/subtitle.smil" />
43+
<media:peerLink type="application/x-bittorrent " href="http://www.foo.org/sampleFile.torrent" />
44+
<media:location description="My house" start="00:01" end="01:00">
45+
<georss:where>
46+
<gml:Point>
47+
<gml:pos>35.669998 139.770004</gml:pos>
48+
</gml:Point>
49+
</georss:where>
50+
</media:location>
51+
<media:restriction type="sharing" relationship="deny" />
52+
<media:scenes>
53+
<media:scene>
54+
<sceneTitle>sceneTitle1</sceneTitle>
55+
<sceneDescription>sceneDesc1</sceneDescription>
56+
<sceneStartTime>00:15</sceneStartTime>
57+
<sceneEndTime>00:45</sceneEndTime>
58+
</media:scene>
59+
</media:scenes>
60+
</item>
61+
</channel>
62+
</rss>

0 commit comments

Comments
 (0)