json.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package json // import "miniflux.app/v2/internal/reader/json"
  4. import "encoding/json"
  5. // JSON Feed specs:
  6. // https://www.jsonfeed.org/version/1.1/
  7. // https://www.jsonfeed.org/version/1/
  8. type JSONFeed struct {
  9. // Version is the URL of the version of the format the feed uses.
  10. // This should appear at the very top, though we recognize that not all JSON generators allow for ordering.
  11. Version string `json:"version"`
  12. // Title is the name of the feed, which will often correspond to the name of the website.
  13. Title string `json:"title"`
  14. // HomePageURL is the URL of the resource that the feed describes.
  15. // This resource may or may not actually be a “home” page, but it should be an HTML page.
  16. HomePageURL string `json:"home_page_url"`
  17. // FeedURL is the URL of the feed, and serves as the unique identifier for the feed.
  18. FeedURL string `json:"feed_url"`
  19. // Description provides more detail, beyond the title, on what the feed is about.
  20. Description string `json:"description"`
  21. // IconURL is the URL of an image for the feed suitable to be used in a timeline, much the way an avatar might be used.
  22. IconURL string `json:"icon"`
  23. // FaviconURL is the URL of an image for the feed suitable to be used in a source list. It should be square and relatively small.
  24. FaviconURL string `json:"favicon"`
  25. // Authors specifies one or more feed authors. The author object has several members.
  26. Authors JSONAuthors `json:"authors"` // JSON Feed v1.1
  27. // Author specifies the feed author. The author object has several members.
  28. // JSON Feed v1 (deprecated)
  29. Author JSONAuthor `json:"author"`
  30. // Language is the primary language for the feed in the format specified in RFC 5646.
  31. // The value is usually a 2-letter language tag from ISO 639-1, optionally followed by a region tag. (Examples: en or en-US.)
  32. Language string `json:"language"`
  33. // Expired is a boolean value that specifies whether or not the feed is finished.
  34. Expired bool `json:"expired"`
  35. // Items is an array, each representing an individual item in the feed.
  36. Items []JSONItem `json:"items"`
  37. // Hubs describes endpoints that can be used to subscribe to real-time notifications from the publisher of this feed.
  38. Hubs []JSONHub `json:"hubs"`
  39. }
  40. type JSONAuthor struct {
  41. // Author's name.
  42. Name string `json:"name"`
  43. // Author's website URL (Blog or micro-blog).
  44. WebsiteURL string `json:"url"`
  45. // Author's avatar URL.
  46. AvatarURL string `json:"avatar"`
  47. }
  48. // JSONAuthors unmarshals either an array or a single author object.
  49. // Some feeds incorrectly use an object for "authors"; we accept it to avoid failing the whole feed.
  50. type JSONAuthors []JSONAuthor
  51. func (a *JSONAuthors) UnmarshalJSON(data []byte) error {
  52. var authors []JSONAuthor
  53. if err := json.Unmarshal(data, &authors); err == nil {
  54. *a = authors
  55. return nil
  56. }
  57. var author JSONAuthor
  58. if err := json.Unmarshal(data, &author); err == nil {
  59. *a = []JSONAuthor{author}
  60. return nil
  61. }
  62. // Ignore invalid formats silently; the caller can still use other fields.
  63. return nil
  64. }
  65. type JSONHub struct {
  66. // Type defines the protocol used to talk with the hub: "rssCloud" or "WebSub".
  67. Type string `json:"type"`
  68. // URL is the location of the hub.
  69. URL string `json:"url"`
  70. }
  71. type JSONItem struct {
  72. // Unique identifier for the item.
  73. // Ideally, the id is the full URL of the resource described by the item, since URLs make great unique identifiers.
  74. ID string `json:"id"`
  75. // URL of the resource described by the item.
  76. URL string `json:"url"`
  77. // ExternalURL is the URL of a page elsewhere.
  78. // This is especially useful for linkblogs.
  79. // If url links to where you’re talking about a thing, then external_url links to the thing you’re talking about.
  80. ExternalURL string `json:"external_url"`
  81. // Title of the item (optional).
  82. // Microblog items in particular may omit titles.
  83. Title string `json:"title"`
  84. // ContentHTML is the HTML body of the item.
  85. ContentHTML string `json:"content_html"`
  86. // ContentText is the text body of the item.
  87. ContentText string `json:"content_text"`
  88. // Summary is a plain text sentence or two describing the item.
  89. Summary string `json:"summary"`
  90. // ImageURL is the URL of the main image for the item.
  91. ImageURL string `json:"image"`
  92. // BannerImageURL is the URL of an image to use as a banner.
  93. BannerImageURL string `json:"banner_image"`
  94. // DatePublished is the date the item was published.
  95. DatePublished string `json:"date_published"`
  96. // DateModified is the date the item was modified.
  97. DateModified string `json:"date_modified"`
  98. // Language is the language of the item.
  99. Language string `json:"language"`
  100. // Authors is an array of JSONAuthor.
  101. Authors JSONAuthors `json:"authors"`
  102. // Author is a JSONAuthor.
  103. // JSON Feed v1 (deprecated)
  104. Author JSONAuthor `json:"author"`
  105. // Tags is an array of strings.
  106. Tags []string `json:"tags"`
  107. // Attachments is an array of JSONAttachment.
  108. Attachments []JSONAttachment `json:"attachments"`
  109. }
  110. type JSONAttachment struct {
  111. // URL of the attachment.
  112. URL string `json:"url"`
  113. // MIME type of the attachment.
  114. MimeType string `json:"mime_type"`
  115. // Title of the attachment.
  116. Title string `json:"title"`
  117. // Size of the attachment in bytes.
  118. Size int64 `json:"size_in_bytes"`
  119. // Duration of the attachment in seconds.
  120. Duration int `json:"duration_in_seconds"`
  121. }