Adds the database for checking new channels/items.

In this iteration the key passed to the database is the Title which is
obviously silly.
I'm still looking for a configurable way of generating the unique key.
This commit is contained in:
Harm Aarts 2013-12-05 15:31:49 +01:00
parent 90c93b8fa4
commit 3b336dc54b
1 changed files with 29 additions and 0 deletions

29
feed.go
View File

@ -58,6 +58,9 @@ type Feed struct {
// Url from which this feed was created. // Url from which this feed was created.
Url string Url string
// Database containing a list of known Items and Channels for this instance
database *database
// A notification function, used to notify the host when a new channel // A notification function, used to notify the host when a new channel
// has been found. // has been found.
chanhandler ChannelHandler chanhandler ChannelHandler
@ -76,6 +79,7 @@ func New(cachetimeout int, enforcecachelimit bool, ch ChannelHandler, ih ItemHan
v.CacheTimeout = cachetimeout v.CacheTimeout = cachetimeout
v.EnforceCacheLimit = enforcecachelimit v.EnforceCacheLimit = enforcecachelimit
v.Type = "none" v.Type = "none"
v.database = NewDatabase()
v.chanhandler = ch v.chanhandler = ch
v.itemhandler = ih v.itemhandler = ih
return v return v
@ -159,9 +163,34 @@ func (this *Feed) makeFeed(doc *xmlx.Document) (err error) {
this.CacheTimeout = this.Channels[0].TTL this.CacheTimeout = this.Channels[0].TTL
} }
this.notifyListeners()
return return
} }
func (this *Feed) notifyListeners() {
var newchannels []*Channel
for _, channel := range this.Channels {
if this.database.request <- channel.Title; <-this.database.response {
newchannels = append(newchannels, channel)
}
var newitems []*Item
for _, item := range channel.Items {
if this.database.request <- item.Title; <-this.database.response {
newitems = append(newitems, item)
}
}
if len(newitems) > 0 {
this.itemhandler(this, channel, newitems)
}
}
if len(newchannels) > 0 {
this.chanhandler(this, newchannels)
}
}
// This function returns true or false, depending on whether the CacheTimeout // This function returns true or false, depending on whether the CacheTimeout
// value has expired or not. Additionally, it will ensure that we adhere to the // value has expired or not. Additionally, it will ensure that we adhere to the
// RSS spec's SkipDays and SkipHours values (if Feed.EnforceCacheLimit is set to // RSS spec's SkipDays and SkipHours values (if Feed.EnforceCacheLimit is set to