handler.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package feed // import "miniflux.app/reader/feed"
  5. import (
  6. "fmt"
  7. "time"
  8. "miniflux.app/errors"
  9. "miniflux.app/http/client"
  10. "miniflux.app/locale"
  11. "miniflux.app/logger"
  12. "miniflux.app/model"
  13. "miniflux.app/reader/browser"
  14. "miniflux.app/reader/icon"
  15. "miniflux.app/reader/parser"
  16. "miniflux.app/reader/processor"
  17. "miniflux.app/storage"
  18. "miniflux.app/timer"
  19. )
  20. var (
  21. errDuplicate = "This feed already exists (%s)"
  22. errNotFound = "Feed %d not found"
  23. errCategoryNotFound = "Category not found for this user"
  24. )
  25. // Handler contains all the logic to create and refresh feeds.
  26. type Handler struct {
  27. store *storage.Storage
  28. }
  29. // CreateFeed fetch, parse and store a new feed.
  30. func (h *Handler) CreateFeed(userID, categoryID int64, url string, crawler bool, userAgent, username, password string) (*model.Feed, error) {
  31. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:CreateFeed] feedUrl=%s", url))
  32. if !h.store.CategoryExists(userID, categoryID) {
  33. return nil, errors.NewLocalizedError(errCategoryNotFound)
  34. }
  35. request := client.New(url)
  36. request.WithCredentials(username, password)
  37. request.WithUserAgent(userAgent)
  38. response, requestErr := browser.Exec(request)
  39. if requestErr != nil {
  40. return nil, requestErr
  41. }
  42. if h.store.FeedURLExists(userID, response.EffectiveURL) {
  43. return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  44. }
  45. subscription, parseErr := parser.ParseFeed(response.String())
  46. if parseErr != nil {
  47. return nil, parseErr
  48. }
  49. subscription.UserID = userID
  50. subscription.WithCategoryID(categoryID)
  51. subscription.WithBrowsingParameters(crawler, userAgent, username, password)
  52. subscription.WithClientResponse(response)
  53. subscription.CheckedNow()
  54. feedProcessor := processor.NewFeedProcessor(userID, h.store, subscription)
  55. feedProcessor.WithCrawler(crawler)
  56. feedProcessor.Process()
  57. if storeErr := h.store.CreateFeed(subscription); storeErr != nil {
  58. return nil, storeErr
  59. }
  60. logger.Debug("[Handler:CreateFeed] Feed saved with ID: %d", subscription.ID)
  61. checkFeedIcon(h.store, subscription.ID, subscription.SiteURL)
  62. return subscription, nil
  63. }
  64. // RefreshFeed fetch and update a feed if necessary.
  65. func (h *Handler) RefreshFeed(userID, feedID int64) error {
  66. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:RefreshFeed] feedID=%d", feedID))
  67. userLanguage := h.store.UserLanguage(userID)
  68. printer := locale.NewPrinter(userLanguage)
  69. originalFeed, storeErr := h.store.FeedByID(userID, feedID)
  70. if storeErr != nil {
  71. return storeErr
  72. }
  73. if originalFeed == nil {
  74. return errors.NewLocalizedError(errNotFound, feedID)
  75. }
  76. originalFeed.CheckedNow()
  77. request := client.New(originalFeed.FeedURL)
  78. request.WithCredentials(originalFeed.Username, originalFeed.Password)
  79. request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
  80. request.WithUserAgent(originalFeed.UserAgent)
  81. response, requestErr := browser.Exec(request)
  82. if requestErr != nil {
  83. originalFeed.WithError(requestErr.Localize(printer))
  84. h.store.UpdateFeed(originalFeed)
  85. return requestErr
  86. }
  87. if response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  88. logger.Debug("[Handler:RefreshFeed] Feed #%d has been modified", feedID)
  89. subscription, parseErr := parser.ParseFeed(response.String())
  90. if parseErr != nil {
  91. originalFeed.WithError(parseErr.Localize(printer))
  92. h.store.UpdateFeed(originalFeed)
  93. return parseErr
  94. }
  95. feedProcessor := processor.NewFeedProcessor(userID, h.store, subscription)
  96. feedProcessor.WithScraperRules(originalFeed.ScraperRules)
  97. feedProcessor.WithUserAgent(originalFeed.UserAgent)
  98. feedProcessor.WithRewriteRules(originalFeed.RewriteRules)
  99. feedProcessor.WithCrawler(originalFeed.Crawler)
  100. feedProcessor.Process()
  101. // Note: We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
  102. if storeErr := h.store.UpdateEntries(originalFeed.UserID, originalFeed.ID, subscription.Entries, !originalFeed.Crawler); storeErr != nil {
  103. return storeErr
  104. }
  105. // We update caching headers only if the feed has been modified,
  106. // because some websites don't return the same headers when replying with a 304.
  107. originalFeed.WithClientResponse(response)
  108. checkFeedIcon(h.store, originalFeed.ID, originalFeed.SiteURL)
  109. } else {
  110. logger.Debug("[Handler:RefreshFeed] Feed #%d not modified", feedID)
  111. }
  112. originalFeed.ResetErrorCounter()
  113. return h.store.UpdateFeed(originalFeed)
  114. }
  115. // NewFeedHandler returns a feed handler.
  116. func NewFeedHandler(store *storage.Storage) *Handler {
  117. return &Handler{store}
  118. }
  119. func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL string) {
  120. if !store.HasIcon(feedID) {
  121. icon, err := icon.FindIcon(websiteURL)
  122. if err != nil {
  123. logger.Error("CheckFeedIcon: %v (feedID=%d websiteURL=%s)", err, feedID, websiteURL)
  124. } else if icon == nil {
  125. logger.Debug("CheckFeedIcon: No icon found (feedID=%d websiteURL=%s)", feedID, websiteURL)
  126. } else {
  127. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  128. logger.Error("CheckFeedIcon: %v (feedID=%d websiteURL=%s)", err, feedID, websiteURL)
  129. }
  130. }
  131. }
  132. }