miniflux/reader/feed/handler.go

178 lines
5.1 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.
package feed
import (
"fmt"
"log"
"time"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/errors"
"github.com/miniflux/miniflux2/helper"
2017-12-03 05:26:21 +01:00
"github.com/miniflux/miniflux2/http"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/reader/icon"
"github.com/miniflux/miniflux2/reader/processor"
2017-11-20 06:10:04 +01:00
"github.com/miniflux/miniflux2/storage"
)
var (
2017-11-25 07:29:20 +01:00
errRequestFailed = "Unable to execute request: %v"
errServerFailure = "Unable to fetch feed (statusCode=%d)."
errDuplicate = "This feed already exists (%s)."
errNotFound = "Feed %d not found"
errEncoding = "Unable to normalize encoding: %v."
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
}
// CreateFeed fetch, parse and store a new feed.
func (h *Handler) CreateFeed(userID, categoryID int64, url string) (*model.Feed, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:CreateFeed] feedUrl=%s", url))
2017-11-25 07:29:20 +01:00
if !h.store.CategoryExists(userID, categoryID) {
return nil, errors.NewLocalizedError(errCategoryNotFound)
}
client := http.NewClient(url)
2017-11-20 06:10:04 +01:00
response, err := client.Get()
if err != nil {
return nil, errors.NewLocalizedError(errRequestFailed, err)
}
if response.HasServerFailure() {
return nil, errors.NewLocalizedError(errServerFailure, response.StatusCode)
}
if h.store.FeedURLExists(userID, response.EffectiveURL) {
return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
}
body, err := response.NormalizeBodyEncoding()
if err != nil {
return nil, errors.NewLocalizedError(errEncoding, err)
}
subscription, err := parseFeed(body)
2017-11-20 06:10:04 +01:00
if err != nil {
return nil, err
}
feedProcessor := processor.NewFeedProcessor(subscription)
feedProcessor.Process()
2017-11-20 06:10:04 +01:00
subscription.Category = &model.Category{ID: categoryID}
subscription.EtagHeader = response.ETag
subscription.LastModifiedHeader = response.LastModified
subscription.FeedURL = response.EffectiveURL
subscription.UserID = userID
err = h.store.CreateFeed(subscription)
if err != nil {
return nil, err
}
log.Println("[Handler:CreateFeed] Feed saved with ID:", subscription.ID)
icon, err := icon.FindIcon(subscription.SiteURL)
if err != nil {
log.Println(err)
} else if icon == nil {
log.Printf("No icon found for feedID=%d\n", subscription.ID)
} else {
h.store.CreateFeedIcon(subscription, icon)
}
return subscription, nil
}
// RefreshFeed fetch and update a feed if necessary.
func (h *Handler) RefreshFeed(userID, feedID int64) error {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:RefreshFeed] feedID=%d", feedID))
2017-11-28 06:30:04 +01:00
originalFeed, err := h.store.FeedByID(userID, feedID)
2017-11-20 06:10:04 +01:00
if err != nil {
return err
}
if originalFeed == nil {
return errors.NewLocalizedError(errNotFound, feedID)
}
client := http.NewClientWithCacheHeaders(originalFeed.FeedURL, originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
2017-11-20 06:10:04 +01:00
response, err := client.Get()
if err != nil {
customErr := errors.NewLocalizedError(errRequestFailed, err)
originalFeed.ParsingErrorCount++
originalFeed.ParsingErrorMsg = customErr.Error()
h.store.UpdateFeed(originalFeed)
return customErr
}
originalFeed.CheckedAt = time.Now()
if response.HasServerFailure() {
err := errors.NewLocalizedError(errServerFailure, response.StatusCode)
originalFeed.ParsingErrorCount++
originalFeed.ParsingErrorMsg = err.Error()
h.store.UpdateFeed(originalFeed)
return err
}
if response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
log.Printf("[Handler:RefreshFeed] Feed #%d has been modified\n", feedID)
body, err := response.NormalizeBodyEncoding()
if err != nil {
return errors.NewLocalizedError(errEncoding, err)
}
2017-11-20 06:10:04 +01:00
subscription, err := parseFeed(body)
2017-11-20 06:10:04 +01:00
if err != nil {
originalFeed.ParsingErrorCount++
originalFeed.ParsingErrorMsg = err.Error()
h.store.UpdateFeed(originalFeed)
return err
}
feedProcessor := processor.NewFeedProcessor(subscription)
feedProcessor.WithScraperRules(originalFeed.ScraperRules)
feedProcessor.WithRewriteRules(originalFeed.RewriteRules)
feedProcessor.Process()
2017-11-20 06:10:04 +01:00
originalFeed.EtagHeader = response.ETag
originalFeed.LastModifiedHeader = response.LastModified
if err := h.store.UpdateEntries(originalFeed.UserID, originalFeed.ID, subscription.Entries); err != nil {
return err
}
if !h.store.HasIcon(originalFeed.ID) {
log.Println("[Handler:RefreshFeed] Looking for feed icon")
icon, err := icon.FindIcon(originalFeed.SiteURL)
if err != nil {
log.Println("[Handler:RefreshFeed]", err)
} else {
h.store.CreateFeedIcon(originalFeed, icon)
}
}
} else {
log.Printf("[Handler:RefreshFeed] Feed #%d not modified\n", feedID)
}
originalFeed.ParsingErrorCount = 0
originalFeed.ParsingErrorMsg = ""
return h.store.UpdateFeed(originalFeed)
}
// NewFeedHandler returns a feed handler.
func NewFeedHandler(store *storage.Storage) *Handler {
return &Handler{store: store}
}