json.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 (
  5. "log/slog"
  6. "strings"
  7. "time"
  8. "miniflux.app/v2/internal/crypto"
  9. "miniflux.app/v2/internal/model"
  10. "miniflux.app/v2/internal/reader/date"
  11. "miniflux.app/v2/internal/reader/sanitizer"
  12. "miniflux.app/v2/internal/urllib"
  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 = urllib.AbsoluteURL(baseURL, j.FeedURL)
  60. if err != nil {
  61. feed.FeedURL = j.FeedURL
  62. }
  63. feed.SiteURL, err = urllib.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 := urllib.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. slog.Debug("Unable to parse date from JSON feed",
  94. slog.String("date", value),
  95. slog.String("url", j.URL),
  96. slog.Any("error", err),
  97. )
  98. return time.Now()
  99. }
  100. return d
  101. }
  102. }
  103. return time.Now()
  104. }
  105. func (j *jsonItem) GetAuthor() string {
  106. if len(j.Authors) > 0 {
  107. return getAuthor(j.Authors[0])
  108. }
  109. return getAuthor(j.Author)
  110. }
  111. func (j *jsonItem) GetHash() string {
  112. for _, value := range []string{j.ID, j.URL, j.Text + j.HTML + j.Summary} {
  113. if value != "" {
  114. return crypto.Hash(value)
  115. }
  116. }
  117. return ""
  118. }
  119. func (j *jsonItem) GetTitle() string {
  120. if j.Title != "" {
  121. return j.Title
  122. }
  123. for _, value := range []string{j.Summary, j.Text, j.HTML} {
  124. if value != "" {
  125. return sanitizer.TruncateHTML(value, 100)
  126. }
  127. }
  128. return j.URL
  129. }
  130. func (j *jsonItem) GetContent() string {
  131. for _, value := range []string{j.HTML, j.Text, j.Summary} {
  132. if value != "" {
  133. return value
  134. }
  135. }
  136. return ""
  137. }
  138. func (j *jsonItem) GetEnclosures() model.EnclosureList {
  139. enclosures := make(model.EnclosureList, 0)
  140. for _, attachment := range j.Attachments {
  141. if attachment.URL == "" {
  142. continue
  143. }
  144. enclosures = append(enclosures, &model.Enclosure{
  145. URL: attachment.URL,
  146. MimeType: attachment.MimeType,
  147. Size: attachment.Size,
  148. })
  149. }
  150. return enclosures
  151. }
  152. func (j *jsonItem) Transform() *model.Entry {
  153. entry := model.NewEntry()
  154. entry.URL = j.URL
  155. entry.Date = j.GetDate()
  156. entry.Author = j.GetAuthor()
  157. entry.Hash = j.GetHash()
  158. entry.Content = j.GetContent()
  159. entry.Title = strings.TrimSpace(j.GetTitle())
  160. entry.Enclosures = j.GetEnclosures()
  161. if len(j.Tags) > 0 {
  162. entry.Tags = j.Tags
  163. }
  164. return entry
  165. }
  166. func getAuthor(author jsonAuthor) string {
  167. if author.Name != "" {
  168. return strings.TrimSpace(author.Name)
  169. }
  170. return ""
  171. }