feed.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 model // import "miniflux.app/model"
  5. import (
  6. "fmt"
  7. "math"
  8. "time"
  9. "miniflux.app/config"
  10. "miniflux.app/http/client"
  11. )
  12. // List of supported schedulers.
  13. const (
  14. SchedulerRoundRobin = "round_robin"
  15. SchedulerEntryFrequency = "entry_frequency"
  16. // Default settings for the feed query builder
  17. DefaultFeedSorting = "parsing_error_count"
  18. DefaultFeedSortingDirection = "desc"
  19. )
  20. // Feed represents a feed in the application.
  21. type Feed struct {
  22. ID int64 `json:"id"`
  23. UserID int64 `json:"user_id"`
  24. FeedURL string `json:"feed_url"`
  25. SiteURL string `json:"site_url"`
  26. Title string `json:"title"`
  27. CheckedAt time.Time `json:"checked_at"`
  28. NextCheckAt time.Time `json:"next_check_at"`
  29. EtagHeader string `json:"etag_header"`
  30. LastModifiedHeader string `json:"last_modified_header"`
  31. ParsingErrorMsg string `json:"parsing_error_message"`
  32. ParsingErrorCount int `json:"parsing_error_count"`
  33. ScraperRules string `json:"scraper_rules"`
  34. RewriteRules string `json:"rewrite_rules"`
  35. Crawler bool `json:"crawler"`
  36. BlocklistRules string `json:"blocklist_rules"`
  37. KeeplistRules string `json:"keeplist_rules"`
  38. UserAgent string `json:"user_agent"`
  39. Username string `json:"username"`
  40. Password string `json:"password"`
  41. Disabled bool `json:"disabled"`
  42. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  43. FetchViaProxy bool `json:"fetch_via_proxy"`
  44. Category *Category `json:"category,omitempty"`
  45. Entries Entries `json:"entries,omitempty"`
  46. Icon *FeedIcon `json:"icon"`
  47. UnreadCount int `json:"-"`
  48. ReadCount int `json:"-"`
  49. }
  50. func (f *Feed) String() string {
  51. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  52. f.ID,
  53. f.UserID,
  54. f.FeedURL,
  55. f.SiteURL,
  56. f.Title,
  57. f.Category,
  58. )
  59. }
  60. // WithClientResponse updates feed attributes from an HTTP request.
  61. func (f *Feed) WithClientResponse(response *client.Response) {
  62. f.EtagHeader = response.ETag
  63. f.LastModifiedHeader = response.LastModified
  64. f.FeedURL = response.EffectiveURL
  65. }
  66. // WithCategoryID initializes the category attribute of the feed.
  67. func (f *Feed) WithCategoryID(categoryID int64) {
  68. f.Category = &Category{ID: categoryID}
  69. }
  70. // WithError adds a new error message and increment the error counter.
  71. func (f *Feed) WithError(message string) {
  72. f.ParsingErrorCount++
  73. f.ParsingErrorMsg = message
  74. }
  75. // ResetErrorCounter removes all previous errors.
  76. func (f *Feed) ResetErrorCounter() {
  77. f.ParsingErrorCount = 0
  78. f.ParsingErrorMsg = ""
  79. }
  80. // CheckedNow set attribute values when the feed is refreshed.
  81. func (f *Feed) CheckedNow() {
  82. f.CheckedAt = time.Now()
  83. if f.SiteURL == "" {
  84. f.SiteURL = f.FeedURL
  85. }
  86. }
  87. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  88. func (f *Feed) ScheduleNextCheck(weeklyCount int) {
  89. switch config.Opts.PollingScheduler() {
  90. case SchedulerEntryFrequency:
  91. var intervalMinutes int
  92. if weeklyCount == 0 {
  93. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  94. } else {
  95. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount)))
  96. }
  97. intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
  98. intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
  99. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  100. default:
  101. f.NextCheckAt = time.Now()
  102. }
  103. }
  104. // FeedCreationRequest represents the request to create a feed.
  105. type FeedCreationRequest struct {
  106. FeedURL string `json:"feed_url"`
  107. CategoryID int64 `json:"category_id"`
  108. UserAgent string `json:"user_agent"`
  109. Username string `json:"username"`
  110. Password string `json:"password"`
  111. Crawler bool `json:"crawler"`
  112. Disabled bool `json:"disabled"`
  113. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  114. FetchViaProxy bool `json:"fetch_via_proxy"`
  115. ScraperRules string `json:"scraper_rules"`
  116. RewriteRules string `json:"rewrite_rules"`
  117. BlocklistRules string `json:"blocklist_rules"`
  118. KeeplistRules string `json:"keeplist_rules"`
  119. }
  120. // FeedModificationRequest represents the request to update a feed.
  121. type FeedModificationRequest struct {
  122. FeedURL *string `json:"feed_url"`
  123. SiteURL *string `json:"site_url"`
  124. Title *string `json:"title"`
  125. ScraperRules *string `json:"scraper_rules"`
  126. RewriteRules *string `json:"rewrite_rules"`
  127. BlocklistRules *string `json:"blocklist_rules"`
  128. KeeplistRules *string `json:"keeplist_rules"`
  129. Crawler *bool `json:"crawler"`
  130. UserAgent *string `json:"user_agent"`
  131. Username *string `json:"username"`
  132. Password *string `json:"password"`
  133. CategoryID *int64 `json:"category_id"`
  134. Disabled *bool `json:"disabled"`
  135. IgnoreHTTPCache *bool `json:"ignore_http_cache"`
  136. FetchViaProxy *bool `json:"fetch_via_proxy"`
  137. }
  138. // Patch updates a feed with modified values.
  139. func (f *FeedModificationRequest) Patch(feed *Feed) {
  140. if f.FeedURL != nil && *f.FeedURL != "" {
  141. feed.FeedURL = *f.FeedURL
  142. }
  143. if f.SiteURL != nil && *f.SiteURL != "" {
  144. feed.SiteURL = *f.SiteURL
  145. }
  146. if f.Title != nil && *f.Title != "" {
  147. feed.Title = *f.Title
  148. }
  149. if f.ScraperRules != nil {
  150. feed.ScraperRules = *f.ScraperRules
  151. }
  152. if f.RewriteRules != nil {
  153. feed.RewriteRules = *f.RewriteRules
  154. }
  155. if f.KeeplistRules != nil {
  156. feed.KeeplistRules = *f.KeeplistRules
  157. }
  158. if f.BlocklistRules != nil {
  159. feed.BlocklistRules = *f.BlocklistRules
  160. }
  161. if f.Crawler != nil {
  162. feed.Crawler = *f.Crawler
  163. }
  164. if f.UserAgent != nil {
  165. feed.UserAgent = *f.UserAgent
  166. }
  167. if f.Username != nil {
  168. feed.Username = *f.Username
  169. }
  170. if f.Password != nil {
  171. feed.Password = *f.Password
  172. }
  173. if f.CategoryID != nil && *f.CategoryID > 0 {
  174. feed.Category.ID = *f.CategoryID
  175. }
  176. if f.Disabled != nil {
  177. feed.Disabled = *f.Disabled
  178. }
  179. if f.IgnoreHTTPCache != nil {
  180. feed.IgnoreHTTPCache = *f.IgnoreHTTPCache
  181. }
  182. if f.FetchViaProxy != nil {
  183. feed.FetchViaProxy = *f.FetchViaProxy
  184. }
  185. }
  186. // Feeds is a list of feed
  187. type Feeds []*Feed