adapter.go 4.8 KB

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