2009-11-23 12:46:12 +00:00
|
|
|
package feeder
|
|
|
|
|
|
|
|
import "os"
|
2011-01-20 21:57:04 +00:00
|
|
|
import xmlx "github.com/jteeuwen/go-pkg-xmlx"
|
2009-11-23 12:46:12 +00:00
|
|
|
|
|
|
|
func (this *Feed) readRss2(doc *xmlx.Document) (err os.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
|
|
|
|
|
2010-05-23 14:21:30 +00:00
|
|
|
channels := doc.SelectNodes("", "channel")
|
2009-11-23 12:46:12 +00:00
|
|
|
for _, node := range channels {
|
2010-12-18 18:11:37 +00:00
|
|
|
if ch = getChan(node.GetValue("", "pubDate"), node.GetValue("", "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
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
ch.Title = node.GetValue("", "title")
|
|
|
|
list = node.SelectNodes("", "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
|
|
|
}
|
|
|
|
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Description = node.GetValue("", "description")
|
|
|
|
ch.Language = node.GetValue("", "language")
|
|
|
|
ch.Copyright = node.GetValue("", "copyright")
|
|
|
|
ch.ManagingEditor = node.GetValue("", "managingEditor")
|
|
|
|
ch.WebMaster = node.GetValue("", "webMaster")
|
|
|
|
ch.PubDate = node.GetValue("", "pubDate")
|
|
|
|
ch.LastBuildDate = node.GetValue("", "lastBuildDate")
|
|
|
|
ch.Docs = node.GetValue("", "docs")
|
|
|
|
|
|
|
|
list = node.SelectNodes("", "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)
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Categories[i].Domain = v.GetAttr("", "domain")
|
|
|
|
ch.Categories[i].Text = v.Value
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
if n = node.SelectNode("", "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
|
|
|
}
|
|
|
|
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.TTL = node.GetValuei("", "ttl")
|
|
|
|
ch.Rating = node.GetValue("", "rating")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2010-05-23 14:21:30 +00:00
|
|
|
list = node.SelectNodes("", "hour")
|
|
|
|
ch.SkipHours = make([]int, len(list))
|
2009-11-23 12:46:12 +00:00
|
|
|
for i, v := range list {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.SkipHours[i] = int(v.GetValuei("", "hour"))
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-05-23 14:21:30 +00:00
|
|
|
list = node.SelectNodes("", "days")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +00:00
|
|
|
if n = node.SelectNode("", "image"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Image.Title = n.GetValue("", "title")
|
|
|
|
ch.Image.Url = n.GetValue("", "url")
|
|
|
|
ch.Image.Link = n.GetValue("", "link")
|
|
|
|
ch.Image.Width = n.GetValuei("", "width")
|
|
|
|
ch.Image.Height = n.GetValuei("", "height")
|
|
|
|
ch.Image.Description = n.GetValue("", "description")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +00:00
|
|
|
if n = node.SelectNode("", "cloud"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.Cloud = Cloud{}
|
|
|
|
ch.Cloud.Domain = n.GetAttr("", "domain")
|
|
|
|
ch.Cloud.Port = n.GetAttri("", "port")
|
|
|
|
ch.Cloud.Path = n.GetAttr("", "path")
|
|
|
|
ch.Cloud.RegisterProcedure = n.GetAttr("", "registerProcedure")
|
|
|
|
ch.Cloud.Protocol = n.GetAttr("", "protocol")
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +00:00
|
|
|
if n = node.SelectNode("", "textInput"); n != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
ch.TextInput = Input{}
|
|
|
|
ch.TextInput.Title = n.GetValue("", "title")
|
|
|
|
ch.TextInput.Description = n.GetValue("", "description")
|
|
|
|
ch.TextInput.Name = n.GetValue("", "name")
|
|
|
|
ch.TextInput.Link = n.GetValue("", "link")
|
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("", "item")
|
2010-12-17 23:25:16 +00:00
|
|
|
|
2009-11-23 12:46:12 +00:00
|
|
|
for _, item := range list {
|
2010-12-17 23:25:16 +00:00
|
|
|
if haveItem(ch, item.GetValue("", "pubDate"),
|
|
|
|
item.GetValue("", "title"), item.GetValue("", "description")) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
i = new(Item)
|
2010-05-23 14:21:30 +00:00
|
|
|
i.Title = item.GetValue("", "title")
|
|
|
|
i.Description = item.GetValue("", "description")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
tl = node.SelectNodes("", "link")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-17 20:57:48 +00:00
|
|
|
if n = item.SelectNode("", "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
|
|
|
}
|
|
|
|
|
2010-05-23 14:21:30 +00:00
|
|
|
i.Comments = item.GetValue("", "comments")
|
|
|
|
i.Guid = item.GetValue("", "guid")
|
|
|
|
i.PubDate = item.GetValue("", "pubDate")
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
tl = item.SelectNodes("", "category")
|
|
|
|
for _, lv := range tl {
|
|
|
|
cat := new(Category)
|
|
|
|
cat.Domain = lv.GetAttr("", "domain")
|
|
|
|
cat.Text = lv.Value
|
|
|
|
i.Categories = append(i.Categories, cat)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
tl = item.SelectNodes("", "enclosure")
|
|
|
|
for _, lv := range tl {
|
|
|
|
enc := new(Enclosure)
|
|
|
|
enc.Url = lv.GetAttr("", "url")
|
|
|
|
enc.Length = lv.GetAttri64("", "length")
|
|
|
|
enc.Type = lv.GetAttr("", "type")
|
|
|
|
i.Enclosures = append(i.Enclosures, enc)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
if src := item.SelectNode("", "source"); src != nil {
|
|
|
|
i.Source = new(Source)
|
2010-05-23 14:21:30 +00:00
|
|
|
i.Source.Url = src.GetAttr("", "url")
|
|
|
|
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
|
|
|
|
}
|