From edb5fae1178c25fa33f1d0e61b39f69b63faaaf1 Mon Sep 17 00:00:00 2001 From: Wito Chandra Date: Wed, 12 Aug 2020 00:42:49 +0700 Subject: [PATCH] parse atom date time --- lib/domain/atom_feed.dart | 5 +++-- lib/domain/atom_item.dart | 5 +++-- lib/util/datetime.dart | 12 ++++++++++++ test/atom_test.dart | 26 +++++++++++++++----------- test/xml/Atom.xml | 2 +- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/domain/atom_feed.dart b/lib/domain/atom_feed.dart index 9d8d2c9..1631013 100644 --- a/lib/domain/atom_feed.dart +++ b/lib/domain/atom_feed.dart @@ -3,13 +3,14 @@ import 'package:webfeed/domain/atom_generator.dart'; import 'package:webfeed/domain/atom_item.dart'; import 'package:webfeed/domain/atom_link.dart'; import 'package:webfeed/domain/atom_person.dart'; +import 'package:webfeed/util/datetime.dart'; import 'package:webfeed/util/xml.dart'; import 'package:xml/xml.dart'; class AtomFeed { final String id; final String title; - final String updated; + final DateTime updated; final List items; final List links; @@ -50,7 +51,7 @@ class AtomFeed { return AtomFeed( id: findElementOrNull(feedElement, "id")?.text, title: findElementOrNull(feedElement, "title")?.text, - updated: findElementOrNull(feedElement, "updated")?.text, + updated: parseDateTime(findElementOrNull(feedElement, "updated")?.text), items: feedElement.findElements("entry").map((element) { return AtomItem.parse(element); }).toList(), diff --git a/lib/domain/atom_item.dart b/lib/domain/atom_item.dart index 2badbb0..f04181f 100644 --- a/lib/domain/atom_item.dart +++ b/lib/domain/atom_item.dart @@ -3,13 +3,14 @@ import 'package:webfeed/domain/atom_link.dart'; import 'package:webfeed/domain/atom_person.dart'; import 'package:webfeed/domain/atom_source.dart'; import 'package:webfeed/domain/media/media.dart'; +import 'package:webfeed/util/datetime.dart'; import 'package:webfeed/util/xml.dart'; import 'package:xml/xml.dart'; class AtomItem { final String id; final String title; - final String updated; + final DateTime updated; final List authors; final List links; @@ -42,7 +43,7 @@ class AtomItem { return AtomItem( id: findElementOrNull(element, "id")?.text, title: findElementOrNull(element, "title")?.text, - updated: findElementOrNull(element, "updated")?.text, + updated: parseDateTime(findElementOrNull(element, "updated")?.text), authors: element.findElements("author").map((element) { return AtomPerson.parse(element); }).toList(), diff --git a/lib/util/datetime.dart b/lib/util/datetime.dart index ef9b289..8c79901 100644 --- a/lib/util/datetime.dart +++ b/lib/util/datetime.dart @@ -4,9 +4,21 @@ var rfc822DateFormat = DateFormat('EEE, dd MMM yyyy HH:mm:ss Z', 'en_US'); DateTime parseDateTime(dateString) { if (dateString == null) return null; + return _parseRfc822DateTime(dateString) ?? _parseIso8601DateTime(dateString); +} + +DateTime _parseRfc822DateTime(dateString) { try { return rfc822DateFormat.parse(dateString); } on FormatException { return null; } } + +DateTime _parseIso8601DateTime(dateString) { + try { + return DateTime.parse(dateString); + } on FormatException { + return null; + } +} diff --git a/test/atom_test.dart b/test/atom_test.dart index e7ba377..70b6b0f 100644 --- a/test/atom_test.dart +++ b/test/atom_test.dart @@ -21,7 +21,7 @@ void main() { expect(feed.id, "foo-bar-id"); expect(feed.title, "Foo bar news"); - expect(feed.updated, "2018-04-06T13:02:46Z"); + expect(feed.updated, DateTime.utc(2018, 4, 6, 13, 2, 46)); expect(feed.links.length, 2); expect(feed.links.first.rel, "foo"); @@ -58,7 +58,7 @@ void main() { var item = feed.items.first; expect(item.id, "foo-bar-entry-id-1"); expect(item.title, "Foo bar item 1"); - expect(item.updated, "2018-04-06T13:02:47Z"); + expect(item.updated, DateTime.utc(2018, 4, 6, 13, 2, 40)); expect(item.authors.length, 2); expect(item.authors.first.name, "Ellie"); @@ -88,16 +88,16 @@ void main() { expect(item.content, "This is content 1"); expect(item.rights, "This is rights 1"); }); - test("parse Atom-Media.xml", (){ + test("parse Atom-Media.xml", () { var xmlString = new File("test/xml/Atom-Media.xml").readAsStringSync(); var feed = new AtomFeed.parse(xmlString); expect(feed.id, "foo-bar-id"); expect(feed.title, "Foo bar news"); - expect(feed.updated, "2018-04-06T13:02:46Z"); + expect(feed.updated, DateTime.utc(2018, 4, 6, 13, 2, 46)); expect(feed.items.length, 1); - + var item = feed.items.first; expect(item.media.group.contents.length, 5); expect(item.media.group.credits.length, 2); @@ -123,7 +123,8 @@ void main() { expect(mediaCredit.scheme, "urn:yvs"); expect(mediaCredit.value, "copyright holder of the entity"); - expect(item.media.category.scheme, "http://search.yahoo.com/mrss/category_ schema"); + expect(item.media.category.scheme, + "http://search.yahoo.com/mrss/category_ schema"); expect(item.media.category.label, "Music"); expect(item.media.category.value, "music/artist/album/song"); @@ -134,10 +135,11 @@ void main() { expect(item.media.title.value, "The Judy's -- The Moo Song"); expect(item.media.description.type, "plain"); - expect(item.media.description.value, "This was some really bizarre band I listened to as a young lad."); - + expect(item.media.description.value, + "This was some really bizarre band I listened to as a young lad."); + expect(item.media.keywords, "kitty, cat, big dog, yarn, fluffy"); - + expect(item.media.thumbnails.length, 2); var mediaThumbnail = item.media.thumbnails.first; expect(mediaThumbnail.url, "http://www.foo.com/keyframe1.jpg"); @@ -184,7 +186,8 @@ void main() { expect(item.media.embed.height, 323); expect(item.media.embed.params.length, 5); expect(item.media.embed.params.first.name, "type"); - expect(item.media.embed.params.first.value, "application/x-shockwave-flash"); + expect( + item.media.embed.params.first.value, "application/x-shockwave-flash"); expect(item.media.responses.length, 2); expect(item.media.responses.first, "http://www.response1.com"); @@ -200,7 +203,8 @@ void main() { expect(item.media.prices.length, 2); expect(item.media.prices.first.price, 19.99); expect(item.media.prices.first.type, "rent"); - expect(item.media.prices.first.info, "http://www.dummy.jp/package_info.html"); + expect( + item.media.prices.first.info, "http://www.dummy.jp/package_info.html"); expect(item.media.prices.first.currency, "EUR"); expect(item.media.license.type, "text/html"); diff --git a/test/xml/Atom.xml b/test/xml/Atom.xml index e88eb12..02f02bf 100644 --- a/test/xml/Atom.xml +++ b/test/xml/Atom.xml @@ -35,7 +35,7 @@ foo-bar-entry-id-1 Foo bar item 1 - 2018-04-06T13:02:47Z + 2018-04-06T13:02:40Z Ellie http://foo.bar.news/people/ellie