Merge pull request #25 from haarts/master

Prevent re-adding Atom entries
This commit is contained in:
jimt 2013-12-02 08:27:13 -08:00
commit b4cf92a0fc
4 changed files with 54 additions and 0 deletions

20
atom.go
View File

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

View File

@ -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.

8
testdata/initial.atom vendored Normal file
View File

@ -0,0 +1,8 @@
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Some title</title>
<id>http://www.example.com/feed/atom/</id>
<entry>
<title>First title</title>
<id>1</id>
</entry>
</feed>

12
testdata/initial_plus_one_new.atom vendored Normal file
View File

@ -0,0 +1,12 @@
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Some title</title>
<id>http://www.example.com/feed/atom/</id>
<entry>
<title>First title</title>
<id>1</id>
</entry>
<entry>
<title>Second title</title>
<id>2</id>
</entry>
</feed>