package backend import ( "fmt" "io" "os" "git.streifling.com/jason/atom" ) type Feed struct{ *atom.Feed } func GenerateAtomFeed(c *Config, db *DB) (*string, error) { feed := atom.NewFeed(c.Title) feed.ID = atom.NewID("urn:feed:1") feed.Subtitle = atom.NewText("text", c.Description) feed.AddLink(atom.NewLink(c.Link)) feed.Generator = atom.NewGenerator("cpolis") feed.Generator.URI = "https://git.streifling.com/jason/cpolis" feed.Generator.Version = "0.13" articles, err := db.GetCertainArticles("published", true) if err != nil { return nil, fmt.Errorf("error getting published articles for RSS feed: %v", err) } for _, article := range articles { articleTitle, err := ConvertToPlain(article.Title) if err != nil { return nil, fmt.Errorf("error converting title to plain text for RSS feed: %v", err) } entry := atom.NewEntry(articleTitle) entry.ID = atom.NewID(fmt.Sprint("urn:entry:", article.ID)) entry.Content = atom.NewContent(atom.OutOfLine, "text/hmtl", article.ContentLink) entry.Published = atom.NewDate(article.Created) linkID := entry.AddLink(atom.NewLink(article.BannerLink)) entry.Links[linkID].Rel = "enclosure" entry.Links[linkID].Type = "image/webp" user, err := db.GetUser(c, article.AuthorID) if err != nil { return nil, fmt.Errorf("error getting user user info for RSS feed: %v", err) } entry.AddAuthor(atom.NewPerson(user.FirstName + " " + user.LastName)) articleSummary, err := ConvertToPlain(article.Summary) if err != nil { return nil, fmt.Errorf("error converting description to plain text for RSS feed: %v", err) } if article.AutoGenerated { articleSummary = "auto generated" } entry.Summary = atom.NewText("text", articleSummary) tags, err := db.GetArticleTags(article.ID) if err != nil { return nil, fmt.Errorf("error getting tags for articles for RSS feed: %v", err) } for _, tag := range tags { entry.AddCategory(atom.NewCategory(tag.Name)) } if article.IsInIssue || article.AutoGenerated { entry.AddCategory(atom.NewCategory(fmt.Sprint("Orient Express ", article.IssueID))) } if article.AutoGenerated { entry.AddCategory(atom.NewCategory("autogenerated")) } entry.Updated = atom.NewDate(article.Created) feed.AddEntry(entry) } if err = feed.Check(); err != nil { return nil, fmt.Errorf("error checking Atom feed: %v", err) } atom, err := feed.ToXML("UTF-8") if err != nil { return nil, fmt.Errorf("error converting RSS feed to XML: %v", err) } return &atom, nil } func SaveAtomFeed(filename string, feed *string) error { file, err := os.Create(filename) if err != nil { return fmt.Errorf("error creating file for Atom feed: %v", err) } defer file.Close() if err = file.Chmod(0644); err != nil { return fmt.Errorf("error setting permissions for Atom file: %v", err) } if _, err = io.WriteString(file, *feed); err != nil { return fmt.Errorf("error writing to Atom file: %v", err) } return nil }