From 997070656fc003f824aefa3f3f2840dd39c8318f Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Mon, 2 Dec 2013 14:24:57 +0100 Subject: [PATCH 1/3] Adds new test data --- testdata/initial.atom | 8 ++++++++ testdata/initial_plus_one_new.atom | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 testdata/initial.atom create mode 100644 testdata/initial_plus_one_new.atom diff --git a/testdata/initial.atom b/testdata/initial.atom new file mode 100644 index 0000000..b086591 --- /dev/null +++ b/testdata/initial.atom @@ -0,0 +1,8 @@ + + Some title + http://www.example.com/feed/atom/ + + First title + 1 + + diff --git a/testdata/initial_plus_one_new.atom b/testdata/initial_plus_one_new.atom new file mode 100644 index 0000000..2461a66 --- /dev/null +++ b/testdata/initial_plus_one_new.atom @@ -0,0 +1,12 @@ + + Some title + http://www.example.com/feed/atom/ + + First title + 1 + + + Second title + 2 + + From b311fe3f8dab10a6c8811dcc2204b7c156d8b537 Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Mon, 2 Dec 2013 14:25:13 +0100 Subject: [PATCH 2/3] Adds test --- feed_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/feed_test.go b/feed_test.go index fad7b7f..c069660 100644 --- a/feed_test.go +++ b/feed_test.go @@ -7,6 +7,20 @@ import ( var items []*Item +func Test_NewItem(t *testing.T) { + content, _ := ioutil.ReadFile("testdata/initial.atom") + feed := New(1, true, chanHandler, itemHandler) + err := feed.FetchBytes("http://example.com", content, nil) + if err != nil { t.Error(err) } + + content, _ = ioutil.ReadFile("testdata/initial_plus_one_new.atom") + feed.FetchBytes("http://example.com", content, nil) + expected := "Second title" + if expected != items[0].Title { + t.Errorf("Expected %s, got %s", expected, items[0].Title) + } +} + func TestFeed(t *testing.T) { urilist := []string{ //"http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", // Non-utf8 encoding. From d79101645df6026cdbe48b07307fe101413a7729 Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Mon, 2 Dec 2013 14:25:45 +0100 Subject: [PATCH 3/3] Implements check if item is already present --- atom.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/atom.go b/atom.go index ccbe786..08808a6 100644 --- a/atom.go +++ b/atom.go @@ -71,6 +71,10 @@ func (this *Feed) readAtom(doc *xmlx.Document) (err error) { list = node.SelectNodes(ns, "entry") for _, item := range list { + if isItemPresent(ch, item.S(ns, "id"), item.S(ns, "title")) { + continue + } + i = new(Item) i.Title = item.S(ns, "title") i.Id = item.S(ns, "id") @@ -123,3 +127,19 @@ func (this *Feed) readAtom(doc *xmlx.Document) (err error) { } return } + +func isItemPresent(ch *Channel, id, title string) bool { + for _, item := range ch.Items { + switch { + case len(id) > 0: + if item.Id == id { + return true + } + case len(title) > 0: + if item.Title == title { + return true + } + } + } + return false +}