From 8228b2cdf98e309454de44e28fe0ee17f6e50f65 Mon Sep 17 00:00:00 2001 From: Wito Chandra Date: Thu, 13 Sep 2018 00:38:10 +0700 Subject: [PATCH] support media:content --- lib/domain/media/content.dart | 56 +++++++++++++++++++++++++++++++ lib/domain/media/media.dart | 17 ++++++++++ lib/domain/rss_item.dart | 16 +++++---- test/rss_test.dart | 27 +++++++++++++++ test/xml/RSS-Media.xml | 62 +++++++++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 6 deletions(-) create mode 100644 lib/domain/media/content.dart create mode 100644 lib/domain/media/media.dart create mode 100644 test/xml/RSS-Media.xml diff --git a/lib/domain/media/content.dart b/lib/domain/media/content.dart new file mode 100644 index 0000000..3a4ebaa --- /dev/null +++ b/lib/domain/media/content.dart @@ -0,0 +1,56 @@ +import 'package:xml/xml.dart'; + +class Content { + final String url; + final String type; + final int fileSize; + final String medium; + final bool isDefault; + final String expression; + final int bitrate; + final double framerate; + final double samplingrate; + final int channels; + final int duration; + final int height; + final int width; + final String lang; + + Content({ + this.url, + this.type, + this.fileSize, + this.medium, + this.isDefault, + this.expression, + this.bitrate, + this.framerate, + this.samplingrate, + this.channels, + this.duration, + this.height, + this.width, + this.lang, + }); + + factory Content.parse(XmlElement element) { + return new Content( + url: element.getAttribute("url"), + type: element.getAttribute("type"), + fileSize: int.tryParse(element.getAttribute("fileSize") ?? "0"), + medium: element.getAttribute("medium"), + isDefault: element.getAttribute("isDefault") == "true", + expression: element.getAttribute("expression"), + bitrate: int.tryParse(element.getAttribute("bitrate") ?? "0"), + framerate: double.tryParse(element.getAttribute("framerate") ?? "0"), + samplingrate: double.tryParse( + element.getAttribute("samplingrate") ?? "0", + ), + channels: int.tryParse(element.getAttribute("channels") ?? "0"), + duration: int.tryParse(element.getAttribute("duration") ?? "0"), + height: int.tryParse(element.getAttribute("height") ?? "0"), + width: int.tryParse(element.getAttribute("width") ?? "0"), + lang: element.getAttribute("lang"), + ); + } +} diff --git a/lib/domain/media/media.dart b/lib/domain/media/media.dart new file mode 100644 index 0000000..fa23e0e --- /dev/null +++ b/lib/domain/media/media.dart @@ -0,0 +1,17 @@ +import 'package:webfeed/domain/media/content.dart'; +import 'package:xml/xml.dart'; + +class Media { + final List contents; + + Media({this.contents}); + + factory Media.parse(XmlElement element) { + var contents = element.findAllElements("media:content").map((e) { + return new Content.parse(e); + }).toList(); + return new Media( + contents: contents, + ); + } +} diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index 2a0a87e..8157bae 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,3 +1,4 @@ +import 'package:webfeed/domain/media/media.dart'; import 'package:webfeed/domain/rss_category.dart'; import 'package:webfeed/domain/rss_content.dart'; import 'package:webfeed/domain/rss_source.dart'; @@ -16,11 +17,12 @@ class RssItem { final String comments; final RssSource source; final RssContent content; + final Media media; - RssItem( + RssItem({ this.title, this.description, - this.link, { + this.link, this.categories, this.guid, this.pubDate, @@ -28,10 +30,11 @@ class RssItem { this.comments, this.source, this.content, + this.media, }); factory RssItem.parse(XmlElement element) { - var title = xmlGetString(element, "title"); + var title = xmlGetString(element, "title", strict: false); var description = xmlGetString(element, "description", strict: false); var link = xmlGetString(element, "link", strict: false); @@ -56,9 +59,9 @@ class RssItem { } on StateError {} return new RssItem( - title, - description, - link, + title: title, + description: description, + link: link, categories: categories, guid: guid, pubDate: pubDate, @@ -66,6 +69,7 @@ class RssItem { comments: comments, source: source, content: content, + media: Media.parse(element), ); } diff --git a/test/rss_test.dart b/test/rss_test.dart index 0952287..b0b762c 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -76,4 +76,31 @@ void main() { expect(feed.items.first.content.value, " Test content
"); expect(feed.items.first.content.images.first, "https://test.com/image_link"); }); + test("parsing RSS-Media.xml", (){ + var xmlString = new File("test/xml/RSS-Media.xml").readAsStringSync(); + + var feed = new RssFeed.parse(xmlString); + expect(feed.title, "Song Site"); + expect(feed.description, "Media RSS example with new fields added in v1.5.0"); + + expect(feed.items.length, 1); + + var item = feed.items.first; + expect(item.title, null); + expect(item.link, "http://www.foo.com"); + expect(item.pubDate, "Mon, 27 Aug 2001 16:08:56 PST"); + + expect(item.media.contents.length, 2); + var mediaContent = item.media.contents.first; + expect(mediaContent.url, "http://www.foo.com/video.mov"); + expect(mediaContent.type, "video/quicktime"); + expect(mediaContent.fileSize, 2000); + expect(mediaContent.medium, "video"); + expect(mediaContent.isDefault, true); + expect(mediaContent.expression, "full"); + expect(mediaContent.bitrate, 128); + expect(mediaContent.framerate, 25); + expect(mediaContent.samplingrate, 44.1); + expect(mediaContent.channels, 2); + }); } diff --git a/test/xml/RSS-Media.xml b/test/xml/RSS-Media.xml new file mode 100644 index 0000000..0a16c59 --- /dev/null +++ b/test/xml/RSS-Media.xml @@ -0,0 +1,62 @@ + + + + Song Site + Media RSS example with new fields added in v1.5.0 + + http://www.foo.com + Mon, 27 Aug 2001 16:08:56 PST + + + + + + news: 5, abc:3 + + + comment1 + comment2 + + + application/x-shockwave-flash + 512 + 323 + true + + id=12345&vid=678912i&lang=en-us&intl=us&thumbUrl=http://www.foo.com/thumbnail.jpg + + + + http://www.response1.com + http://www.response2.com + + + http://www.backlink1.com + http://www.backlink2.com + + + + Sample license for a video + + + + + + 35.669998 139.770004 + + + + + + + sceneTitle1 + sceneDesc1 + 00:15 + 00:45 + + + + + \ No newline at end of file