-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
153 lines (143 loc) · 3.38 KB
/
http.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
149
150
151
152
153
package main
import (
"fmt"
"io"
"mime"
"net/http"
"strings"
"sync"
"time"
"github.com/antchfx/goreadly"
"github.com/antchfx/htmlquery"
"github.com/sirupsen/logrus"
"github.com/julienschmidt/httprouter"
"github.com/zhengchun/syndfeed"
"golang.org/x/net/html/charset"
)
var httpClient = &http.Client{
Timeout: time.Second * 45,
}
func httpGet(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
// bug has fixed: https://github.com/golang/go/issues/18779
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
r, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type"))
if err != nil {
resp.Body.Close()
return nil, err
}
resp.Body = &responseReader{rc: resp.Body, r: r}
return resp, nil
}
type responseReader struct {
rc io.ReadCloser
r io.Reader
}
func (r *responseReader) Read(p []byte) (int, error) {
return r.r.Read(p)
}
func (r *responseReader) Close() error {
return r.rc.Close()
}
func FullRss(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
source := strings.TrimLeft(p.ByName("feed"), "/")
if source == "" || !(strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://")) {
w.WriteHeader(400)
w.Write([]byte(fmt.Sprintf("Invalid source feed(%s)", source)))
return
}
handleError := func(err error) {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
resp, err := httpGet(source)
if err != nil {
handleError(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
handleError(fmt.Errorf("%s got status-code is not 200(%d)", source, resp.StatusCode))
return
}
v := resp.Header.Get("Content-Type")
mediatype, _, err := mime.ParseMediaType(v)
var feed *syndfeed.Feed
switch mediatype {
case "text/xml",
"application/xml",
"application/rss+xml",
"application/atom+xml":
var err error
feed, err = syndfeed.Parse(resp.Body)
if err != nil {
handleError(err)
return
}
default:
handleError(fmt.Errorf("%s got mediatype is not supported(%s)", source, mediatype))
return
}
if len(feed.Items) > 0 {
if len(feed.Items) > *aItemCount {
feed.Items = feed.Items[:*aItemCount]
}
var wg sync.WaitGroup
c := make(chan struct{})
// create 2 worker to work.
var queue = make(chan *syndfeed.Item, 1)
for n := 0; n < *aConnectionPerFeed; n++ {
go func() {
for {
select {
case item := <-queue:
link := item.Links[0].URL
logrus.Debugf("%s", link)
if err := fulltext(item, link); err != nil {
wg.Done()
logrus.Warnf("GET %s failed. %s", link, err)
}
wg.Done()
case <-c:
return
}
}
}()
}
for _, item := range feed.Items {
if len(item.Links) > 0 {
wg.Add(1)
queue <- item
}
}
wg.Wait()
close(c)
}
// outout RSS 2.0
w.Header().Set("Content-Type", "application/xml")
outputRss20(w.(io.StringWriter), feed)
}
func fulltext(item *syndfeed.Item, link string) error {
resp, err := httpGet(link)
if err != nil {
return err
}
defer resp.Body.Close()
htmlDoc, err := htmlquery.Parse(resp.Body)
if err != nil {
return err
}
doc, err := goreadly.ParseHTML(resp.Request.URL, htmlDoc)
if err != nil {
return err
}
item.Content = doc.Body
return nil
}