json.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package feeds
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "time"
  6. )
  7. const jsonFeedVersion = "https://jsonfeed.org/version/1.1"
  8. // JSONAuthor represents the author of the feed or of an individual item
  9. // in the feed
  10. type JSONAuthor struct {
  11. Name string `json:"name,omitempty"`
  12. Url string `json:"url,omitempty"`
  13. Avatar string `json:"avatar,omitempty"`
  14. }
  15. // JSONAttachment represents a related resource. Podcasts, for instance, would
  16. // include an attachment that’s an audio or video file.
  17. type JSONAttachment struct {
  18. Url string `json:"url,omitempty"`
  19. MIMEType string `json:"mime_type,omitempty"`
  20. Title string `json:"title,omitempty"`
  21. Size int32 `json:"size,omitempty"`
  22. Duration time.Duration `json:"duration_in_seconds,omitempty"`
  23. }
  24. // MarshalJSON implements the json.Marshaler interface.
  25. // The Duration field is marshaled in seconds, all other fields are marshaled
  26. // based upon the definitions in struct tags.
  27. func (a *JSONAttachment) MarshalJSON() ([]byte, error) {
  28. type EmbeddedJSONAttachment JSONAttachment
  29. return json.Marshal(&struct {
  30. Duration float64 `json:"duration_in_seconds,omitempty"`
  31. *EmbeddedJSONAttachment
  32. }{
  33. EmbeddedJSONAttachment: (*EmbeddedJSONAttachment)(a),
  34. Duration: a.Duration.Seconds(),
  35. })
  36. }
  37. // UnmarshalJSON implements the json.Unmarshaler interface.
  38. // The Duration field is expected to be in seconds, all other field types
  39. // match the struct definition.
  40. func (a *JSONAttachment) UnmarshalJSON(data []byte) error {
  41. type EmbeddedJSONAttachment JSONAttachment
  42. var raw struct {
  43. Duration float64 `json:"duration_in_seconds,omitempty"`
  44. *EmbeddedJSONAttachment
  45. }
  46. raw.EmbeddedJSONAttachment = (*EmbeddedJSONAttachment)(a)
  47. err := json.Unmarshal(data, &raw)
  48. if err != nil {
  49. return err
  50. }
  51. if raw.Duration > 0 {
  52. nsec := int64(raw.Duration * float64(time.Second))
  53. raw.EmbeddedJSONAttachment.Duration = time.Duration(nsec)
  54. }
  55. return nil
  56. }
  57. // JSONItem represents a single entry/post for the feed.
  58. type JSONItem struct {
  59. Id string `json:"id"`
  60. Url string `json:"url,omitempty"`
  61. ExternalUrl string `json:"external_url,omitempty"`
  62. Title string `json:"title,omitempty"`
  63. ContentHTML string `json:"content_html,omitempty"`
  64. ContentText string `json:"content_text,omitempty"`
  65. Summary string `json:"summary,omitempty"`
  66. Image string `json:"image,omitempty"`
  67. BannerImage string `json:"banner_,omitempty"`
  68. PublishedDate *time.Time `json:"date_published,omitempty"`
  69. ModifiedDate *time.Time `json:"date_modified,omitempty"`
  70. Author *JSONAuthor `json:"author,omitempty"` // deprecated in JSON Feed v1.1, keeping for backwards compatibility
  71. Authors []*JSONAuthor `json:"authors,omitempty"`
  72. Tags []string `json:"tags,omitempty"`
  73. Attachments []JSONAttachment `json:"attachments,omitempty"`
  74. }
  75. // JSONHub describes an endpoint that can be used to subscribe to real-time
  76. // notifications from the publisher of this feed.
  77. type JSONHub struct {
  78. Type string `json:"type"`
  79. Url string `json:"url"`
  80. }
  81. // JSONFeed represents a syndication feed in the JSON Feed Version 1 format.
  82. // Matching the specification found here: https://jsonfeed.org/version/1.
  83. type JSONFeed struct {
  84. Version string `json:"version"`
  85. Title string `json:"title"`
  86. Language string `json:"language,omitempty"`
  87. HomePageUrl string `json:"home_page_url,omitempty"`
  88. FeedUrl string `json:"feed_url,omitempty"`
  89. Description string `json:"description,omitempty"`
  90. UserComment string `json:"user_comment,omitempty"`
  91. NextUrl string `json:"next_url,omitempty"`
  92. Icon string `json:"icon,omitempty"`
  93. Favicon string `json:"favicon,omitempty"`
  94. Author *JSONAuthor `json:"author,omitempty"` // deprecated in JSON Feed v1.1, keeping for backwards compatibility
  95. Authors []*JSONAuthor `json:"authors,omitempty"`
  96. Expired *bool `json:"expired,omitempty"`
  97. Hubs []*JSONHub `json:"hubs,omitempty"`
  98. Items []*JSONItem `json:"items,omitempty"`
  99. }
  100. // JSON is used to convert a generic Feed to a JSONFeed.
  101. type JSON struct {
  102. *Feed
  103. }
  104. // ToJSON encodes f into a JSON string. Returns an error if marshalling fails.
  105. func (f *JSON) ToJSON() (string, error) {
  106. return f.JSONFeed().ToJSON()
  107. }
  108. // ToJSON encodes f into a JSON string. Returns an error if marshalling fails.
  109. func (f *JSONFeed) ToJSON() (string, error) {
  110. data, err := json.MarshalIndent(f, "", " ")
  111. if err != nil {
  112. return "", err
  113. }
  114. return string(data), nil
  115. }
  116. // JSONFeed creates a new JSONFeed with a generic Feed struct's data.
  117. func (f *JSON) JSONFeed() *JSONFeed {
  118. feed := &JSONFeed{
  119. Version: jsonFeedVersion,
  120. Title: f.Title,
  121. Description: f.Description,
  122. }
  123. if f.Link != nil {
  124. feed.HomePageUrl = f.Link.Href
  125. }
  126. if f.Author != nil {
  127. author := &JSONAuthor{
  128. Name: f.Author.Name,
  129. }
  130. feed.Author = author
  131. feed.Authors = []*JSONAuthor{author}
  132. }
  133. for _, e := range f.Items {
  134. feed.Items = append(feed.Items, newJSONItem(e))
  135. }
  136. return feed
  137. }
  138. func newJSONItem(i *Item) *JSONItem {
  139. item := &JSONItem{
  140. Id: i.Id,
  141. Title: i.Title,
  142. Summary: i.Description,
  143. ContentHTML: i.Content,
  144. }
  145. if i.Link != nil {
  146. item.Url = i.Link.Href
  147. }
  148. if i.Source != nil {
  149. item.ExternalUrl = i.Source.Href
  150. }
  151. if i.Author != nil {
  152. author := &JSONAuthor{
  153. Name: i.Author.Name,
  154. }
  155. item.Author = author
  156. item.Authors = []*JSONAuthor{author}
  157. }
  158. if !i.Created.IsZero() {
  159. item.PublishedDate = &i.Created
  160. }
  161. if !i.Updated.IsZero() {
  162. item.ModifiedDate = &i.Updated
  163. }
  164. if i.Enclosure != nil && strings.HasPrefix(i.Enclosure.Type, "image/") {
  165. item.Image = i.Enclosure.Url
  166. }
  167. return item
  168. }