adapter.go 5.6 KB

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