adapter.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. "cmp"
  6. "log/slog"
  7. "slices"
  8. "strings"
  9. "time"
  10. "miniflux.app/v2/internal/crypto"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/reader/date"
  13. "miniflux.app/v2/internal/reader/sanitizer"
  14. "miniflux.app/v2/internal/urllib"
  15. )
  16. type JSONAdapter struct {
  17. jsonFeed *JSONFeed
  18. }
  19. func NewJSONAdapter(jsonFeed *JSONFeed) *JSONAdapter {
  20. return &JSONAdapter{jsonFeed}
  21. }
  22. func (j *JSONAdapter) BuildFeed(baseURL string) *model.Feed {
  23. feed := &model.Feed{
  24. Title: strings.TrimSpace(j.jsonFeed.Title),
  25. FeedURL: strings.TrimSpace(j.jsonFeed.FeedURL),
  26. SiteURL: strings.TrimSpace(j.jsonFeed.HomePageURL),
  27. Description: strings.TrimSpace(j.jsonFeed.Description),
  28. }
  29. if feed.FeedURL == "" {
  30. feed.FeedURL = strings.TrimSpace(baseURL)
  31. }
  32. // Fallback to the feed URL if the site URL is empty.
  33. if feed.SiteURL == "" {
  34. feed.SiteURL = feed.FeedURL
  35. }
  36. if feedURL, err := urllib.ResolveToAbsoluteURL(baseURL, feed.FeedURL); err == nil {
  37. feed.FeedURL = feedURL
  38. }
  39. if siteURL, err := urllib.ResolveToAbsoluteURL(baseURL, feed.SiteURL); err == nil {
  40. feed.SiteURL = siteURL
  41. }
  42. // Fallback to the feed URL if the title is empty.
  43. if feed.Title == "" {
  44. feed.Title = feed.SiteURL
  45. }
  46. // Populate the icon URL if present.
  47. for _, iconURL := range []string{j.jsonFeed.FaviconURL, j.jsonFeed.IconURL} {
  48. if iconURL = strings.TrimSpace(iconURL); iconURL == "" {
  49. continue
  50. }
  51. if absoluteIconURL, err := urllib.ResolveToAbsoluteURL(feed.SiteURL, iconURL); err == nil {
  52. feed.IconURL = absoluteIconURL
  53. break
  54. }
  55. }
  56. for _, item := range j.jsonFeed.Items {
  57. entry := model.NewEntry()
  58. for _, itemURL := range []string{item.URL, item.ExternalURL} {
  59. if itemURL = strings.TrimSpace(itemURL); itemURL == "" {
  60. continue
  61. }
  62. // Make sure the entry URL is absolute.
  63. if entryURL, err := urllib.ResolveToAbsoluteURL(feed.SiteURL, itemURL); err == nil {
  64. entry.URL = entryURL
  65. break
  66. }
  67. }
  68. entry.Title = strings.TrimSpace(item.Title)
  69. if entry.Title == "" {
  70. // The entry title is optional, so we need to find a fallback.
  71. for _, value := range []string{item.Summary, item.ContentText, item.ContentHTML} {
  72. if value = sanitizer.TruncateHTML(value, 100); value == "" {
  73. continue
  74. }
  75. entry.Title = value
  76. break
  77. }
  78. }
  79. // Fallback to the entry URL if the title is empty.
  80. if entry.Title == "" {
  81. entry.Title = entry.URL
  82. }
  83. // Populate the entry content.
  84. for _, value := range []string{item.ContentHTML, item.ContentText, item.Summary} {
  85. if value = strings.TrimSpace(value); value == "" {
  86. continue
  87. }
  88. entry.Content = value
  89. break
  90. }
  91. // Populate the entry date.
  92. for _, value := range []string{item.DatePublished, item.DateModified} {
  93. if value = strings.TrimSpace(value); value == "" {
  94. continue
  95. }
  96. parsedDate, err := date.Parse(value)
  97. if err != nil {
  98. slog.Debug("Unable to parse date from JSON feed",
  99. slog.String("date", value),
  100. slog.String("url", entry.URL),
  101. slog.Any("error", err),
  102. )
  103. continue
  104. }
  105. entry.Date = parsedDate
  106. break
  107. }
  108. if entry.Date.IsZero() {
  109. entry.Date = time.Now()
  110. }
  111. // Populate the entry author.
  112. authorNames := make([]string, 0, len(j.jsonFeed.Authors)+len(item.Authors)+1+1)
  113. authorNames = appendSorted(authorNames, JSONAuthor.name, j.jsonFeed.Authors...)
  114. authorNames = appendSorted(authorNames, JSONAuthor.name, item.Authors...)
  115. authorNames = appendSorted(authorNames, JSONAuthor.name, item.Author, j.jsonFeed.Author)
  116. entry.Author = strings.Join(authorNames, ", ")
  117. // Populate the entry enclosures.
  118. for _, attachment := range item.Attachments {
  119. attachmentURL := strings.TrimSpace(attachment.URL)
  120. if attachmentURL == "" {
  121. continue
  122. }
  123. absoluteAttachmentURL, err := urllib.ResolveToAbsoluteURL(feed.SiteURL, attachmentURL)
  124. if err != nil {
  125. slog.Debug("Unable to build absolute URL for attachment",
  126. slog.String("url", attachmentURL),
  127. slog.String("site_url", feed.SiteURL),
  128. slog.Any("error", err),
  129. )
  130. continue
  131. }
  132. entry.Enclosures = append(entry.Enclosures, &model.Enclosure{
  133. URL: absoluteAttachmentURL,
  134. MimeType: attachment.MimeType,
  135. Size: attachment.Size,
  136. })
  137. }
  138. // Populate the entry tags.
  139. entry.Tags = make([]string, 0, len(item.Tags))
  140. entry.Tags = appendSorted(entry.Tags, strings.TrimSpace, item.Tags...)
  141. // Generate a hash for the entry.
  142. for _, value := range []string{item.ID, item.URL, item.ExternalURL, item.ContentText + item.ContentHTML + item.Summary} {
  143. value = strings.TrimSpace(value)
  144. if value != "" {
  145. entry.Hash = crypto.SHA256(value)
  146. break
  147. }
  148. }
  149. feed.Entries = append(feed.Entries, entry)
  150. }
  151. return feed
  152. }
  153. // appendSortedSeq appends elements from "values" slice into "sorted" slice.
  154. // - "fn" applied to every element of "values"
  155. // - elements inserted into "sorted" slice so it stays sorted
  156. // - duplicate elements are not inserted
  157. func appendSorted[I any, O cmp.Ordered](sorted []O, fn func(I) O, values ...I) []O {
  158. var zero O
  159. sorted = slices.Grow(sorted, len(values))
  160. for in := range slices.Values(values) {
  161. out := fn(in)
  162. if out == zero {
  163. continue
  164. }
  165. where, found := slices.BinarySearch(sorted, out)
  166. if found {
  167. continue
  168. }
  169. // Insert sorted to avoid duplicates.
  170. sorted = slices.Insert(sorted, where, out)
  171. }
  172. return sorted
  173. }