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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package main
import (
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"
)
var (
// outputMode is either HTML or RSS
outputMode string
// cache will hold all previous network calls and re-use within set timeframe
cache safeCache
// cacheTimeout sets the time in in seconds for how long a channel will be cached
cacheTimeout float64
)
func init() {
outputMode = "html"
cache = safeCache{v: make(map[string]*channel)}
cacheTimeout = 60 * 15 // seconds
}
type safeCache struct {
sync.Mutex
v map[string]*channel
}
type channel struct {
Title string
Link string
Name string
Time time.Time
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"), 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) {
if r.URL.Path == "/favicon.ico" {
return
}
group := r.URL.Path[1:]
if strings.HasSuffix(group, ".rss") {
group = strings.TrimSuffix(group, ".rss")
outputMode = "rss"
}
c, ok := cache.v[group]
if !ok || time.Now().Sub(c.Time).Seconds() > cacheTimeout {
var err error
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
c.Time = time.Now()
cache.Lock()
cache.v[c.Name] = c
cache.Unlock()
}
fmt.Fprintf(w, "%s\n", c.String())
}
func fetch(group string) (c *channel, err error) {
if group == "" {
return
}
url := "https://www.facebook.com/pg/" + group + "/posts/"
log.Println("Fetching: ", url)
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
}
func main() {
log.Println("Serving: http://localhost:1212")
http.ListenAndServe(":1212", handler{})
}
|