forked from benjojo/bnblog
/
rss.go
82 lines (71 loc) · 2.4 KB
/
rss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"crypto/md5"
"encoding/base64"
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/feeds"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
func GetRSS(rw http.ResponseWriter, req *http.Request) {
now := time.Now()
feed := &feeds.Feed{
Title: "cynthia's blog",
Link: &feeds.Link{Href: "https://blog.cynthia.re"},
Description: "Programming, Networking and some things I found hard to fix at some point",
Author: &feeds.Author{"Cynthia Revström", "me@cynthia.re"},
Created: now,
}
c := appengine.NewContext(req)
q := datastore.NewQuery("Post").Order("-Date").Limit(100)
posts := make([]Post, 0, 100)
if _, err := q.GetAll(c, &posts); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
feed.Items = []*feeds.Item{}
for _, v := range posts {
if !strings.HasPrefix(v.Slug, "DRAFT-") {
// newpost := PostFormatted{
// Author: v.Author,
// Content: v.Content,
// Date: v.Date.Format("2006-01-02 15:04:05"),
// Slug: v.Slug,
// Title: v.Title,
// }
// FormattedPosts = append(FormattedPosts, newpost)
postd, _ := base64.StdEncoding.DecodeString(v.Content)
wot := &feeds.Item{
Title: v.Title,
Link: &feeds.Link{Href: fmt.Sprintf("https://blog.cynthia.re/post/%s", v.Slug)},
Description: string(postd[:256]),
Author: &feeds.Author{"me@cynthia.re", "me@cynthia.re"},
Created: v.Date,
Id: generateBadUUID(v.Title),
}
feed.Items = append(feed.Items, wot)
}
}
rss, err := feed.ToRss()
if err != nil {
http.Error(rw, fmt.Sprintf("argh %s", err), http.StatusInternalServerError)
return
}
rw.Header().Add("Content-Type", "application/rss+xml; charset=utf-8")
rw.Write([]byte(rss))
}
// This is a hack as you might have guessed. This blogging system was
// never designed with UUIDs in mind, so I'm sort of just generating one
// out of the title, MD5 is fine since I don't think I am going attack
// myself with colliding titles.
func generateBadUUID(title string) string {
hashbytes := md5.Sum([]byte(title))
return fmt.Sprintf("%1x%1x%1x%1x-%1x%1x-40%1x-%1x%1x-%1x%1x%1x%1x%1x%1x",
hashbytes[0], hashbytes[1], hashbytes[2], hashbytes[3], hashbytes[4],
hashbytes[5], hashbytes[6], hashbytes[7], hashbytes[8], hashbytes[9],
hashbytes[10], hashbytes[11], hashbytes[12], hashbytes[13],
hashbytes[14])
}