Skip to content

Commit bd23738

Browse files
committed
Better formatting and an author
1 parent b055295 commit bd23738

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

phpbb2rss.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package phpbb2rss
22

33
import (
4+
"bytes"
45
"encoding/xml"
56
"fmt"
67
"github.com/PuerkitoBio/goquery"
78
"net/http"
89
"net/url"
910
"strings"
11+
"text/template"
1012
"time"
1113
)
1214

@@ -29,6 +31,7 @@ type Item struct {
2931
Description string `xml:"description"`
3032
PubDate string `xml:"pubDate"`
3133
GUID string `xml:"guid"`
34+
Author string `xml:"author"`
3235
}
3336

3437
func FetchAndGenerateRSS(forumURL string) (string, error) {
@@ -49,7 +52,6 @@ func FetchAndGenerateRSS(forumURL string) (string, error) {
4952

5053
pageTitle := ""
5154
doc.Find("a.nav:contains('Forum Index')").Each(func(i int, s *goquery.Selection) {
52-
// Extract text from the link and trim "Forum Index" if present
5355
pageTitle = strings.TrimSpace(s.Text())
5456
if strings.HasSuffix(pageTitle, "Forum Index") {
5557
pageTitle = strings.TrimSuffix(pageTitle, " Forum Index")
@@ -69,6 +71,18 @@ func FetchAndGenerateRSS(forumURL string) (string, error) {
6971
return "", fmt.Errorf("failed to parse base URL: %w", err)
7072
}
7173

74+
descTemplate := `Category: {{.Category}}
75+
Author: {{.Author}}
76+
Last Commenter: {{.LastCommenter}}
77+
Replies: {{.Replies}}
78+
Posts: {{.Posts}}
79+
Pages: {{.Pages}}`
80+
81+
tmpl, err := template.New("description").Parse(descTemplate)
82+
if err != nil {
83+
return "", fmt.Errorf("failed to parse description template: %w", err)
84+
}
85+
7286
var items []Item
7387
doc.Find(".forumline tr").Each(func(i int, s *goquery.Selection) {
7488
topic := s.Find(".topictitle a").First()
@@ -79,29 +93,27 @@ func FetchAndGenerateRSS(forumURL string) (string, error) {
7993
return
8094
}
8195

82-
// Use url.Parse to ensure proper handling of relative URLs
8396
topicURL, err := baseURL.Parse(topicLink)
8497
if err != nil {
8598
return
8699
}
87100

88-
link := topicURL.String() // Full URL for the topic
101+
link := topicURL.String()
89102
q := topicURL.Query()
90103
q.Del("sid")
91104
topicURL.RawQuery = q.Encode()
92-
guid := topicURL.String() // Full URL for the topic
105+
guid := topicURL.String()
93106

94-
// If the latest post exists, use that link instead
95107
if latestPostExists {
96108
latestPostURL, err := baseURL.Parse(latestPostLink)
97109
if err != nil {
98110
return
99111
}
100-
link = latestPostURL.String() // Full URL for the latest post
112+
link = latestPostURL.String()
101113
q := latestPostURL.Query()
102114
q.Del("sid")
103115
latestPostURL.RawQuery = q.Encode()
104-
guid = latestPostURL.String() // Full URL for the topic
116+
guid = latestPostURL.String()
105117
}
106118

107119
pubDateRaw := strings.TrimSpace(s.Find(".postdetails").Last().Text())
@@ -117,23 +129,34 @@ func FetchAndGenerateRSS(forumURL string) (string, error) {
117129
pages := parsePageCount(s.Find("span.gensmall").Text())
118130
category := strings.TrimSpace(s.Find("a.forumlink").Text())
119131

120-
description := fmt.Sprintf("Category: %s\nAuthor: %s\nLast Commenter: %s\nReplies: %s\nPosts: %s\nPages: %s", category, author, lastCommenter, replies, posts, pages)
132+
var descriptionBuilder bytes.Buffer
133+
if err := tmpl.Execute(&descriptionBuilder, map[string]string{
134+
"Category": category,
135+
"Author": author,
136+
"LastCommenter": lastCommenter,
137+
"Replies": replies,
138+
"Posts": posts,
139+
"Pages": pages,
140+
}); err != nil {
141+
return
142+
}
121143

122144
titleWithCategory := fmt.Sprintf("[%s] %s", category, title)
123145

124146
items = append(items, Item{
125147
Title: titleWithCategory,
126148
Link: link,
127-
Description: description,
149+
Description: descriptionBuilder.String(),
128150
PubDate: parsedDate.Format(time.RFC1123),
129151
GUID: guid,
152+
Author: author,
130153
})
131154
})
132155

133156
rss := RSS{
134157
Version: "2.0",
135158
Channel: Channel{
136-
Title: pageTitle, // Dynamically set page title
159+
Title: pageTitle,
137160
Link: forumURL,
138161
Description: "RSS feed for topics from a PHPBB2 forum page",
139162
Items: items,

0 commit comments

Comments
 (0)