cpolis/cmd/backend/rss.go

136 lines
3.5 KiB
Go
Raw Normal View History

package backend
2024-02-18 12:41:49 +01:00
import (
"fmt"
2024-03-28 12:51:33 +01:00
"io"
"os"
2024-03-09 10:12:46 +01:00
"time"
2024-02-18 12:41:49 +01:00
"git.streifling.com/jason/rss"
2024-02-18 12:41:49 +01:00
)
func GetChannel(db *DB, title, link, description string) (*rss.Channel, error) {
2024-03-09 10:12:46 +01:00
channel := &rss.Channel{
Title: title,
Link: link,
Description: description,
2024-03-09 10:12:46 +01:00
Items: make([]*rss.Item, 0),
2024-02-18 12:41:49 +01:00
}
articles, err := db.GetCertainArticles(true, false)
if err != nil {
2024-03-09 10:12:46 +01:00
return nil, fmt.Errorf("error fetching published articles: %v", err)
}
2024-03-09 10:12:46 +01:00
for _, article := range articles {
tags, err := db.GetArticleTags(article.ID)
if err != nil {
return nil, fmt.Errorf("error fetching tags for article %v: %v", article.Title, err)
}
tagNames := make([]string, 0)
for _, tag := range tags {
tagNames = append(tagNames, tag.Name)
}
2024-03-09 10:12:46 +01:00
user, err := db.GetUser(article.AuthorID)
if err != nil {
return nil, fmt.Errorf("error finding user %v: %v", article.AuthorID, err)
}
2024-02-18 12:41:49 +01:00
2024-03-09 10:12:46 +01:00
channel.Items = append(channel.Items, &rss.Item{
Title: article.Title,
Author: fmt.Sprint(user.FirstName, " ", user.LastName),
2024-03-09 10:12:46 +01:00
PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Description,
2024-03-09 10:12:46 +01:00
Content: &rss.Content{Value: article.Content},
Categories: tagNames,
})
2024-02-18 12:41:49 +01:00
}
2024-03-09 10:12:46 +01:00
return channel, nil
2024-03-01 21:01:38 +01:00
}
2024-03-28 12:51:33 +01:00
func GenerateRSS(c *Config, db *DB) (*string, error) {
2024-03-28 12:51:33 +01:00
channel := &rss.Channel{
Title: c.Title,
Link: c.Link,
Description: c.Description,
2024-03-28 12:51:33 +01:00
Items: make([]*rss.Item, 0),
}
articles, err := db.GetCertainArticles(true, false)
if err != nil {
return nil, fmt.Errorf("error getting published articles for RSS feed: %v", err)
}
for _, article := range articles {
tags, err := db.GetArticleTags(article.ID)
if err != nil {
return nil, fmt.Errorf("error getting tags for articles for RSS feed: %v", err)
}
tagNames := make([]string, 0)
for _, tag := range tags {
tagNames = append(tagNames, tag.Name)
}
tagNames = append(tagNames, fmt.Sprint("Orient Express ", article.IssueID))
user, err := db.GetUser(article.AuthorID)
if err != nil {
return nil, fmt.Errorf("error getting user user info for RSS feed: %v", err)
}
articleTitle, err := ConvertToPlain(article.Title)
if err != nil {
return nil, fmt.Errorf("error converting title to plain text for RSS feed: %v", err)
}
articleDescription, err := ConvertToPlain(article.Description)
if err != nil {
return nil, fmt.Errorf("error converting description to plain text for RSS feed: %v", err)
}
item := &rss.Item{
Author: fmt.Sprint(user.FirstName, " ", user.LastName),
2024-03-28 12:51:33 +01:00
Categories: tagNames,
Description: articleDescription,
PubDate: article.Created.Format(time.RFC1123Z),
Title: articleTitle,
}
fmt.Println(article.Link, ": ", len(article.Link))
if article.Link == "" {
item.Link = fmt.Sprint("http://", c.Domain, "/article/serve/", article.ID, ".html")
} else {
item.Link = article.Link
}
channel.Items = append(channel.Items, item)
2024-03-28 12:51:33 +01:00
}
feed := rss.NewFeed()
feed.Channels = append(feed.Channels, channel)
2024-04-04 17:09:29 +02:00
rss, err := feed.ToXML("UTF-8")
2024-03-28 12:51:33 +01:00
if err != nil {
return nil, fmt.Errorf("error converting RSS feed to XML: %v", err)
}
return &rss, nil
}
func SaveRSS(filename string, feed *string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating file for RSS feed: %v", err)
}
defer file.Close()
if err = file.Chmod(0644); err != nil {
return fmt.Errorf("error setting permissions for RSS file: %v", err)
}
2024-03-28 12:51:33 +01:00
if _, err = io.WriteString(file, *feed); err != nil {
return fmt.Errorf("error writing to RSS file: %v", err)
}
return nil
}