package main import ( "fmt" "net/http" "strings" "time" ) var outputMode string type channel struct { Title string Link string Name string Description string Items []*post } func (c *channel) String() string { var template string switch outputMode { case "html": template = htmlRoot case "rss": template = rssRoot } var s string s = strings.Replace(template, "{{title}}", c.Title, -1) s = strings.Replace(s, "{{link}}", c.Link, -1) s = strings.Replace(s, "{{description}}", c.Description, -1) s = strings.Replace(s, "{{name}}", c.Name, -1) var items string for i := range c.Items { item := c.Items[i].String() item = strings.Replace(item, "{{link}}", c.Link, -1) items += item } s = strings.Replace(s, "{{items}}", items, 1) return s } type post struct { Time time.Time Link string Content string Images []*image // list of urls } type image struct { Source string Caption string } func (p *post) String() string { var template string switch outputMode { case "html": template = htmlItem case "rss": template = rssItem } var s string // time format: Mon Jan 2 15:04:05 -0700 MST 2006 s = strings.Replace(template, "{{time}}", p.Time.Format("Mon, 2 Jan 2006 15:04:05 MST"), 2) s = strings.Replace(s, "{{content}}", p.Content, 1) var imgs string for i := range p.Images { imgs += `` } s = strings.Replace(s, "{{images}}", imgs, 1) return s } type handler struct{} func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { group := r.URL.Path[1:] if strings.HasSuffix(group, ".rss") { group = strings.TrimSuffix(group, ".rss") outputMode = "rss" } c, err := fetch(group) if err != nil { http.Error(w, fmt.Sprintf("error: %s", err), 400) return } if c == nil || len(c.Items) < 1 { http.Error(w, fmt.Sprintf("%s", "group not found"), 400) return } c.Name = group fmt.Fprintf(w, "%s\n", c.String()) } func main() { outputMode = "html" fmt.Println("Serving: http://localhost:1212") http.ListenAndServe(":1212", handler{}) } func fetch(group string) (c *channel, err error) { if group == "" { return } url := "https://www.facebook.com/pg/" + group + "/posts/" resp, err := http.Get(url) if err != nil { return } defer resp.Body.Close() c, err = parse(resp.Body) if err != nil { return } c.Link = url return }