json.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package json // import "miniflux.app/reader/json"
  4. import (
  5. "strings"
  6. "time"
  7. "miniflux.app/crypto"
  8. "miniflux.app/logger"
  9. "miniflux.app/model"
  10. "miniflux.app/reader/date"
  11. "miniflux.app/reader/sanitizer"
  12. "miniflux.app/url"
  13. )
  14. type jsonFeed struct {
  15. Version string `json:"version"`
  16. Title string `json:"title"`
  17. SiteURL string `json:"home_page_url"`
  18. IconURL string `json:"icon"`
  19. FaviconURL string `json:"favicon"`
  20. FeedURL string `json:"feed_url"`
  21. Authors []jsonAuthor `json:"authors"`
  22. Author jsonAuthor `json:"author"`
  23. Items []jsonItem `json:"items"`
  24. }
  25. type jsonAuthor struct {
  26. Name string `json:"name"`
  27. URL string `json:"url"`
  28. }
  29. type jsonItem struct {
  30. ID string `json:"id"`
  31. URL string `json:"url"`
  32. Title string `json:"title"`
  33. Summary string `json:"summary"`
  34. Text string `json:"content_text"`
  35. HTML string `json:"content_html"`
  36. DatePublished string `json:"date_published"`
  37. DateModified string `json:"date_modified"`
  38. Authors []jsonAuthor `json:"authors"`
  39. Author jsonAuthor `json:"author"`
  40. Attachments []jsonAttachment `json:"attachments"`
  41. Tags []string `json:"tags"`
  42. }
  43. type jsonAttachment struct {
  44. URL string `json:"url"`
  45. MimeType string `json:"mime_type"`
  46. Title string `json:"title"`
  47. Size int64 `json:"size_in_bytes"`
  48. Duration int `json:"duration_in_seconds"`
  49. }
  50. func (j *jsonFeed) GetAuthor() string {
  51. if len(j.Authors) > 0 {
  52. return (getAuthor(j.Authors[0]))
  53. }
  54. return getAuthor(j.Author)
  55. }
  56. func (j *jsonFeed) Transform(baseURL string) *model.Feed {
  57. var err error
  58. feed := new(model.Feed)
  59. feed.FeedURL, err = url.AbsoluteURL(baseURL, j.FeedURL)
  60. if err != nil {
  61. feed.FeedURL = j.FeedURL
  62. }
  63. feed.SiteURL, err = url.AbsoluteURL(baseURL, j.SiteURL)
  64. if err != nil {
  65. feed.SiteURL = j.SiteURL
  66. }
  67. feed.IconURL = strings.TrimSpace(j.IconURL)
  68. if feed.IconURL == "" {
  69. feed.IconURL = strings.TrimSpace(j.FaviconURL)
  70. }
  71. feed.Title = strings.TrimSpace(j.Title)
  72. if feed.Title == "" {
  73. feed.Title = feed.SiteURL
  74. }
  75. for _, item := range j.Items {
  76. entry := item.Transform()
  77. entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
  78. if err == nil {
  79. entry.URL = entryURL
  80. }
  81. if entry.Author == "" {
  82. entry.Author = j.GetAuthor()
  83. }
  84. feed.Entries = append(feed.Entries, entry)
  85. }
  86. return feed
  87. }
  88. func (j *jsonItem) GetDate() time.Time {
  89. for _, value := range []string{j.DatePublished, j.DateModified} {
  90. if value != "" {
  91. d, err := date.Parse(value)
  92. if err != nil {
  93. logger.Error("json: %v", err)
  94. return time.Now()
  95. }
  96. return d
  97. }
  98. }
  99. return time.Now()
  100. }
  101. func (j *jsonItem) GetAuthor() string {
  102. if len(j.Authors) > 0 {
  103. return getAuthor(j.Authors[0])
  104. }
  105. return getAuthor(j.Author)
  106. }
  107. func (j *jsonItem) GetHash() string {
  108. for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
  109. if value != "" {
  110. return crypto.Hash(value)
  111. }
  112. }
  113. return ""
  114. }
  115. func (j *jsonItem) GetTitle() string {
  116. if j.Title != "" {
  117. return j.Title
  118. }
  119. for _, value := range []string{j.Summary, j.Text, j.HTML} {
  120. if value != "" {
  121. return sanitizer.TruncateHTML(value, 100)
  122. }
  123. }
  124. return j.URL
  125. }
  126. func (j *jsonItem) GetContent() string {
  127. for _, value := range []string{j.HTML, j.Text, j.Summary} {
  128. if value != "" {
  129. return value
  130. }
  131. }
  132. return ""
  133. }
  134. func (j *jsonItem) GetEnclosures() model.EnclosureList {
  135. enclosures := make(model.EnclosureList, 0)
  136. for _, attachment := range j.Attachments {
  137. if attachment.URL == "" {
  138. continue
  139. }
  140. enclosures = append(enclosures, &model.Enclosure{
  141. URL: attachment.URL,
  142. MimeType: attachment.MimeType,
  143. Size: attachment.Size,
  144. })
  145. }
  146. return enclosures
  147. }
  148. func (j *jsonItem) Transform() *model.Entry {
  149. entry := new(model.Entry)
  150. entry.URL = j.URL
  151. entry.Date = j.GetDate()
  152. entry.Author = j.GetAuthor()
  153. entry.Hash = j.GetHash()
  154. entry.Content = j.GetContent()
  155. entry.Title = strings.TrimSpace(j.GetTitle())
  156. entry.Enclosures = j.GetEnclosures()
  157. entry.Tags = j.Tags
  158. return entry
  159. }
  160. func getAuthor(author jsonAuthor) string {
  161. if author.Name != "" {
  162. return strings.TrimSpace(author.Name)
  163. }
  164. return ""
  165. }