handler.go 6.9 KB

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