Skip to content
This repository was archived by the owner on Dec 17, 2023. It is now read-only.

Commit 897f294

Browse files
committed
fix: RSS parsing with comments from stacker.news and HTML entities encoding
1 parent 0727373 commit 897f294

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

pkg/feed/feed.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"encoding/hex"
88
"encoding/json"
99
"fmt"
10-
"github.com/piraces/rsslay/pkg/helpers"
11-
"log"
12-
"net/http"
13-
"strings"
14-
"time"
15-
1610
"github.com/PuerkitoBio/goquery"
1711
strip "github.com/grokify/html-strip-tags-go"
1812
"github.com/mmcdole/gofeed"
1913
"github.com/nbd-wtf/go-nostr"
14+
"github.com/piraces/rsslay/pkg/helpers"
2015
"github.com/rif/cache2go"
16+
"html"
17+
"log"
18+
"net/http"
19+
"strings"
20+
"time"
2121
)
2222

2323
var (
@@ -145,8 +145,14 @@ func ItemToTextNote(pubkey string, item *gofeed.Item, feed *gofeed.Feed, default
145145
if item.Title != "" {
146146
content = "**" + item.Title + "**\n\n"
147147
}
148+
148149
description := strip.StripTags(item.Description)
149150

151+
// Handle stacker.news comments
152+
if strings.Contains(feed.Link, "stacker.news") {
153+
description += fmt.Sprintf(": %s", item.GUID)
154+
}
155+
150156
if !strings.EqualFold(item.Title, description) {
151157
content += description
152158
}
@@ -176,6 +182,7 @@ func ItemToTextNote(pubkey string, item *gofeed.Item, feed *gofeed.Feed, default
176182
content += description
177183
}
178184

185+
content = html.UnescapeString(content)
179186
if len(content) > 250 {
180187
content = content[0:249] + "…"
181188
}
@@ -198,7 +205,7 @@ func ItemToTextNote(pubkey string, item *gofeed.Item, feed *gofeed.Feed, default
198205
CreatedAt: createdAt,
199206
Kind: nostr.KindTextNote,
200207
Tags: nostr.Tags{},
201-
Content: content,
208+
Content: strings.ToValidUTF8(content, ""),
202209
}
203210
evt.ID = string(evt.Serialize())
204211

pkg/feed/feed_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ var sampleNitterFeed = gofeed.Feed{
4242
},
4343
}
4444

45+
var sampleStackerNewsFeed = gofeed.Feed{
46+
Title: "Stacker News",
47+
Description: "Like Hacker News, but we pay you Bitcoin.",
48+
Link: "https://stacker.news",
49+
FeedLink: "https://stacker.news/rss",
50+
Links: []string{"https://blog.cryptographyengineering.com/2014/11/zero-knowledge-proofs-illustrated-primer.html"},
51+
PublishedParsed: &actualTime,
52+
Language: "en",
53+
}
54+
4555
var sampleNitterFeedRTItem = gofeed.Item{
4656
Title: "RT by @coldplay: TOMORROW",
4757
Description: "Sample description",
@@ -81,6 +91,16 @@ var sampleDefaultFeedItem = gofeed.Item{
8191
var sampleDefaultFeedItemExpectedContent = fmt.Sprintf("**%s**\n\n%s", sampleDefaultFeedItem.Title, sampleDefaultFeedItem.Description)
8292
var sampleDefaultFeedItemExpectedContentSubstring = sampleDefaultFeedItemExpectedContent[0:249]
8393

94+
var sampleStackerNewsFeedItem = gofeed.Item{
95+
Title: "Zero Knowledge Proofs: An illustrated primer",
96+
Description: "<a href=\"https://stacker.news/items/131533\">Comments</a>",
97+
Content: "Sample content",
98+
Link: "https://blog.cryptographyengineering.com/2014/11/zero-knowledge-proofs-illustrated-primer.html",
99+
UpdatedParsed: &actualTime,
100+
PublishedParsed: &actualTime,
101+
GUID: "https://stacker.news/items/131533",
102+
}
103+
84104
var sampleDefaultFeed = gofeed.Feed{
85105
Title: "Golang Weekly",
86106
Description: "A weekly newsletter about the Go programming language",
@@ -216,6 +236,14 @@ func TestItemToTextNote(t *testing.T) {
216236
originalUrl: sampleDefaultFeed.FeedLink,
217237
expectedContent: sampleDefaultFeedItemExpectedContentSubstring + "…" + "\n\n" + sampleDefaultFeedItem.Link,
218238
},
239+
{
240+
pubKey: samplePubKey,
241+
item: &sampleStackerNewsFeedItem,
242+
feed: &sampleStackerNewsFeed,
243+
defaultCreatedAt: actualTime,
244+
originalUrl: sampleStackerNewsFeed.FeedLink,
245+
expectedContent: fmt.Sprintf("**%s**\n\nComments: %s\n\n%s", sampleStackerNewsFeedItem.Title, sampleStackerNewsFeedItem.GUID, sampleStackerNewsFeedItem.Link),
246+
},
219247
}
220248
for _, tc := range testCases {
221249
event := ItemToTextNote(tc.pubKey, tc.item, tc.feed, tc.defaultCreatedAt, tc.originalUrl)

0 commit comments

Comments
 (0)