2009-11-23 12:46:12 +00:00
|
|
|
package feeder
|
|
|
|
|
2011-01-20 21:57:04 +00:00
|
|
|
import xmlx "github.com/jteeuwen/go-pkg-xmlx"
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2011-11-02 15:51:04 +00:00
|
|
|
func (this *Feed) readAtom(doc *xmlx.Document) (err error) {
|
2010-05-23 14:21:30 +00:00
|
|
|
ns := "http://www.w3.org/2005/Atom"
|
|
|
|
channels := doc.SelectNodes(ns, "feed")
|
2010-12-17 23:25:16 +00:00
|
|
|
|
2010-12-18 18:11:37 +00:00
|
|
|
getChan := func(id, title string) *Channel {
|
2010-12-17 23:25:16 +00:00
|
|
|
for _, c := range this.Channels {
|
2010-12-18 18:11:37 +00:00
|
|
|
switch {
|
|
|
|
case len(id) > 0:
|
|
|
|
if c.Id == id {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
case len(title) > 0:
|
|
|
|
if c.Title == title {
|
|
|
|
return c
|
|
|
|
}
|
2010-12-17 23:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:11:37 +00:00
|
|
|
haveItem := func(ch *Channel, id, title, desc string) bool {
|
2010-12-17 23:25:16 +00:00
|
|
|
for _, item := range ch.Items {
|
2010-12-18 18:11:37 +00:00
|
|
|
switch {
|
|
|
|
case len(id) > 0:
|
|
|
|
if item.Id == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case len(title) > 0:
|
|
|
|
if item.Title == title {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case len(desc) > 0:
|
|
|
|
if item.Description == desc {
|
|
|
|
return true
|
|
|
|
}
|
2010-12-17 23:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var ch *Channel
|
|
|
|
var i *Item
|
|
|
|
var tn *xmlx.Node
|
|
|
|
var list []*xmlx.Node
|
|
|
|
|
2009-11-23 12:46:12 +00:00
|
|
|
for _, node := range channels {
|
2011-05-11 15:40:56 +00:00
|
|
|
if ch = getChan(node.S(ns, "id"), node.S(ns, "title")); ch == nil {
|
2010-12-17 23:25:16 +00:00
|
|
|
ch = new(Channel)
|
|
|
|
this.Channels = append(this.Channels, ch)
|
|
|
|
}
|
|
|
|
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Title = node.S(ns, "title")
|
|
|
|
ch.LastBuildDate = node.S(ns, "updated")
|
|
|
|
ch.Id = node.S(ns, "id")
|
|
|
|
ch.Rights = node.S(ns, "rights")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
list = node.SelectNodes(ns, "link")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Links = make([]Link, len(list))
|
2009-11-23 12:46:12 +00:00
|
|
|
for i, v := range list {
|
2011-05-11 15:40:56 +00:00
|
|
|
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")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
if tn = node.SelectNode(ns, "subtitle"); tn != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.SubTitle = SubTitle{}
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.SubTitle.Type = tn.As("", "type")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.SubTitle.Text = tn.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +00:00
|
|
|
if tn = node.SelectNode(ns, "generator"); tn != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Generator = Generator{}
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Generator.Uri = tn.As("", "uri")
|
|
|
|
ch.Generator.Version = tn.As("", "version")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Generator.Text = tn.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +00:00
|
|
|
if tn = node.SelectNode(ns, "author"); tn != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Author = Author{}
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Author.Name = tn.S("", "name")
|
|
|
|
ch.Author.Uri = tn.S("", "uri")
|
|
|
|
ch.Author.Email = tn.S("", "email")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
itemcount := len(ch.Items)
|
2010-05-23 14:21:30 +00:00
|
|
|
list = node.SelectNodes(ns, "entry")
|
2010-12-17 23:25:16 +00:00
|
|
|
|
|
|
|
for _, item := range list {
|
2011-05-11 15:40:56 +00:00
|
|
|
if haveItem(ch, item.S(ns, "id"), item.S(ns, "title"), item.S(ns, "summary")) {
|
2010-12-17 23:25:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
i = new(Item)
|
2011-05-11 15:40:56 +00:00
|
|
|
i.Title = item.S(ns, "title")
|
|
|
|
i.Id = item.S(ns, "id")
|
|
|
|
i.PubDate = item.S(ns, "updated")
|
|
|
|
i.Description = item.S(ns, "summary")
|
2010-12-17 23:25:16 +00:00
|
|
|
|
|
|
|
links := item.SelectNodes(ns, "link")
|
|
|
|
for _, lv := range links {
|
2012-02-29 09:48:17 +00:00
|
|
|
if lv.As(ns, "rel") == "enclosure" {
|
2010-12-17 23:25:16 +00:00
|
|
|
enc := new(Enclosure)
|
2011-05-11 15:40:56 +00:00
|
|
|
enc.Url = lv.As("", "href")
|
|
|
|
enc.Type = lv.As("", "type")
|
2010-12-17 23:25:16 +00:00
|
|
|
i.Enclosures = append(i.Enclosures, enc)
|
2009-11-23 12:46:12 +00:00
|
|
|
} else {
|
2010-12-17 23:25:16 +00:00
|
|
|
lnk := new(Link)
|
2011-05-11 15:40:56 +00:00
|
|
|
lnk.Href = lv.As("", "href")
|
|
|
|
lnk.Rel = lv.As("", "rel")
|
|
|
|
lnk.Type = lv.As("", "type")
|
|
|
|
lnk.HrefLang = lv.As("", "hreflang")
|
2010-12-17 23:25:16 +00:00
|
|
|
i.Links = append(i.Links, lnk)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
list = item.SelectNodes(ns, "contributor")
|
|
|
|
for _, cv := range list {
|
2011-05-11 15:40:56 +00:00
|
|
|
i.Contributors = append(i.Contributors, cv.S("", "name"))
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
if tn = item.SelectNode(ns, "content"); tn != nil {
|
|
|
|
i.Content = new(Content)
|
2011-05-11 15:40:56 +00:00
|
|
|
i.Content.Type = tn.As("", "type")
|
|
|
|
i.Content.Lang = tn.S("xml", "lang")
|
|
|
|
i.Content.Base = tn.S("xml", "base")
|
2010-12-17 23:25:16 +00:00
|
|
|
i.Content.Text = tn.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
2010-12-17 23:25:16 +00:00
|
|
|
|
|
|
|
ch.Items = append(ch.Items, i)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
if itemcount != len(ch.Items) && this.itemhandler != nil {
|
|
|
|
this.itemhandler(this, ch, ch.Items[itemcount:])
|
|
|
|
}
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|