feed.go 2.7 KB

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