handler.go 7.1 KB

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