adapter.go 4.4 KB

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