Browse Source

feat(rss): fallback to enclosure URL when entry URL is missing

Frédéric Guillot 8 tháng trước cách đây
mục cha
commit
d9de9d1852
2 tập tin đã thay đổi với 35 bổ sung1 xóa
  1. 7 1
      internal/reader/rss/adapter.go
  2. 28 0
      internal/reader/rss/parser_test.go

+ 7 - 1
internal/reader/rss/adapter.go

@@ -79,7 +79,13 @@ func (r *RSSAdapter) BuildFeed(baseURL string) *model.Feed {
 		// Populate the entry URL.
 		entryURL := findEntryURL(&item)
 		if entryURL == "" {
-			entry.URL = feed.SiteURL
+			// Fallback to the first enclosure URL if it exists.
+			if len(entry.Enclosures) > 0 && entry.Enclosures[0].URL != "" {
+				entry.URL = entry.Enclosures[0].URL
+			} else {
+				// Fallback to the feed URL if no entry URL is found.
+				entry.URL = feed.SiteURL
+			}
 		} else {
 			if absoluteEntryURL, err := urllib.AbsoluteURL(feed.SiteURL, entryURL); err == nil {
 				entry.URL = absoluteEntryURL

+ 28 - 0
internal/reader/rss/parser_test.go

@@ -632,6 +632,34 @@ func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
 	}
 }
 
+func TestParseEntryWithoutLinkAndWithEnclosureURLs(t *testing.T) {
+	data := `<?xml version="1.0" encoding="utf-8"?>
+		<rss version="2.0">
+		<channel>
+			<link>https://example.org/feed</link>
+			<item>
+				<guid isPermaLink="false">guid</guid>
+				<enclosure url=" " length="155844084" type="audio/mpeg" />
+				<enclosure url="https://audio-file" length="155844084" type="audio/mpeg" />
+				<enclosure url="https://another-audio-file" length="155844084" type="audio/mpeg" />
+			</item>
+		</channel>
+		</rss>`
+
+	feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if len(feed.Entries) != 1 {
+		t.Fatalf("Expected 1 entry, got: %d", len(feed.Entries))
+	}
+
+	if feed.Entries[0].URL != "https://audio-file" {
+		t.Errorf("Incorrect entry link, got: %q", feed.Entries[0].URL)
+	}
+}
+
 func TestParseFeedURLWithAtomLink(t *testing.T) {
 	data := `<?xml version="1.0" encoding="utf-8"?>
 		<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">