-
Notifications
You must be signed in to change notification settings - Fork 0
/
diet.go
148 lines (122 loc) · 2.97 KB
/
diet.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
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
package main
import (
"encoding/xml"
"io/ioutil"
"log"
"net/http"
"regexp"
"strconv"
"github.com/gorilla/mux"
)
var pointsRegexp = regexp.MustCompile("(\\d+) points")
type RSS struct {
XMLName xml.Name `xml:"rss"`
Items Items `xml:"channel"`
}
type Items struct {
XMLName xml.Name `xml:"channel"`
ItemList []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Comments string `xml:"comments"`
}
func xmlHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/xml")
fn(w, r)
}
}
func main() {
root := mux.NewRouter()
root.HandleFunc("/", home)
http.Handle("/", root)
feeds := root.PathPrefix("/feeds").Subrouter()
feeds.HandleFunc("/hn/{min_points:[0-9]+}", xmlHandler(hn))
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello!"))
}
func checkPoints(sc chan Item, fc chan bool, item Item, minPoints int) {
response, error := http.Get(item.Comments)
if error != nil {
// http.Error(w, error.Error(), http.StatusBadGateway)
fc <- false
return
}
defer response.Body.Close()
html, error := ioutil.ReadAll(response.Body)
if error != nil {
// http.Error(w, error.Error(), http.StatusInternalServerError)
fc <- false
return
}
matches := pointsRegexp.FindStringSubmatch(string(html))
if len(matches) >= 2 {
points, error := strconv.Atoi(matches[1])
if error != nil {
// http.Error(w, error.Error(), http.StatusInternalServerError)
fc <- false
return
}
if points >= minPoints {
sc <- item
} else {
fc <- false
}
} else {
fc <- false
}
}
func hn(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
min_points, err := strconv.Atoi(params["min_points"])
if err != nil {
http.Error(w, "Provided min_points must be an integer", http.StatusBadRequest)
return
}
resp, err := http.Get("https://news.ycombinator.com/rss")
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Error reading YC response", http.StatusInternalServerError)
return
}
var i RSS
err = xml.Unmarshal(contents, &i)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var validItems []Item
var sc = make(chan Item)
var fc = make(chan bool)
for _, item := range i.Items.ItemList {
go checkPoints(sc, fc, item, min_points)
}
var s int
for s < len(i.Items.ItemList) {
select {
case item := <-sc:
validItems = append(validItems, item)
case <-fc:
// pass
}
s++
}
i.Items.ItemList = validItems
data, err := xml.MarshalIndent(i, "", " ")
if err != nil {
http.Error(w, "Error expanding to XML: "+err.Error(), http.StatusInternalServerError)
return
}
w.Write(data)
}