feed.go 3.9 KB

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