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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
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 += `<a href="` + p.Images[i].Source + `"><img src="` + p.Images[i].Source + `" title="` + p.Images[i].Caption + `"></a>`
}
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
}
|