65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
|
package feed
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"golang.org/x/net/html"
|
||
|
)
|
||
|
|
||
|
const feedfmt = `<?xml version="1.0" encoding="utf-8"?>
|
||
|
<rss version="2.0">
|
||
|
<channel>
|
||
|
<title>%s</title>
|
||
|
<link>%s</link>
|
||
|
<description>%s</description>%s
|
||
|
</channel>
|
||
|
</rss>`
|
||
|
|
||
|
const itemfmt = `
|
||
|
<item>
|
||
|
<title>Content Title</title>
|
||
|
<link>%s</link>
|
||
|
<guid>%s</guid>
|
||
|
<pubDate>%s</pubDate>
|
||
|
<description>
|
||
|
%s
|
||
|
</description>
|
||
|
</item>`
|
||
|
|
||
|
type RSSBuilder struct {
|
||
|
Info FeedInfo
|
||
|
Items []FeedItem
|
||
|
}
|
||
|
|
||
|
func GenerateRss(siteUrl, siteTitle, siteDesc string, pageUrls ...string) (string, error) {
|
||
|
var items strings.Builder
|
||
|
var errs strings.Builder
|
||
|
var err error
|
||
|
|
||
|
for _, u := range pageUrls {
|
||
|
var formattedArticle strings.Builder
|
||
|
var err error
|
||
|
page, err := fetchPage(u)
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
article, atime, err := parseArticle(page)
|
||
|
if err != nil && article == "" {
|
||
|
errs.WriteString(fmt.Sprintf("error parsing article %s: %s", u, err.Error()))
|
||
|
continue
|
||
|
}
|
||
|
for _, line := range strings.Split(article, "\n") {
|
||
|
formattedArticle.WriteString(fmt.Sprintf("\t\t%s\n", html.EscapeString(line)))
|
||
|
}
|
||
|
if atime != nil {
|
||
|
items.WriteString(fmt.Sprintf(itemfmt, u, u, atime.Format("Mon, 2 Jan 2006 15:04:05 MST"), formattedArticle.String()))
|
||
|
} else {
|
||
|
items.WriteString(fmt.Sprintf(itemfmt, u, u, time.Now().Format("Mon, 2 Jan 2006 15:04:05 MST"), formattedArticle.String()))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return fmt.Sprintf(feedfmt, siteTitle, siteUrl, siteDesc, items.String()), err
|
||
|
}
|