示例#1
0
func createAtomFeed(filename string, posts []*Post) (err error) {
	feed := atom.Feed{
		Title: "Will Roe's blog",
	}
	for _, p := range posts {
		var t time.Time
		t, err = p.Date()
		if err != nil {
			fmt.Printf("Error getting date of post: %v\n", p)
			break
		}
		e := &atom.Entry{
			Title:     p.Title,
			Published: atom.Time(t),
		}
		feed.Entry = append(feed.Entry, e)
	}

	var data []byte
	data, err = xml.Marshal(&feed)
	if err != nil {
		fmt.Printf("Failed to marshal the feed: %s", err)
		os.Exit(1)
	}
	writeFile(string(data[:]), filename)
	return
}
示例#2
0
func getAtomFeed(res http.ResponseWriter, req *http.Request) {
	feed := atom.Feed{
		Title:   "",
		Updated: atom.Time(time.Now()),
	}
	limit, err := strconv.Atoi(req.URL.Query().Get("limit"))
	if err != nil {
		limit = 100
	}
	alerts, err := qcap.GetAlerts(limit)
	if err != nil {
		log.Panic("Error retrieving alerts:", err)
	}
	for _, a := range alerts {
		t := atom.Time(a.Id.Time())
		id := a.Id.Hex()
		e := atom.Entry{
			Title:     fmt.Sprint("Alert ", id),
			ID:        id,
			Published: t,
			Updated:   t,
			Link: []atom.Link{
				atom.Link{Rel: "self", Href: qcapBaseUrl + xmlEndpoint + "/alerts/" + id},
			},
		}
		feed.Entry = append(feed.Entry, &e)
	}
	r.XML(res, http.StatusOK, feed)
}
示例#3
0
文件: xblog.go 项目: Sfeir/onGolang
// renderAtomFeed generates an XML Atom feed and stores it in the Server's
// atomFeed field.
func (s *Server) renderAtomFeed() error {
	var updated time.Time
	if len(s.docs) > 0 {
		updated = s.docs[0].Time
	}
	feed := atom.Feed{
		Title:   s.cfg.FeedTitle,
		ID:      "tag:" + s.cfg.Hostname + ",2015:" + s.cfg.Hostname,
		Updated: atom.Time(updated),
		Link: []atom.Link{{
			Rel:  "self",
			Href: s.cfg.BaseURL + "/feed.atom",
		}},
	}
	for i, doc := range s.docs {
		if i >= s.cfg.FeedArticles {
			break
		}
		e := &atom.Entry{
			Title: doc.Title,
			ID:    feed.ID + doc.Path,
			Link: []atom.Link{{
				Rel:  "alternate",
				Href: doc.Permalink,
			}},
			Published: atom.Time(doc.Time),
			Updated:   atom.Time(doc.Time),
			Summary: &atom.Text{
				Type: "html",
				Body: summary(doc),
			},
			Content: &atom.Text{
				Type: "html",
				Body: string(doc.HTML),
			},
			Author: &atom.Person{
				Name: authors(doc.Authors),
			},
		}
		feed.Entry = append(feed.Entry, e)
	}
	data, err := xml.Marshal(&feed)
	if err != nil {
		return err
	}
	s.atomFeed = data
	return nil
}