feed.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "time"
  8. "miniflux.app/http/client"
  9. )
  10. // Feed represents a feed in the application.
  11. type Feed struct {
  12. ID int64 `json:"id"`
  13. UserID int64 `json:"user_id"`
  14. FeedURL string `json:"feed_url"`
  15. SiteURL string `json:"site_url"`
  16. Title string `json:"title"`
  17. CheckedAt time.Time `json:"checked_at"`
  18. EtagHeader string `json:"etag_header"`
  19. LastModifiedHeader string `json:"last_modified_header"`
  20. ParsingErrorMsg string `json:"parsing_error_message"`
  21. ParsingErrorCount int `json:"parsing_error_count"`
  22. ScraperRules string `json:"scraper_rules"`
  23. RewriteRules string `json:"rewrite_rules"`
  24. Crawler bool `json:"crawler"`
  25. UserAgent string `json:"user_agent"`
  26. Username string `json:"username"`
  27. Password string `json:"password"`
  28. Disabled bool `json:"disabled"`
  29. Category *Category `json:"category,omitempty"`
  30. Entries Entries `json:"entries,omitempty"`
  31. Icon *FeedIcon `json:"icon"`
  32. UnreadCount int
  33. ReadCount int
  34. }
  35. func (f *Feed) String() string {
  36. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  37. f.ID,
  38. f.UserID,
  39. f.FeedURL,
  40. f.SiteURL,
  41. f.Title,
  42. f.Category,
  43. )
  44. }
  45. // WithClientResponse updates feed attributes from an HTTP request.
  46. func (f *Feed) WithClientResponse(response *client.Response) {
  47. f.EtagHeader = response.ETag
  48. f.LastModifiedHeader = response.LastModified
  49. f.FeedURL = response.EffectiveURL
  50. }
  51. // WithCategoryID initializes the category attribute of the feed.
  52. func (f *Feed) WithCategoryID(categoryID int64) {
  53. f.Category = &Category{ID: categoryID}
  54. }
  55. // WithBrowsingParameters defines browsing parameters.
  56. func (f *Feed) WithBrowsingParameters(crawler bool, userAgent, username, password string) {
  57. f.Crawler = crawler
  58. f.UserAgent = userAgent
  59. f.Username = username
  60. f.Password = password
  61. }
  62. // WithError adds a new error message and increment the error counter.
  63. func (f *Feed) WithError(message string) {
  64. f.ParsingErrorCount++
  65. f.ParsingErrorMsg = message
  66. }
  67. // ResetErrorCounter removes all previous errors.
  68. func (f *Feed) ResetErrorCounter() {
  69. f.ParsingErrorCount = 0
  70. f.ParsingErrorMsg = ""
  71. }
  72. // CheckedNow set attribute values when the feed is refreshed.
  73. func (f *Feed) CheckedNow() {
  74. f.CheckedAt = time.Now()
  75. if f.SiteURL == "" {
  76. f.SiteURL = f.FeedURL
  77. }
  78. }
  79. // Feeds is a list of feed
  80. type Feeds []*Feed