feed.go 3.8 KB

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