feed.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // Feed represents a feed in the application.
  13. type Feed struct {
  14. ID int64 `json:"id"`
  15. UserID int64 `json:"user_id"`
  16. FeedURL string `json:"feed_url"`
  17. SiteURL string `json:"site_url"`
  18. Title string `json:"title"`
  19. CheckedAt time.Time `json:"checked_at"`
  20. NextCheckAt time.Time `json:"next_check_at"`
  21. EtagHeader string `json:"etag_header"`
  22. LastModifiedHeader string `json:"last_modified_header"`
  23. ParsingErrorMsg string `json:"parsing_error_message"`
  24. ParsingErrorCount int `json:"parsing_error_count"`
  25. ScraperRules string `json:"scraper_rules"`
  26. RewriteRules string `json:"rewrite_rules"`
  27. Crawler bool `json:"crawler"`
  28. BlocklistRules string `json:"blocklist_rules"`
  29. KeeplistRules string `json:"keeplist_rules"`
  30. UserAgent string `json:"user_agent"`
  31. Username string `json:"username"`
  32. Password string `json:"password"`
  33. Disabled bool `json:"disabled"`
  34. IgnoreHTTPCache bool `json:"ignore_http_cache"`
  35. FetchViaProxy bool `json:"fetch_via_proxy"`
  36. Category *Category `json:"category,omitempty"`
  37. Entries Entries `json:"entries,omitempty"`
  38. Icon *FeedIcon `json:"icon"`
  39. UnreadCount int `json:"-"`
  40. ReadCount int `json:"-"`
  41. }
  42. // List of supported schedulers.
  43. const (
  44. SchedulerRoundRobin = "round_robin"
  45. SchedulerEntryFrequency = "entry_frequency"
  46. )
  47. func (f *Feed) String() string {
  48. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  49. f.ID,
  50. f.UserID,
  51. f.FeedURL,
  52. f.SiteURL,
  53. f.Title,
  54. f.Category,
  55. )
  56. }
  57. // WithClientResponse updates feed attributes from an HTTP request.
  58. func (f *Feed) WithClientResponse(response *client.Response) {
  59. f.EtagHeader = response.ETag
  60. f.LastModifiedHeader = response.LastModified
  61. f.FeedURL = response.EffectiveURL
  62. }
  63. // WithCategoryID initializes the category attribute of the feed.
  64. func (f *Feed) WithCategoryID(categoryID int64) {
  65. f.Category = &Category{ID: categoryID}
  66. }
  67. // WithError adds a new error message and increment the error counter.
  68. func (f *Feed) WithError(message string) {
  69. f.ParsingErrorCount++
  70. f.ParsingErrorMsg = message
  71. }
  72. // ResetErrorCounter removes all previous errors.
  73. func (f *Feed) ResetErrorCounter() {
  74. f.ParsingErrorCount = 0
  75. f.ParsingErrorMsg = ""
  76. }
  77. // CheckedNow set attribute values when the feed is refreshed.
  78. func (f *Feed) CheckedNow() {
  79. f.CheckedAt = time.Now()
  80. if f.SiteURL == "" {
  81. f.SiteURL = f.FeedURL
  82. }
  83. }
  84. // ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
  85. func (f *Feed) ScheduleNextCheck(weeklyCount int) {
  86. switch config.Opts.PollingScheduler() {
  87. case SchedulerEntryFrequency:
  88. var intervalMinutes int
  89. if weeklyCount == 0 {
  90. intervalMinutes = config.Opts.SchedulerEntryFrequencyMaxInterval()
  91. } else {
  92. intervalMinutes = int(math.Round(float64(7*24*60) / float64(weeklyCount)))
  93. }
  94. intervalMinutes = int(math.Min(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMaxInterval())))
  95. intervalMinutes = int(math.Max(float64(intervalMinutes), float64(config.Opts.SchedulerEntryFrequencyMinInterval())))
  96. f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(intervalMinutes))
  97. default:
  98. f.NextCheckAt = time.Now()
  99. }
  100. }
  101. // Feeds is a list of feed
  102. type Feeds []*Feed