Prepared for in-memory-caching (not yet functional).

This commit is contained in:
Martin Dosch 2019-06-01 10:59:19 +02:00
parent 1bdf8c048f
commit fb21d01556
2 changed files with 106 additions and 95 deletions

View file

@ -17,19 +17,22 @@ import (
"jaytaylor.com/html2text"
)
// ToDo: If caching == false create a global variable to save timestamp in memory.
// Get new articles for specified feed.
func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (string, error) {
func getArticles(feedURL string, max int, noExcerpt bool, filter []string, caching bool) (string, error) {
type feedCache struct {
LastChange string
}
var output, cachePath string
var output, cachePath, cacheFile string
var last time.Time
var lastUpdate feedCache
var file *os.File
var updateTime time.Time
if caching == true {
// Get systems user cache path.
osCacheDir := os.Getenv("$XDG_CACHE_HOME")
if osCacheDir != "" {
@ -78,7 +81,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (stri
}
}
cacheFile := cachePath + strconv.Itoa(int(h.Sum32()))
cacheFile = cachePath + strconv.Itoa(int(h.Sum32()))
if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
file, err = os.Create(cacheFile)
@ -116,6 +119,9 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (stri
log.Fatal("Error: Can't parse last updates time stamp:", err)
}
}
} else {
last = time.Now()
}
fp := gofeed.NewParser()
feed, err := fp.ParseURL(feedURL)
@ -174,6 +180,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (stri
last = updateTime
lastUpdate.LastChange = updateTime.Format(time.RFC3339)
if caching == true {
// Remove file with cached timestamp and create it
// again with updated timestamp.
// ToDo: Replace timestamp without deleting.
@ -193,6 +200,7 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (stri
if err != nil {
log.Fatal("Error: Can't write last update time stamp to cache file:", err)
}
}
// Remove redirects and tracking parameters from URL.
cleanURL, _ := removeTracking(article.Link)

View file

@ -17,11 +17,12 @@ type configuration struct {
Password string
Muc string
MucNick string
Contact string
MaxArticles int
RefreshTime time.Duration
NoExcerpt bool
Quiet bool
Contact string
Caching bool
Filter []string
Feeds []string
}
@ -72,7 +73,8 @@ func main() {
go pingMUC(client, config.BotJid, config.Muc, config.MucNick)
// Starting goroutine to process received stanzas.
go processStanzas(client, config.Muc, config.MucNick, config.Feeds, config.Quiet, config.Contact)
go processStanzas(client, config.Muc, config.MucNick, config.Feeds, config.Quiet,
config.Contact)
// Set RefreshTime to 30 seconds if not defined.
if config.RefreshTime == 0 {
@ -83,7 +85,8 @@ func main() {
// Check all configured feeds for new articles and send
// new articles to configured MUC.
for i := 0; i < len(config.Feeds); i++ {
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter)
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt,
config.Filter, config.Caching)
if err != nil {
// Exit if an error occurs checking the feeds.
log.Fatal("Error: Can't check feeds for new articles: ", err)