adapter.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.AbsoluteURL(baseURL, feed.FeedURL); err == nil {
  36. feed.FeedURL = feedURL
  37. }
  38. if siteURL, err := urllib.AbsoluteURL(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.AbsoluteURL(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.AbsoluteURL(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. if value != "" {
  72. entry.Title = sanitizer.TruncateHTML(value, 100)
  73. }
  74. }
  75. }
  76. // Fallback to the entry URL if the title is empty.
  77. if entry.Title == "" {
  78. entry.Title = entry.URL
  79. }
  80. // Populate the entry content.
  81. for _, value := range []string{item.ContentHTML, item.ContentText, item.Summary} {
  82. value = strings.TrimSpace(value)
  83. if value != "" {
  84. entry.Content = value
  85. break
  86. }
  87. }
  88. // Populate the entry date.
  89. for _, value := range []string{item.DatePublished, item.DateModified} {
  90. value = strings.TrimSpace(value)
  91. if value != "" {
  92. if date, err := date.Parse(value); err != nil {
  93. slog.Debug("Unable to parse date from JSON feed",
  94. slog.String("date", value),
  95. slog.String("url", entry.URL),
  96. slog.Any("error", err),
  97. )
  98. } else {
  99. entry.Date = date
  100. break
  101. }
  102. }
  103. }
  104. if entry.Date.IsZero() {
  105. entry.Date = time.Now()
  106. }
  107. // Populate the entry author.
  108. itemAuthors := j.jsonFeed.Authors
  109. itemAuthors = append(itemAuthors, item.Authors...)
  110. itemAuthors = append(itemAuthors, item.Author, j.jsonFeed.Author)
  111. var authorNames []string
  112. for _, author := range itemAuthors {
  113. authorName := strings.TrimSpace(author.Name)
  114. if authorName != "" {
  115. authorNames = append(authorNames, authorName)
  116. }
  117. }
  118. slices.Sort(authorNames)
  119. authorNames = slices.Compact(authorNames)
  120. entry.Author = strings.Join(authorNames, ", ")
  121. // Populate the entry enclosures.
  122. for _, attachment := range item.Attachments {
  123. attachmentURL := strings.TrimSpace(attachment.URL)
  124. if attachmentURL != "" {
  125. if absoluteAttachmentURL, err := urllib.AbsoluteURL(feed.SiteURL, attachmentURL); err == nil {
  126. entry.Enclosures = append(entry.Enclosures, &model.Enclosure{
  127. URL: absoluteAttachmentURL,
  128. MimeType: attachment.MimeType,
  129. Size: attachment.Size,
  130. })
  131. }
  132. }
  133. }
  134. // Populate the entry tags.
  135. for _, tag := range item.Tags {
  136. tag = strings.TrimSpace(tag)
  137. if tag != "" {
  138. entry.Tags = append(entry.Tags, tag)
  139. }
  140. }
  141. // Sort and deduplicate tags.
  142. slices.Sort(entry.Tags)
  143. entry.Tags = slices.Compact(entry.Tags)
  144. // Generate a hash for the entry.
  145. for _, value := range []string{item.ID, item.URL, item.ContentText + item.ContentHTML + item.Summary} {
  146. value = strings.TrimSpace(value)
  147. if value != "" {
  148. entry.Hash = crypto.SHA256(value)
  149. break
  150. }
  151. }
  152. feed.Entries = append(feed.Entries, entry)
  153. }
  154. return feed
  155. }