Merge pull request #25 from haarts/master
Prevent re-adding Atom entries
This commit is contained in:
		
						commit
						b4cf92a0fc
					
				
					 4 changed files with 54 additions and 0 deletions
				
			
		
							
								
								
									
										20
									
								
								atom.go
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								atom.go
									
										
									
									
									
								
							| 
						 | 
					@ -71,6 +71,10 @@ func (this *Feed) readAtom(doc *xmlx.Document) (err error) {
 | 
				
			||||||
		list = node.SelectNodes(ns, "entry")
 | 
							list = node.SelectNodes(ns, "entry")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for _, item := range list {
 | 
							for _, item := range list {
 | 
				
			||||||
 | 
								if isItemPresent(ch, item.S(ns, "id"), item.S(ns, "title")) {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			i = new(Item)
 | 
								i = new(Item)
 | 
				
			||||||
			i.Title = item.S(ns, "title")
 | 
								i.Title = item.S(ns, "title")
 | 
				
			||||||
			i.Id = item.S(ns, "id")
 | 
								i.Id = item.S(ns, "id")
 | 
				
			||||||
| 
						 | 
					@ -123,3 +127,19 @@ func (this *Feed) readAtom(doc *xmlx.Document) (err error) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										14
									
								
								feed_test.go
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								feed_test.go
									
										
									
									
									
								
							| 
						 | 
					@ -7,6 +7,20 @@ import (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var items []*Item
 | 
					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) {
 | 
					func TestFeed(t *testing.T) {
 | 
				
			||||||
	urilist := []string{
 | 
						urilist := []string{
 | 
				
			||||||
		//"http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", // Non-utf8 encoding.
 | 
							//"http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", // Non-utf8 encoding.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										8
									
								
								testdata/initial.atom
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								testdata/initial.atom
									
										
									
									
										vendored
									
									
										Normal 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
									
								
							
							
						
						
									
										12
									
								
								testdata/initial_plus_one_new.atom
									
										
									
									
										vendored
									
									
										Normal 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>
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue