package backend import ( "fmt" "io" "os" "time" "git.streifling.com/jason/rss" ) func GenerateRSS(c *Config, db *DB) (*string, error) { channel := &rss.Channel{ Title: c.Title, Link: c.Link, Description: c.Description, 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 { if !article.Published { continue } 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), Categories: tagNames, Description: articleDescription, Guid: string(article.ID), Link: article.Link, PubDate: article.Created.Format(time.RFC1123Z), Title: articleTitle, } fmt.Println(article.Link, ": ", len(article.Link)) if article.Title == "Autogenerated cpolis Issue Article" { item.Enclosure = &rss.Enclosure{ Url: article.EncURL, Lenght: article.EncLength, Type: article.EncType, } } channel.Items = append(channel.Items, item) } feed := rss.NewFeed() feed.Channels = append(feed.Channels, channel) rss, err := feed.ToXML("UTF-8") 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) } if _, err = io.WriteString(file, *feed); err != nil { return fmt.Errorf("error writing to RSS file: %v", err) } return nil }