Bladeren bron

parse atom date time

master
Wito Chandra 6 jaren geleden
bovenliggende
commit
edb5fae117
5 gewijzigde bestanden met toevoegingen van 34 en 16 verwijderingen
  1. +3
    -2
      lib/domain/atom_feed.dart
  2. +3
    -2
      lib/domain/atom_item.dart
  3. +12
    -0
      lib/util/datetime.dart
  4. +15
    -11
      test/atom_test.dart
  5. +1
    -1
      test/xml/Atom.xml

+ 3
- 2
lib/domain/atom_feed.dart Bestand weergeven

@@ -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<AtomItem> items;

final List<AtomLink> 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(),


+ 3
- 2
lib/domain/atom_item.dart Bestand weergeven

@@ -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<AtomPerson> authors;
final List<AtomLink> 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(),


+ 12
- 0
lib/util/datetime.dart Bestand weergeven

@@ -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;
}
}

+ 15
- 11
test/atom_test.dart Bestand weergeven

@@ -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");


+ 1
- 1
test/xml/Atom.xml Bestand weergeven

@@ -35,7 +35,7 @@
<entry>
<id>foo-bar-entry-id-1</id>
<title>Foo bar item 1</title>
<updated>2018-04-06T13:02:47Z</updated>
<updated>2018-04-06T13:02:40Z</updated>
<author>
<name>Ellie</name>
<uri>http://foo.bar.news/people/ellie</uri>


Laden…
Annuleren
Opslaan