Beispiel #1
0
func Handler(w http.ResponseWriter, r *http.Request) {
	s, err := show.Get("http:/" + r.RequestURI)
	if err != nil {
		ErrorHandler(err, w, r)
		return
	}
	feed := newFeed(r, s)
	rss, err := feeds.ToXML(feed)
	if err != nil {
		ErrorHandler(err, w, r)
		return
	}
	w.Header().Set("Content-Type", "application/rss+xml")
	io.WriteString(w, rss)
}
Beispiel #2
0
func (p *podslingHandler) getRSS(w http.ResponseWriter, r *http.Request) {
	r.Body.Close()

	path := strings.TrimSuffix(r.URL.Path, ".rss")

	var b []byte
	var err error
	if strings.HasPrefix(path, "/b64/") {
		b, err = p.pathBase64(strings.TrimPrefix(path, "/b64/"))
	} else {
		b, err = p.getJSON(strings.TrimPrefix(path, "/"))
	}
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "Decode error: %s", err)
		return
	}

	f := feed{}
	if err := json.Unmarshal(b, &f); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "Decode error: %s. JSON:\n%s", err, b)
		return
	}

	numItems := len(f.Items)
	if numItems == 0 {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, "No items specified")
		return
	}

	rss := &feeds.RssFeed{
		Title:          f.Title,
		Link:           "http://" + r.Host + r.URL.Path,
		Description:    f.Description,
		ManagingEditor: f.Author,
		PubDate:        f.Date.Format(time.RFC822),
		Items:          make([]*feeds.RssItem, numItems),
	}

	if f.Image != "" {
		rss.Image = &feeds.RssImage{Url: f.Image, Link: f.Image}
	}

	for i, item := range f.Items {
		ext := filepath.Ext(item.URL)
		title := item.Title
		if title == "" {
			title = strings.TrimSuffix(filepath.Base(item.URL), ext)
			if esc, err := url.QueryUnescape(title); err == nil {
				title = esc
			}
		}
		mtype := mime.TypeByExtension(ext)
		//(numItems-1)-
		rss.Items[i] = &feeds.RssItem{
			Guid:        hash(item),
			Title:       title,
			Link:        item.URL,
			Description: fmt.Sprintf("Track %d of %d\n\n%s", i+1, numItems, f.Description),
			Enclosure: &feeds.RssEnclosure{
				Url:  item.URL,
				Type: mtype,
				// Length: "71894574",
			},
			Author:  f.Author,
			PubDate: f.Date.Add(-(time.Duration(i) * time.Hour)).Format(time.RFC822),
		}
	}

	str, _ := feeds.ToXML(rss)
	w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
	str = strings.Replace(str, "managingEditor>", "itunes:author>", 2)
	str = strings.Replace(str, `<rss version="2.0">`, `<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">`, 1)
	b = []byte(str)
	if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
		w.Header().Set("Content-Encoding", "gzip")
		gz := gzip.NewWriter(w)
		gz.Write(b)
		gz.Close()
	} else {
		w.Write(b)
	}
}