feed.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Category *Category `json:"category,omitempty"`
  29. Entries Entries `json:"entries,omitempty"`
  30. Icon *FeedIcon `json:"icon"`
  31. }
  32. func (f *Feed) String() string {
  33. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  34. f.ID,
  35. f.UserID,
  36. f.FeedURL,
  37. f.SiteURL,
  38. f.Title,
  39. f.Category,
  40. )
  41. }
  42. // WithClientResponse updates feed attributes from an HTTP request.
  43. func (f *Feed) WithClientResponse(response *client.Response) {
  44. f.EtagHeader = response.ETag
  45. f.LastModifiedHeader = response.LastModified
  46. f.FeedURL = response.EffectiveURL
  47. }
  48. // WithCategoryID initializes the category attribute of the feed.
  49. func (f *Feed) WithCategoryID(categoryID int64) {
  50. f.Category = &Category{ID: categoryID}
  51. }
  52. // WithBrowsingParameters defines browsing parameters.
  53. func (f *Feed) WithBrowsingParameters(crawler bool, userAgent, username, password string) {
  54. f.Crawler = crawler
  55. f.UserAgent = userAgent
  56. f.Username = username
  57. f.Password = password
  58. }
  59. // WithError adds a new error message and increment the error counter.
  60. func (f *Feed) WithError(message string) {
  61. f.ParsingErrorCount++
  62. f.ParsingErrorMsg = message
  63. }
  64. // ResetErrorCounter removes all previous errors.
  65. func (f *Feed) ResetErrorCounter() {
  66. f.ParsingErrorCount = 0
  67. f.ParsingErrorMsg = ""
  68. }
  69. // CheckedNow set attribute values when the feed is refreshed.
  70. func (f *Feed) CheckedNow() {
  71. f.CheckedAt = time.Now()
  72. if f.SiteURL == "" {
  73. f.SiteURL = f.FeedURL
  74. }
  75. }
  76. // Feeds is a list of feed
  77. type Feeds []*Feed