1
0
Fork 0
miniflux/reader/handler/handler.go

213 lines
6.6 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05: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 handler // import "miniflux.app/reader/handler"
2017-11-20 00:10:04 -05:00
import (
"fmt"
"time"
"miniflux.app/config"
2018-08-25 00:51:50 -04:00
"miniflux.app/errors"
"miniflux.app/http/client"
"miniflux.app/locale"
"miniflux.app/logger"
"miniflux.app/model"
"miniflux.app/reader/browser"
2018-08-25 00:51:50 -04:00
"miniflux.app/reader/icon"
"miniflux.app/reader/parser"
2018-12-02 23:51:06 -05:00
"miniflux.app/reader/processor"
2018-08-25 00:51:50 -04:00
"miniflux.app/storage"
"miniflux.app/timer"
2017-11-20 00:10:04 -05:00
)
var (
2018-02-07 22:10:36 -05:00
errDuplicate = "This feed already exists (%s)"
2017-11-25 01:29:20 -05:00
errNotFound = "Feed %d not found"
2018-02-07 22:10:36 -05:00
errCategoryNotFound = "Category not found for this user"
2017-11-20 00:10:04 -05:00
)
// FeedCreationArgs represents the arguments required to create a new feed.
type FeedCreationArgs struct {
UserID int64
CategoryID int64
FeedURL string
UserAgent string
Username string
Password string
Crawler bool
Disabled bool
IgnoreHTTPCache bool
FetchViaProxy bool
ScraperRules string
RewriteRules string
BlocklistRules string
KeeplistRules string
2017-11-20 00:10:04 -05:00
}
// CreateFeed fetch, parse and store a new feed.
func CreateFeed(store *storage.Storage, args *FeedCreationArgs) (*model.Feed, error) {
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[CreateFeed] FeedURL=%s", args.FeedURL))
2017-11-20 00:10:04 -05:00
if !store.CategoryExists(args.UserID, args.CategoryID) {
2017-11-25 01:29:20 -05:00
return nil, errors.NewLocalizedError(errCategoryNotFound)
}
request := client.NewClientWithConfig(args.FeedURL, config.Opts)
request.WithCredentials(args.Username, args.Password)
request.WithUserAgent(args.UserAgent)
if args.FetchViaProxy {
request.WithProxy()
}
response, requestErr := browser.Exec(request)
if requestErr != nil {
return nil, requestErr
}
if store.FeedURLExists(args.UserID, response.EffectiveURL) {
2017-11-20 00:10:04 -05:00
return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
}
subscription, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
if parseErr != nil {
return nil, parseErr
}
subscription.UserID = args.UserID
subscription.UserAgent = args.UserAgent
subscription.Username = args.Username
subscription.Password = args.Password
subscription.Crawler = args.Crawler
subscription.Disabled = args.Disabled
subscription.IgnoreHTTPCache = args.IgnoreHTTPCache
subscription.FetchViaProxy = args.FetchViaProxy
subscription.ScraperRules = args.ScraperRules
subscription.RewriteRules = args.RewriteRules
subscription.BlocklistRules = args.BlocklistRules
subscription.KeeplistRules = args.KeeplistRules
subscription.WithCategoryID(args.CategoryID)
subscription.WithClientResponse(response)
subscription.CheckedNow()
2017-11-20 00:10:04 -05:00
processor.ProcessFeedEntries(store, subscription)
if storeErr := store.CreateFeed(subscription); storeErr != nil {
return nil, storeErr
2017-11-20 00:10:04 -05:00
}
logger.Debug("[CreateFeed] Feed saved with ID: %d", subscription.ID)
2017-11-20 00:10:04 -05:00
checkFeedIcon(store, subscription.ID, subscription.SiteURL, args.FetchViaProxy)
2017-11-20 00:10:04 -05:00
return subscription, nil
}
2020-09-27 19:01:06 -04:00
// RefreshFeed refreshes a feed.
func RefreshFeed(store *storage.Storage, userID, feedID int64) error {
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[RefreshFeed] feedID=%d", feedID))
userLanguage := store.UserLanguage(userID)
printer := locale.NewPrinter(userLanguage)
2017-11-20 00:10:04 -05:00
originalFeed, storeErr := store.FeedByID(userID, feedID)
if storeErr != nil {
return storeErr
2017-11-20 00:10:04 -05:00
}
if originalFeed == nil {
return errors.NewLocalizedError(errNotFound, feedID)
}
weeklyEntryCount := 0
if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
var weeklyCountErr error
weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
if weeklyCountErr != nil {
return weeklyCountErr
}
}
originalFeed.CheckedNow()
originalFeed.ScheduleNextCheck(weeklyEntryCount)
2018-02-08 21:16:54 -05:00
request := client.NewClientWithConfig(originalFeed.FeedURL, config.Opts)
request.WithCredentials(originalFeed.Username, originalFeed.Password)
request.WithUserAgent(originalFeed.UserAgent)
2020-06-06 00:50:59 -04:00
if !originalFeed.IgnoreHTTPCache {
request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
}
if originalFeed.FetchViaProxy {
request.WithProxy()
}
response, requestErr := browser.Exec(request)
if requestErr != nil {
originalFeed.WithError(requestErr.Localize(printer))
store.UpdateFeedError(originalFeed)
return requestErr
2017-11-20 00:10:04 -05:00
}
if store.AnotherFeedURLExists(userID, originalFeed.ID, response.EffectiveURL) {
storeErr := errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
originalFeed.WithError(storeErr.Error())
store.UpdateFeedError(originalFeed)
return storeErr
}
2020-06-06 00:50:59 -04:00
if originalFeed.IgnoreHTTPCache || response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
logger.Debug("[RefreshFeed] Feed #%d has been modified", feedID)
updatedFeed, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
if parseErr != nil {
originalFeed.WithError(parseErr.Localize(printer))
store.UpdateFeedError(originalFeed)
return parseErr
2017-11-20 00:10:04 -05:00
}
originalFeed.Entries = updatedFeed.Entries
processor.ProcessFeedEntries(store, originalFeed)
// We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
if storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, !originalFeed.Crawler); storeErr != nil {
originalFeed.WithError(storeErr.Error())
store.UpdateFeedError(originalFeed)
return storeErr
2017-11-20 00:10:04 -05: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(store, originalFeed.ID, originalFeed.SiteURL, originalFeed.FetchViaProxy)
2017-11-20 00:10:04 -05:00
} else {
logger.Debug("[RefreshFeed] Feed #%d not modified", feedID)
2017-11-20 00:10:04 -05:00
}
originalFeed.ResetErrorCounter()
2018-12-15 16:04:38 -05:00
if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
2018-12-15 16:04:38 -05:00
originalFeed.WithError(storeErr.Error())
store.UpdateFeedError(originalFeed)
2018-12-15 16:04:38 -05:00
return storeErr
}
return nil
2017-11-20 00:10:04 -05:00
}
func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL string, fetchViaProxy bool) {
if !store.HasIcon(feedID) {
icon, err := icon.FindIcon(websiteURL, fetchViaProxy)
if err != nil {
logger.Debug(`[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.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
}
}
}
}