feed.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 `json:"-"`
  33. ReadCount int `json:"-"`
  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, scraperRules, rewriteRules string) {
  57. f.Crawler = crawler
  58. f.UserAgent = userAgent
  59. f.Username = username
  60. f.Password = password
  61. f.ScraperRules = scraperRules
  62. f.RewriteRules = rewriteRules
  63. }
  64. // WithError adds a new error message and increment the error counter.
  65. func (f *Feed) WithError(message string) {
  66. f.ParsingErrorCount++
  67. f.ParsingErrorMsg = message
  68. }
  69. // ResetErrorCounter removes all previous errors.
  70. func (f *Feed) ResetErrorCounter() {
  71. f.ParsingErrorCount = 0
  72. f.ParsingErrorMsg = ""
  73. }
  74. // CheckedNow set attribute values when the feed is refreshed.
  75. func (f *Feed) CheckedNow() {
  76. f.CheckedAt = time.Now()
  77. if f.SiteURL == "" {
  78. f.SiteURL = f.FeedURL
  79. }
  80. }
  81. // Feeds is a list of feed
  82. type Feeds []*Feed