2009-11-23 12:46:12 +00:00
|
|
|
package feeder
|
|
|
|
|
2011-11-02 15:51: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) readRss2(doc *xmlx.Document) (err error) {
|
2010-12-17 23:25:16 +00:00
|
|
|
days := make(map[string]int)
|
|
|
|
days["Monday"] = 1
|
|
|
|
days["Tuesday"] = 2
|
|
|
|
days["Wednesday"] = 3
|
|
|
|
days["Thursday"] = 4
|
|
|
|
days["Friday"] = 5
|
|
|
|
days["Saturday"] = 6
|
|
|
|
days["Sunday"] = 7
|
|
|
|
|
2010-12-18 18:11:37 +00:00
|
|
|
getChan := func(pubdate, 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(pubdate) > 0:
|
|
|
|
if c.PubDate == pubdate {
|
|
|
|
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, pubdate, title, desc string) bool {
|
2010-12-17 23:25:16 +00:00
|
|
|
for _, item := range ch.Items {
|
|
|
|
switch {
|
2010-12-18 18:11:37 +00:00
|
|
|
case len(pubdate) > 0:
|
|
|
|
if item.PubDate == pubdate {
|
2010-12-17 23:25:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
case len(title) > 0:
|
|
|
|
if item.Title == title {
|
|
|
|
return true
|
|
|
|
}
|
2010-12-18 18:11:37 +00:00
|
|
|
case len(desc) > 0:
|
2010-12-17 23:25:16 +00:00
|
|
|
if item.Description == desc {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var ch *Channel
|
|
|
|
var i *Item
|
|
|
|
var n *xmlx.Node
|
|
|
|
var list, tl []*xmlx.Node
|
2011-02-01 14:30:39 +00:00
|
|
|
const ns = "*"
|
2010-12-17 23:25:16 +00:00
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
channels := doc.SelectNodes(ns, "channel")
|
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, "pubDate"), node.S(ns, "title")); ch == nil {
|
2010-12-17 23:25:16 +00:00
|
|
|
ch = new(Channel)
|
|
|
|
this.Channels = append(this.Channels, ch)
|
|
|
|
}
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Title = node.S(ns, "title")
|
2011-02-01 14:30:39 +00:00
|
|
|
list = node.SelectNodes(ns, "link")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Links = make([]Link, len(list))
|
2010-12-17 23:25:16 +00:00
|
|
|
|
2009-11-23 12:46:12 +00:00
|
|
|
for i, v := range list {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Links[i].Href = v.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Description = node.S(ns, "description")
|
|
|
|
ch.Language = node.S(ns, "language")
|
|
|
|
ch.Copyright = node.S(ns, "copyright")
|
|
|
|
ch.ManagingEditor = node.S(ns, "managingEditor")
|
|
|
|
ch.WebMaster = node.S(ns, "webMaster")
|
|
|
|
ch.PubDate = node.S(ns, "pubDate")
|
|
|
|
ch.LastBuildDate = node.S(ns, "lastBuildDate")
|
|
|
|
ch.Docs = node.S(ns, "docs")
|
2010-05-23 14:21:30 +00:00
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
list = node.SelectNodes(ns, "category")
|
2010-12-17 23:25:16 +00:00
|
|
|
ch.Categories = make([]*Category, len(list))
|
2009-11-23 12:46:12 +00:00
|
|
|
for i, v := range list {
|
2010-12-17 23:25:16 +00:00
|
|
|
ch.Categories[i] = new(Category)
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Categories[i].Domain = v.As(ns, "domain")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Categories[i].Text = v.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
if n = node.SelectNode(ns, "generator"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Generator = Generator{}
|
|
|
|
ch.Generator.Text = n.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.TTL = node.I(ns, "ttl")
|
|
|
|
ch.Rating = node.S(ns, "rating")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
list = node.SelectNodes(ns, "hour")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.SkipHours = make([]int, len(list))
|
2009-11-23 12:46:12 +00:00
|
|
|
for i, v := range list {
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.SkipHours[i] = v.I(ns, "hour")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
list = node.SelectNodes(ns, "days")
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.SkipDays = make([]int, len(list))
|
2009-11-23 12:46:12 +00:00
|
|
|
for i, v := range list {
|
2010-12-17 23:25:16 +00:00
|
|
|
ch.SkipDays[i] = days[v.Value]
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
if n = node.SelectNode(ns, "image"); n != nil {
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Image.Title = n.S(ns, "title")
|
|
|
|
ch.Image.Url = n.S(ns, "url")
|
|
|
|
ch.Image.Link = n.S(ns, "link")
|
|
|
|
ch.Image.Width = n.I(ns, "width")
|
|
|
|
ch.Image.Height = n.I(ns, "height")
|
|
|
|
ch.Image.Description = n.S(ns, "description")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
if n = node.SelectNode(ns, "cloud"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Cloud = Cloud{}
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.Cloud.Domain = n.As(ns, "domain")
|
|
|
|
ch.Cloud.Port = n.Ai(ns, "port")
|
|
|
|
ch.Cloud.Path = n.As(ns, "path")
|
|
|
|
ch.Cloud.RegisterProcedure = n.As(ns, "registerProcedure")
|
|
|
|
ch.Cloud.Protocol = n.As(ns, "protocol")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
if n = node.SelectNode(ns, "textInput"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.TextInput = Input{}
|
2011-05-11 15:40:56 +00:00
|
|
|
ch.TextInput.Title = n.S(ns, "title")
|
|
|
|
ch.TextInput.Description = n.S(ns, "description")
|
|
|
|
ch.TextInput.Name = n.S(ns, "name")
|
|
|
|
ch.TextInput.Link = n.S(ns, "link")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
itemcount := len(ch.Items)
|
2011-02-01 14:30:39 +00:00
|
|
|
list = node.SelectNodes(ns, "item")
|
2010-12-17 23:25:16 +00:00
|
|
|
|
2009-11-23 12:46:12 +00:00
|
|
|
for _, item := range list {
|
2011-05-11 15:40:56 +00:00
|
|
|
if haveItem(ch, item.S(ns, "pubDate"),
|
|
|
|
item.S(ns, "title"), item.S(ns, "description")) {
|
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.Description = item.S(ns, "description")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2011-08-26 07:50:14 +00:00
|
|
|
tl = item.SelectNodes(ns, "link")
|
2010-12-17 23:25:16 +00:00
|
|
|
for _, v := range tl {
|
|
|
|
lnk := new(Link)
|
2010-05-23 14:21:30 +00:00
|
|
|
lnk.Href = v.Value
|
2010-12-17 20:57:48 +00:00
|
|
|
i.Links = append(i.Links, lnk)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
if n = item.SelectNode(ns, "author"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
i.Author = Author{}
|
|
|
|
i.Author.Name = n.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
2013-03-28 03:05:27 +00:00
|
|
|
i.Author.Name = item.S(ns, "creator")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2011-05-11 15:40:56 +00:00
|
|
|
i.Comments = item.S(ns, "comments")
|
|
|
|
i.Guid = item.S(ns, "guid")
|
|
|
|
i.PubDate = item.S(ns, "pubDate")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
tl = item.SelectNodes(ns, "category")
|
2010-12-17 23:25:16 +00:00
|
|
|
for _, lv := range tl {
|
|
|
|
cat := new(Category)
|
2011-05-11 15:40:56 +00:00
|
|
|
cat.Domain = lv.As(ns, "domain")
|
2010-12-17 23:25:16 +00:00
|
|
|
cat.Text = lv.Value
|
|
|
|
i.Categories = append(i.Categories, cat)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
tl = item.SelectNodes(ns, "enclosure")
|
2010-12-17 23:25:16 +00:00
|
|
|
for _, lv := range tl {
|
|
|
|
enc := new(Enclosure)
|
2011-05-11 15:40:56 +00:00
|
|
|
enc.Url = lv.As(ns, "url")
|
|
|
|
enc.Length = lv.Ai64(ns, "length")
|
|
|
|
enc.Type = lv.As(ns, "type")
|
2010-12-17 23:25:16 +00:00
|
|
|
i.Enclosures = append(i.Enclosures, enc)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2011-02-01 14:30:39 +00:00
|
|
|
if src := item.SelectNode(ns, "source"); src != nil {
|
2010-12-17 23:25:16 +00:00
|
|
|
i.Source = new(Source)
|
2011-05-11 15:40:56 +00:00
|
|
|
i.Source.Url = src.As(ns, "url")
|
2010-05-23 14:21:30 +00:00
|
|
|
i.Source.Text = src.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +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
|
|
|
|
}
|