4
0

feed.go 4.0 KB

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