Merge pull request #33 from dukex/bugfix/ignored-link

Fixes atom ignored link
This commit is contained in:
jimt 2014-01-18 12:15:33 -08:00
commit 1ade55aa42
4 changed files with 107 additions and 2 deletions

View File

@ -88,6 +88,26 @@ func Test_CData(t *testing.T) {
} }
} }
func Test_Link(t *testing.T) {
content, _ := ioutil.ReadFile("testdata/nytimes.rss")
feed := New(1, true, chanHandler, itemHandler)
feed.FetchBytes("http://example.com", content, nil)
channel := feed.Channels[0]
item := channel.Items[0]
channelLinkExpected := "http://www.nytimes.com/services/xml/rss/nyt/US.xml"
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 {
t.Errorf("Expected author to be %s but found %s", channelLinkExpected, channel.Links[0].Href)
}
if item.Links[0].Href != itemLinkExpected {
t.Errorf("Expected author to be %s but found %s", itemLinkExpected, item.Links[0].Href)
}
}
func chanHandler(feed *Feed, newchannels []*Channel) { func chanHandler(feed *Feed, newchannels []*Channel) {
println(len(newchannels), "new channel(s) in", feed.Url) println(len(newchannels), "new channel(s) in", feed.Url)
} }

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

68
testdata/ignoredLink.rss vendored Normal file
View File

@ -0,0 +1,68 @@
<rss version="2.0">
<channel>
<title>Conservatives.com News</title>
<link>http://www.conservatives.com/XMLGateway/RSS/News.xml</link>
<description>Conservatives.com news headlines (RSS 2.0)</description>
<language>en-gb</language>
<item>
<title>Dr Tania Mathias chosen to stand up for local people in Twickenham</title>
<link>http://www.conservatives.com/News/News_stories/2013/09/Dr_Tania_Mathias_chosen_to_stand_up_for_local_people_in_Twickenham.aspx</link>
<description>Last night local residents of Twickenham attended an Open Primary and selected local Twickenham councilor Dr Tania Mathias to be their Conservative candidate for the Twickenham Constituency at the General Election in 2015.</description>
<pubDate>Fri, 06 Sep 2013 17:07:00 GMT</pubDate>
</item>
<item>
<title>Cabinet Ministers warn of Labour tax bombshell on family holidays</title>
<link>http://www.conservatives.com/News/News_stories/2013/08/Cabinet_Ministers_warn_of_Labour_tax_bombshell_on_family_holidays.aspx</link>
<description>Labour want new taxes on local hotels and B and Bs say Maria Miller and Eric Pickles</description>
<pubDate>Fri, 23 Aug 2013 11:52:00 GMT</pubDate>
</item>
<item>
<title>Results of MEP ballot announced</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Conservatives_announce_results_of_MEP_ballot.aspx</link>
<description>Over the last month, Conservative members have been balloted to choose the candidates and their position on the ballot paper for next years European Elections.</description>
<pubDate>Wed, 31 Jul 2013 15:44:00 GMT</pubDate>
</item>
<item>
<title>Marcus Fysh selected for Yeovil</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Marcus_Fysh_selected_for_Yeovil.aspx</link>
<description>Marcus Fysh chosen to stand up for local people in Yeovil</description>
<pubDate>Wed, 31 Jul 2013 14:13:00 GMT</pubDate>
</item>
<item>
<title>Clarence Mitchell selected for Brighton Pavilion</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Clarence_Mitchell_selected_for_Brighton_and_Hove.aspx</link>
<description>Brighton and Hove Conservatives have selected Clarence Mitchell as their Prospective Parliamentary Candidate for Brighton Pavilion in the 2015 General Election.</description>
<pubDate>Tue, 30 Jul 2013 11:14:00 GMT</pubDate>
</item>
<item>
<title>Ann Myatt selected for Westmorland and Lonsdale</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Ann_Myatt_selected_for_Westmorland_and_Lonsdale.aspx</link>
<description>Westmorland and Lonsdale Conservatives have selected Ann Myatt as their Prospective Parliamentary candidate for the 2015 General Election.</description>
<pubDate>Mon, 29 Jul 2013 11:20:00 GMT</pubDate>
</item>
<item>
<title>Robert Alden selected for Birmingham Erdington</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Robert_Alden_selected_for_Birmingham_Erdington.aspx</link>
<description>Erdington Conservative Association is delighted to announce that popular local resident Cllr Robert Alden has been selected to stand at the next general election.</description>
<pubDate>Mon, 29 Jul 2013 10:52:00 GMT</pubDate>
</item>
<item>
<title>Matthew Maxwell Scott selected for Carshalton &amp; Wallington</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Matthew_Maxwell_Scott_selected_for_Carshalton_Wallington.aspx</link>
<description>Carshalton and Wallington Conservatives have selected Matthew Maxwell Scott as their Prospective Parliamentary Candidate for the 2015 General Election. </description>
<pubDate>Fri, 19 Jul 2013 15:14:00 GMT</pubDate>
</item>
<item>
<title>Crime fallen by 10% under this government</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Crime_fallen_by_10_under_this_government.aspx</link>
<description>Recorded crime is down by more than ten per cent under this government, and the independent survey shows that the publics experience of crime is at its lowest level since records began.</description>
<pubDate>Thu, 18 Jul 2013 16:42:00 GMT</pubDate>
</item>
<item>
<title>Unemployment down by 57k in last quarter</title>
<link>http://www.conservatives.com/News/News_stories/2013/07/Unemployment_down_by_57k_in_last_quarter.aspx</link>
<description>Unemployment is down and fewer people are claiming Jobseeker's Allowance than in May 2010</description>
<pubDate>Wed, 17 Jul 2013 10:04:00 GMT</pubDate>
</item>
</channel>
</rss>

2
testdata/nytimes.rss vendored Normal file

File diff suppressed because one or more lines are too long