handler.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 handler // import "miniflux.app/reader/handler"
  5. import (
  6. "fmt"
  7. "time"
  8. "miniflux.app/config"
  9. "miniflux.app/errors"
  10. "miniflux.app/http/client"
  11. "miniflux.app/locale"
  12. "miniflux.app/logger"
  13. "miniflux.app/model"
  14. "miniflux.app/reader/browser"
  15. "miniflux.app/reader/icon"
  16. "miniflux.app/reader/parser"
  17. "miniflux.app/reader/processor"
  18. "miniflux.app/storage"
  19. "miniflux.app/timer"
  20. )
  21. var (
  22. errDuplicate = "This feed already exists (%s)"
  23. errNotFound = "Feed %d not found"
  24. errCategoryNotFound = "Category not found for this user"
  25. )
  26. // CreateFeed fetch, parse and store a new feed.
  27. func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model.FeedCreationRequest) (*model.Feed, error) {
  28. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[CreateFeed] FeedURL=%s", feedCreationRequest.FeedURL))
  29. if !store.CategoryIDExists(userID, feedCreationRequest.CategoryID) {
  30. return nil, errors.NewLocalizedError(errCategoryNotFound)
  31. }
  32. request := client.NewClientWithConfig(feedCreationRequest.FeedURL, config.Opts)
  33. request.WithCredentials(feedCreationRequest.Username, feedCreationRequest.Password)
  34. request.WithUserAgent(feedCreationRequest.UserAgent)
  35. if feedCreationRequest.FetchViaProxy {
  36. request.WithProxy()
  37. }
  38. response, requestErr := browser.Exec(request)
  39. if requestErr != nil {
  40. return nil, requestErr
  41. }
  42. if store.FeedURLExists(userID, response.EffectiveURL) {
  43. return nil, errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  44. }
  45. subscription, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  46. if parseErr != nil {
  47. return nil, parseErr
  48. }
  49. subscription.UserID = userID
  50. subscription.UserAgent = feedCreationRequest.UserAgent
  51. subscription.Username = feedCreationRequest.Username
  52. subscription.Password = feedCreationRequest.Password
  53. subscription.Crawler = feedCreationRequest.Crawler
  54. subscription.Disabled = feedCreationRequest.Disabled
  55. subscription.IgnoreHTTPCache = feedCreationRequest.IgnoreHTTPCache
  56. subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
  57. subscription.ScraperRules = feedCreationRequest.ScraperRules
  58. subscription.RewriteRules = feedCreationRequest.RewriteRules
  59. subscription.BlocklistRules = feedCreationRequest.BlocklistRules
  60. subscription.KeeplistRules = feedCreationRequest.KeeplistRules
  61. subscription.WithCategoryID(feedCreationRequest.CategoryID)
  62. subscription.WithClientResponse(response)
  63. subscription.CheckedNow()
  64. processor.ProcessFeedEntries(store, subscription)
  65. if storeErr := store.CreateFeed(subscription); storeErr != nil {
  66. return nil, storeErr
  67. }
  68. logger.Debug("[CreateFeed] Feed saved with ID: %d", subscription.ID)
  69. checkFeedIcon(store, subscription.ID, subscription.SiteURL, feedCreationRequest.FetchViaProxy)
  70. return subscription, nil
  71. }
  72. // RefreshFeed refreshes a feed.
  73. func RefreshFeed(store *storage.Storage, userID, feedID int64) error {
  74. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[RefreshFeed] feedID=%d", feedID))
  75. userLanguage := store.UserLanguage(userID)
  76. printer := locale.NewPrinter(userLanguage)
  77. originalFeed, storeErr := store.FeedByID(userID, feedID)
  78. if storeErr != nil {
  79. return storeErr
  80. }
  81. if originalFeed == nil {
  82. return errors.NewLocalizedError(errNotFound, feedID)
  83. }
  84. weeklyEntryCount := 0
  85. if config.Opts.PollingScheduler() == model.SchedulerEntryFrequency {
  86. var weeklyCountErr error
  87. weeklyEntryCount, weeklyCountErr = store.WeeklyFeedEntryCount(userID, feedID)
  88. if weeklyCountErr != nil {
  89. return weeklyCountErr
  90. }
  91. }
  92. originalFeed.CheckedNow()
  93. originalFeed.ScheduleNextCheck(weeklyEntryCount)
  94. request := client.NewClientWithConfig(originalFeed.FeedURL, config.Opts)
  95. request.WithCredentials(originalFeed.Username, originalFeed.Password)
  96. request.WithUserAgent(originalFeed.UserAgent)
  97. if !originalFeed.IgnoreHTTPCache {
  98. request.WithCacheHeaders(originalFeed.EtagHeader, originalFeed.LastModifiedHeader)
  99. }
  100. if originalFeed.FetchViaProxy {
  101. request.WithProxy()
  102. }
  103. response, requestErr := browser.Exec(request)
  104. if requestErr != nil {
  105. originalFeed.WithError(requestErr.Localize(printer))
  106. store.UpdateFeedError(originalFeed)
  107. return requestErr
  108. }
  109. if store.AnotherFeedURLExists(userID, originalFeed.ID, response.EffectiveURL) {
  110. storeErr := errors.NewLocalizedError(errDuplicate, response.EffectiveURL)
  111. originalFeed.WithError(storeErr.Error())
  112. store.UpdateFeedError(originalFeed)
  113. return storeErr
  114. }
  115. if originalFeed.IgnoreHTTPCache || response.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) {
  116. logger.Debug("[RefreshFeed] Feed #%d has been modified", feedID)
  117. updatedFeed, parseErr := parser.ParseFeed(response.EffectiveURL, response.BodyAsString())
  118. if parseErr != nil {
  119. originalFeed.WithError(parseErr.Localize(printer))
  120. store.UpdateFeedError(originalFeed)
  121. return parseErr
  122. }
  123. originalFeed.Entries = updatedFeed.Entries
  124. processor.ProcessFeedEntries(store, originalFeed)
  125. // We don't update existing entries when the crawler is enabled (we crawl only inexisting entries).
  126. if storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, originalFeed.Entries, !originalFeed.Crawler); storeErr != nil {
  127. originalFeed.WithError(storeErr.Error())
  128. store.UpdateFeedError(originalFeed)
  129. return storeErr
  130. }
  131. // We update caching headers only if the feed has been modified,
  132. // because some websites don't return the same headers when replying with a 304.
  133. originalFeed.WithClientResponse(response)
  134. checkFeedIcon(store, originalFeed.ID, originalFeed.SiteURL, originalFeed.FetchViaProxy)
  135. } else {
  136. logger.Debug("[RefreshFeed] Feed #%d not modified", feedID)
  137. }
  138. originalFeed.ResetErrorCounter()
  139. if storeErr := store.UpdateFeed(originalFeed); storeErr != nil {
  140. originalFeed.WithError(storeErr.Error())
  141. store.UpdateFeedError(originalFeed)
  142. return storeErr
  143. }
  144. return nil
  145. }
  146. func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL string, fetchViaProxy bool) {
  147. if !store.HasIcon(feedID) {
  148. icon, err := icon.FindIcon(websiteURL, fetchViaProxy)
  149. if err != nil {
  150. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  151. } else if icon == nil {
  152. logger.Debug(`[CheckFeedIcon] No icon found (feedID=%d websiteURL=%s)`, feedID, websiteURL)
  153. } else {
  154. if err := store.CreateFeedIcon(feedID, icon); err != nil {
  155. logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
  156. }
  157. }
  158. }
  159. }