fixed links when rss has atom:link e link tag [#21]

This commit is contained in:
Duke 2014-01-18 15:41:26 -02:00
parent a3882284e0
commit 6f578a1273
3 changed files with 22 additions and 5 deletions

View File

@ -89,15 +89,15 @@ func Test_CData(t *testing.T) {
} }
func Test_Link(t *testing.T) { func Test_Link(t *testing.T) {
content, _ := ioutil.ReadFile("testdata/ignoredLink.rss") content, _ := ioutil.ReadFile("testdata/nytimes.rss")
feed := New(1, true, chanHandler, itemHandler) feed := New(1, true, chanHandler, itemHandler)
feed.FetchBytes("http://example.com", content, nil) feed.FetchBytes("http://example.com", content, nil)
channel := feed.Channels[0] channel := feed.Channels[0]
item := channel.Items[0] item := channel.Items[0]
channelLinkExpected := "http://www.conservatives.com/XMLGateway/RSS/News.xml" channelLinkExpected := "http://www.nytimes.com/services/xml/rss/nyt/US.xml"
itemLinkExpected := "http://www.conservatives.com/News/News_stories/2013/09/Dr_Tania_Mathias_chosen_to_stand_up_for_local_people_in_Twickenham.aspx" itemLinkExpected := "http://www.nytimes.com/2014/01/18/technology/in-keeping-grip-on-data-pipeline-obama-does-little-to-reassure-industry.html?partner=rss&emc=rss"
if channel.Links[0].Href != channelLinkExpected { if channel.Links[0].Href != channelLinkExpected {
t.Errorf("Expected author to be %s but found %s", channelLinkExpected, channel.Links[0].Href) t.Errorf("Expected author to be %s but found %s", channelLinkExpected, channel.Links[0].Href)

15
rss.go
View File

@ -43,8 +43,15 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) {
ch.Links = make([]Link, len(list)) ch.Links = make([]Link, len(list))
for i, v := range list { for i, v := range list {
if v.Name.Space == "http://www.w3.org/2005/Atom" && v.Name.Local == "link" {
ch.Links[i].Href = v.As("", "href")
ch.Links[i].Rel = v.As("", "rel")
ch.Links[i].Type = v.As("", "type")
ch.Links[i].HrefLang = v.As("", "hreflang")
} else {
ch.Links[i].Href = v.GetValue() ch.Links[i].Href = v.GetValue()
} }
}
ch.Description = node.S(ns, "description") ch.Description = node.S(ns, "description")
ch.Language = node.S(ns, "language") ch.Language = node.S(ns, "language")
@ -122,7 +129,15 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) {
tl = item.SelectNodes(ns, "link") tl = item.SelectNodes(ns, "link")
for _, v := range tl { for _, v := range tl {
lnk := new(Link) lnk := new(Link)
if v.Name.Space == "http://www.w3.org/2005/Atom" && v.Name.Local == "link" {
lnk.Href = v.As("", "href")
lnk.Rel = v.As("", "rel")
lnk.Type = v.As("", "type")
lnk.HrefLang = v.As("", "hreflang")
} else {
lnk.Href = v.GetValue() lnk.Href = v.GetValue()
}
i.Links = append(i.Links, lnk) i.Links = append(i.Links, lnk)
} }

2
testdata/nytimes.rss vendored Normal file

File diff suppressed because one or more lines are too long