feed.go 7.3 KB

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