adapter.go 4.5 KB

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