handler.go 7.4 KB

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