miniflux/reader/feed/handler.go

154 lines
4.8 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 06:51:50 +02:00
package feed // import "miniflux.app/reader/feed"
2017-11-20 06:10:04 +01:00
import (
"fmt"
"time"
2018-08-25 06:51:50 +02:00
"miniflux.app/errors"
"miniflux.app/http/client"
"miniflux.app/locale"
"miniflux.app/logger"
"miniflux.app/model"
"miniflux.app/reader/browser"
"miniflux.app/reader/filter"
2018-08-25 06:51:50 +02:00
"miniflux.app/reader/icon"
"miniflux.app/reader/parser"
2018-08-25 06:51:50 +02:00
"miniflux.app/storage"
"miniflux.app/timer"
2017-11-20 06:10:04 +01:00
)
var (
2018-02-08 04:10:36 +01:00
errDuplicate = "This feed already exists (%s)"
2017-11-25 07:29:20 +01:00
errNotFound = "Feed %d not found"
2018-02-08 04:10:36 +01:00
errCategoryNotFound = "Category not found for this user"
2017-11-20 06:10:04 +01:00
)
// Handler contains all the logic to create and refresh feeds.
type Handler struct {
store *storage.Storage
2017-11-20 06:10:04 +01:00
}
// CreateFeed fetch, parse and store a new feed.
func (h *Handler) CreateFeed(userID, categoryID int64, url string, crawler bool, userAgent, username, password string) (*model.Feed, error) {
2018-01-03 04:15:08 +01:00
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:CreateFeed] feedUrl=%s", url))
2017-11-20 06:10:04 +01:00
2017-11-25 07:29:20 +01:00
if !h.store.CategoryExists(userID, categoryID) {
return nil, errors.NewLocalizedError(errCategoryNotFound)
}
request := client.New(url)
request.WithCredentials(username, password)
request.WithUserAgent(userAgent)
response, requestErr := browser.Exec(request)
if requestErr != nil {
return nil, requestErr
}
2017-11-20 06:10:04 +01:00
if h.store.FeedURLExists(userID, response.EffectiveURL) {
return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
}
subscription, parseErr := parser.ParseFeed(response.String())
if parseErr != nil {
return nil, parseErr
}
subscription.UserID = userID
subscription.WithCategoryID(categoryID)
subscription.WithBrowsingParameters(crawler, userAgent, username, password)
subscription.WithClientResponse(response)
subscription.CheckedNow()
2017-11-20 06:10:04 +01:00
filter.Apply(h.store, subscription)
if storeErr := h.store.CreateFeed(subscription); storeErr != nil {
return nil, storeErr
2017-11-20 06:10:04 +01:00
}
2017-12-16 03:55:57 +01:00
logger.Debug("[Handler:CreateFeed] Feed saved with ID: %d", subscription.ID)
2017-11-20 06:10:04 +01:00
checkFeedIcon(h.store, subscription.ID, subscription.SiteURL)
2017-11-20 06:10:04 +01:00
return subscription, nil
}
// RefreshFeed fetch and update a feed if necessary.
func (h *Handler) RefreshFeed(userID, feedID int64) error {
2018-01-03 04:15:08 +01:00
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:RefreshFeed] feedID=%d", feedID))
userLanguage := h.store.UserLanguage(userID)
printer := locale.NewPrinter(userLanguage)
2017-11-20 06:10:04 +01:00
originalFeed, storeErr := h.store.FeedByID(userID, feedID)
if storeErr != nil {
return storeErr
2017-11-20 06:10:04 +01:00
}
if originalFeed == nil {
return errors.NewLocalizedError(errNotFound, feedID)
}
originalFeed.CheckedNow()
2018-02-09 03:16:54 +01:00
request := client.New(originalFeed.FeedURL)
request.WithCredentials(originalFeed.Username, originalFeed.Password)
request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
request.WithUserAgent(originalFeed.UserAgent)
response, requestErr := browser.Exec(request)
if requestErr != nil {
originalFeed.WithError(requestErr.Localize(printer))
2017-11-20 06:10:04 +01:00
h.store.UpdateFeed(originalFeed)
return requestErr
2017-11-20 06:10:04 +01:00
}
if response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
2017-12-16 03:55:57 +01:00
logger.Debug("[Handler:RefreshFeed] Feed #%d has been modified", feedID)
updatedFeed, parseErr := parser.ParseFeed(response.String())
if parseErr != nil {
originalFeed.WithError(parseErr.Localize(printer))
2017-11-20 06:10:04 +01:00
h.store.UpdateFeed(originalFeed)
return parseErr
2017-11-20 06:10:04 +01:00
}
originalFeed.Entries = updatedFeed.Entries
filter.Apply(h.store, originalFeed)
// We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
if storeErr := h.store.UpdateEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, !originalFeed.Crawler); storeErr != nil {
return storeErr
2017-11-20 06:10:04 +01:00
}
// We update caching headers only if the feed has been modified,
// because some websites don't return the same headers when replying with a 304.
originalFeed.WithClientResponse(response)
checkFeedIcon(h.store, originalFeed.ID, originalFeed.SiteURL)
2017-11-20 06:10:04 +01:00
} else {
2017-12-16 03:55:57 +01:00
logger.Debug("[Handler:RefreshFeed] Feed #%d not modified", feedID)
2017-11-20 06:10:04 +01:00
}
originalFeed.ResetErrorCounter()
2017-11-20 06:10:04 +01:00
return h.store.UpdateFeed(originalFeed)
}
// NewFeedHandler returns a feed handler.
func NewFeedHandler(store *storage.Storage) *Handler {
return &Handler{store}
2017-11-20 06:10:04 +01:00
}
func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL string) {
if !store.HasIcon(feedID) {
icon, err := icon.FindIcon(websiteURL)
if err != nil {
logger.Error("CheckFeedIcon: %v (feedID=%d websiteURL=%s)", err, feedID, websiteURL)
} else if icon == nil {
logger.Debug("CheckFeedIcon: No icon found (feedID=%d websiteURL=%s)", feedID, websiteURL)
} else {
if err := store.CreateFeedIcon(feedID, icon); err != nil {
logger.Error("CheckFeedIcon: %v (feedID=%d websiteURL=%s)", err, feedID, websiteURL)
}
}
}
}