From 88419721d150c6f134c068ab2ba2904cabb3c026 Mon Sep 17 00:00:00 2001 From: Chonli Date: Wed, 17 Mar 2021 20:57:07 +0100 Subject: [PATCH] Add W3C GeoRSS --- lib/domain/atom_item.dart | 4 ++++ lib/domain/geo/geo.dart | 25 +++++++++++++++++++++++++ lib/domain/rss_item.dart | 4 ++++ test/atom_test.dart | 3 +++ test/rss_test.dart | 3 +++ test/xml/Atom.xml | 4 ++++ test/xml/RSS.xml | 4 ++++ 7 files changed, 47 insertions(+) create mode 100644 lib/domain/geo/geo.dart diff --git a/lib/domain/atom_item.dart b/lib/domain/atom_item.dart index a5efa08..f994655 100644 --- a/lib/domain/atom_item.dart +++ b/lib/domain/atom_item.dart @@ -2,6 +2,7 @@ import 'package:webfeed/domain/atom_category.dart'; 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/geo/geo.dart'; import 'package:webfeed/domain/media/media.dart'; import 'package:webfeed/util/datetime.dart'; import 'package:webfeed/util/xml.dart'; @@ -22,6 +23,7 @@ class AtomItem { final String summary; final String rights; final Media media; + final Geo geo; AtomItem({ this.id, @@ -37,6 +39,7 @@ class AtomItem { this.summary, this.rights, this.media, + this.geo, }); factory AtomItem.parse(XmlElement element) { @@ -64,6 +67,7 @@ class AtomItem { summary: findFirstElement(element, 'summary')?.text, rights: findFirstElement(element, 'rights')?.text, media: Media.parse(element), + geo: Geo.parse(element), ); } } diff --git a/lib/domain/geo/geo.dart b/lib/domain/geo/geo.dart new file mode 100644 index 0000000..2df7f30 --- /dev/null +++ b/lib/domain/geo/geo.dart @@ -0,0 +1,25 @@ +import 'package:webfeed/util/xml.dart'; +import 'package:xml/xml.dart'; + +class Geo { + final double lat; + final double long; + + Geo(this.lat, this.long); + + factory Geo.parse(XmlElement element) { + if (element == null) { + return null; + } + + var lat = double.tryParse(findFirstElement(element, 'geo:lat')?.text ?? ''); + var long = + double.tryParse(findFirstElement(element, 'geo:long')?.text ?? ''); + + if (lat == null || long == null) { + return null; + } else { + return Geo(lat, long); + } + } +} diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index 15fa909..6f39a71 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,4 +1,5 @@ import 'package:webfeed/domain/dublin_core/dublin_core.dart'; +import 'package:webfeed/domain/geo/geo.dart'; import 'package:webfeed/domain/itunes/itunes.dart'; import 'package:webfeed/domain/media/media.dart'; import 'package:webfeed/domain/rss_category.dart'; @@ -25,6 +26,7 @@ class RssItem { final RssEnclosure enclosure; final DublinCore dc; final Itunes itunes; + final Geo geo; RssItem({ this.title, @@ -41,6 +43,7 @@ class RssItem { this.enclosure, this.dc, this.itunes, + this.geo, }); factory RssItem.parse(XmlElement element) { @@ -62,6 +65,7 @@ class RssItem { enclosure: RssEnclosure.parse(findFirstElement(element, 'enclosure')), dc: DublinCore.parse(element), itunes: Itunes.parse(element), + geo: Geo.parse(element), ); } } diff --git a/test/atom_test.dart b/test/atom_test.dart index 1cd24fc..5d72534 100644 --- a/test/atom_test.dart +++ b/test/atom_test.dart @@ -87,6 +87,9 @@ void main() { expect(item.summary, 'This is summary 1'); expect(item.content, 'This is content 1'); expect(item.rights, 'This is rights 1'); + + expect(item.geo.lat, 45.3); + expect(item.geo.long, -0.5); }); test('parse Atom-Media.xml', () { var xmlString = File('test/xml/Atom-Media.xml').readAsStringSync(); diff --git a/test/rss_test.dart b/test/rss_test.dart index 44eb9b2..c59c495 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -89,6 +89,9 @@ void main() { ' Test content
'); expect( feed.items.first.content.images.first, 'https://test.com/image_link'); + + expect(feed.items.first.geo.lat, 45.3); + expect(feed.items.first.geo.long, -0.5); }); test('parse RSS-Media.xml', () { var xmlString = File('test/xml/RSS-Media.xml').readAsStringSync(); diff --git a/test/xml/Atom.xml b/test/xml/Atom.xml index 02f02bf..eda1f4a 100644 --- a/test/xml/Atom.xml +++ b/test/xml/Atom.xml @@ -72,6 +72,8 @@ This is summary 1 This is content 1 This is rights 1 + 45.3 + -0.5 foo-bar-entry-id-2 @@ -113,5 +115,7 @@ This is summary 2 This is content 2 This is rights 2 + 43.3 + 3.5 \ No newline at end of file diff --git a/test/xml/RSS.xml b/test/xml/RSS.xml index 07d80ae..5257862 100644 --- a/test/xml/RSS.xml +++ b/test/xml/RSS.xml @@ -49,6 +49,8 @@ https://foo.bar.news/1/comments Test content
]]>
+ 45.3 + -0.5 Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC @@ -62,6 +64,8 @@ bob@foo.bar.news Lorem Ipsum https://foo.bar.news/2/comments + 49.4 + 9.5 \ No newline at end of file