diff --git a/channel.go b/channel.go index fe3318b..dc3104f 100644 --- a/channel.go +++ b/channel.go @@ -21,6 +21,7 @@ type Channel struct { Items []*Item Cloud Cloud TextInput Input + Extensions map[string]map[string][]Extension // Atom fields Id string diff --git a/feed_test.go b/feed_test.go index b0e74a4..950d8d1 100644 --- a/feed_test.go +++ b/feed_test.go @@ -76,6 +76,61 @@ func Test_RssAuthor(t *testing.T) { } } +func Test_ItemExtensions(t *testing.T) { + content, _ := ioutil.ReadFile("testdata/extension.rss") + feed := New(1, true, chanHandler, itemHandler) + feed.FetchBytes("http://example.com", content, nil) + + edgarExtensionxbrlFiling := feed.Channels[0].Items[0].Extensions["http://www.sec.gov/Archives/edgar"]["xbrlFiling"][0].Childrens + companyExpected := "Cellular Biomedicine Group, Inc." + companyName := edgarExtensionxbrlFiling["companyName"][0] + if companyName.Value != companyExpected { + t.Errorf("Expected company to be %s but found %s", companyExpected, companyName.Value) + } + + files := edgarExtensionxbrlFiling["xbrlFiles"][0].Childrens["xbrlFile"] + fileSizeExpected := 10 + if len(files) != 10 { + t.Errorf("Expected files size to be %s but found %s", fileSizeExpected, len(files)) + } + + file := files[0] + fileExpected := "cbmg_10qa.htm" + if file.Attrs["file"] != fileExpected { + t.Errorf("Expected file to be %s but found %s", fileExpected, len(file.Attrs["file"])) + } +} + +func Test_ChannelExtensions(t *testing.T) { + content, _ := ioutil.ReadFile("testdata/extension.rss") + feed := New(1, true, chanHandler, itemHandler) + feed.FetchBytes("http://example.com", content, nil) + + channel := feed.Channels[0] + itunesExtentions := channel.Extensions["http://www.itunes.com/dtds/podcast-1.0.dtd"] + + authorExptected := "The Author" + ownerEmailExpected := "test@rss.com" + categoryExpected := "Politics" + imageExptected := "http://golang.org/doc/gopher/project.png" + + if itunesExtentions["author"][0].Value != authorExptected { + t.Errorf("Expected author to be %s but found %s", authorExptected, itunesExtentions["author"][0].Value) + } + + if itunesExtentions["owner"][0].Childrens["email"][0].Value != ownerEmailExpected { + t.Errorf("Expected owner email to be %s but found %s", ownerEmailExpected, itunesExtentions["owner"][0].Childrens["email"][0].Value) + } + + if itunesExtentions["category"][0].Attrs["text"] != categoryExpected { + t.Errorf("Expected category text to be %s but found %s", categoryExpected, itunesExtentions["category"][0].Attrs["text"]) + } + + if itunesExtentions["image"][0].Attrs["href"] != imageExptected { + t.Errorf("Expected image href to be %s but found %s", imageExptected, itunesExtentions["image"][0].Attrs["href"]) + } +} + func Test_CData(t *testing.T) { content, _ := ioutil.ReadFile("testdata/iosBoardGameGeek.rss") feed := New(1, true, chanHandler, itemHandler) diff --git a/item.go b/item.go index ea13f85..74045e4 100644 --- a/item.go +++ b/item.go @@ -23,6 +23,8 @@ type Item struct { Generator *Generator Contributors []string Content *Content + + Extensions map[string]map[string][]Extension } func (i *Item) Key() string { diff --git a/rss.go b/rss.go index 1a81316..1c347ca 100644 --- a/rss.go +++ b/rss.go @@ -6,6 +6,13 @@ import ( xmlx "github.com/jteeuwen/go-pkg-xmlx" ) +type Extension struct { + Name string + Value string + Attrs map[string]string + Childrens map[string][]Extension +} + var days = map[string]int{ "Monday": 1, "Tuesday": 2, @@ -189,9 +196,62 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) { } } + tl = item.SelectNodes(ns, ns) + i.Extensions = make(map[string]map[string][]Extension) + for _, lv := range tl { + getExtensions(&i.Extensions, lv) + } + ch.Items = append(ch.Items, i) } + + x := node.SelectNodes(ns, ns) + ch.Extensions = make(map[string]map[string][]Extension) + for _, v := range x { + if v.Name.Space != "" { + getExtensions(&ch.Extensions, v) + } + } + } this.Channels = foundChannels return } + +func getExtensions(extensionsX *map[string]map[string][]Extension, node *xmlx.Node) { + extentions := *extensionsX + + if node.Name.Space != "" { + extensione, noErrors := getExtension(node) + if noErrors { + if len(extentions[node.Name.Space]) == 0 { + extentions[node.Name.Space] = make(map[string][]Extension, 0) + } + if len(extentions[node.Name.Space][node.Name.Local]) == 0 { + extentions[node.Name.Space][node.Name.Local] = make([]Extension, 0) + } + extentions[node.Name.Space][node.Name.Local] = append(extentions[node.Name.Space][node.Name.Local], extensione) + } + } +} + +func getExtension(node *xmlx.Node) (Extension, bool) { + var extension Extension + if node.Name.Space != "" { + extension = Extension{Name: node.Name.Local, Value: node.GetValue()} + extension.Attrs = make(map[string]string) + extension.Childrens = make(map[string][]Extension, 0) + for _, x := range node.Attributes { + extension.Attrs[x.Name.Local] = x.Value + } + for _, y := range node.Children { + children, ok := getExtension(y) + if ok { + extension.Childrens[y.Name.Local] = append(extension.Childrens[y.Name.Local], children) + } + } + return extension, true + } else { + return extension, false + } +} diff --git a/testdata/extension.rss b/testdata/extension.rss new file mode 100644 index 0000000..3d7c255 --- /dev/null +++ b/testdata/extension.rss @@ -0,0 +1,55 @@ + + + + Extensions Test + http://test.extensions.net + Just a Test + en-US + Wed, 27 Mar 2013 12:30:18 PDT + + http://test.extensions.net + http://test.extensions.net/test.jpg + Extensions Test + + The Author + + + test@rss.com + + + + + Cellular Biomedicine Group, Inc. (0001378624) (Filer) + http://www.sec.gov/Archives/edgar/data/1378624/000135448813006749/0001354488-13-006749-index.htm + http://www.sec.gov/Archives/edgar/data/1378624/000135448813006749/0001354488-13-006749-xbrl.zip + + 10-Q/A + Fri, 06 Dec 2013 17:29:22 EST + + Cellular Biomedicine Group, Inc. + 10-Q/A + 12/06/2013 + 0001378624 + 0001354488-13-006749 + 000-52282 + 20131206172922 + 20130630 + 7 + 6199 + 1231 + + + + + + + + + + + + + + + +