adapter.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "sort"
  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(feedURL string) *model.Feed {
  22. feed := &model.Feed{
  23. Title: strings.TrimSpace(j.jsonFeed.Title),
  24. FeedURL: j.jsonFeed.FeedURL,
  25. SiteURL: j.jsonFeed.HomePageURL,
  26. }
  27. if feed.FeedURL == "" {
  28. feed.FeedURL = feedURL
  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(feedURL, j.jsonFeed.FeedURL); err == nil {
  35. feed.FeedURL = feedURL
  36. }
  37. if siteURL, err := urllib.AbsoluteURL(feedURL, j.jsonFeed.HomePageURL); 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 := append(item.Authors, j.jsonFeed.Authors...)
  103. itemAuthors = append(itemAuthors, item.Author, j.jsonFeed.Author)
  104. authorNamesMap := make(map[string]bool)
  105. for _, author := range itemAuthors {
  106. authorName := strings.TrimSpace(author.Name)
  107. if authorName != "" {
  108. authorNamesMap[authorName] = true
  109. }
  110. }
  111. var authors []string
  112. for authorName := range authorNamesMap {
  113. authors = append(authors, authorName)
  114. }
  115. sort.Strings(authors)
  116. entry.Author = strings.Join(authors, ", ")
  117. // Populate the entry enclosures.
  118. for _, attachment := range item.Attachments {
  119. attachmentURL := strings.TrimSpace(attachment.URL)
  120. if attachmentURL != "" {
  121. if absoluteAttachmentURL, err := urllib.AbsoluteURL(feed.SiteURL, attachmentURL); err == nil {
  122. entry.Enclosures = append(entry.Enclosures, &model.Enclosure{
  123. URL: absoluteAttachmentURL,
  124. MimeType: attachment.MimeType,
  125. Size: attachment.Size,
  126. })
  127. }
  128. }
  129. }
  130. // Populate the entry tags.
  131. for _, tag := range item.Tags {
  132. tag = strings.TrimSpace(tag)
  133. if tag != "" {
  134. entry.Tags = append(entry.Tags, tag)
  135. }
  136. }
  137. // Generate a hash for the entry.
  138. for _, value := range []string{item.ID, item.URL, item.ContentText + item.ContentHTML + item.Summary} {
  139. value = strings.TrimSpace(value)
  140. if value != "" {
  141. entry.Hash = crypto.Hash(value)
  142. break
  143. }
  144. }
  145. feed.Entries = append(feed.Entries, entry)
  146. }
  147. return feed
  148. }