From f49fc8ca0ba24923bb82e655b86c78de4ba2e3ad Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 28 Mar 2024 12:51:33 +0100 Subject: [PATCH] Generate RSS to file --- cmd/control/rss.go | 81 ++++++++++++++++++++++++++++++++++++++++++++ cmd/view/articles.go | 17 ++++++++++ 2 files changed, 98 insertions(+) diff --git a/cmd/control/rss.go b/cmd/control/rss.go index 813cd8c..5721ddb 100644 --- a/cmd/control/rss.go +++ b/cmd/control/rss.go @@ -2,6 +2,8 @@ package control import ( "fmt" + "io" + "os" "time" "git.streifling.com/jason/rss" @@ -48,3 +50,82 @@ func GetChannel(db *model.DB, title, link, description string) (*rss.Channel, er return channel, nil } + +func GenerateRSS(db *model.DB, title, link, desc string) (*string, error) { + channel := &rss.Channel{ + Title: title, + Link: link, + Description: desc, + 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) + } + + articleContent, err := ConvertToHTML(article.Content) + if err != nil { + return nil, fmt.Errorf("error converting content to HTML for RSS feed: %v", err) + } + + channel.Items = append(channel.Items, &rss.Item{ + Title: articleTitle, + Author: user.FirstName + user.LastName, + PubDate: article.Created.Format(time.RFC1123Z), + Description: articleDescription, + Content: &rss.Content{Value: articleContent}, + Categories: tagNames, + }) + } + + feed := rss.NewFeed() + feed.Channels = append(feed.Channels, channel) + rss, err := feed.ToXML() + 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() + file.Chmod(0644) + + if _, err = io.WriteString(file, *feed); err != nil { + return fmt.Errorf("error writing to RSS file: %v", err) + } + + return nil +} diff --git a/cmd/view/articles.go b/cmd/view/articles.go index fb10bdc..b6a9aba 100644 --- a/cmd/view/articles.go +++ b/cmd/view/articles.go @@ -305,6 +305,23 @@ func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc { return } + feed, err := control.GenerateRSS( + db, + "Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", + "https://distrikt-ni-st.de", + "Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität", + ) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if err = control.SaveRSS("tmp/orientexpress_alle.rss", feed); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])