-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
68 lines (56 loc) · 1.09 KB
/
article.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
package main
import (
"html/template"
"io"
"net/http"
"time"
readability "github.com/go-shiori/go-readability"
"github.com/motemen/go-pocket/api"
)
const timeOut = 30 * time.Second
const templ = `
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
<meta charset="UTF-8">
<link rel="canonical" href="{{.URL}}" />
</head>
<body>
<h1><a href="{{.URL}}">{{.Title}}</a></h1>
{{ if ne .Image "" }}
<img src="{{.Image}}" />
{{ end }}
{{.Body}}
</body>
`
var t *template.Template
func init() {
t = template.Must(template.New("article").Parse(templ))
}
type Article struct {
api.Item
}
func (a Article) Download() (io.Reader, error) {
r, w := io.Pipe()
resp, err := http.Get(a.URL())
if err != nil {
return nil, err
}
defer resp.Body.Close()
parser := readability.NewParser()
article, err := parser.Parse(resp.Body, a.URL())
if err != nil {
return nil, err
}
go func() {
t.Execute(w, map[string]interface{}{
"Body": template.HTML(article.Content),
"Url": a.URL(),
"Title": a.Title(),
"Image": article.Image,
})
w.Close()
}()
return r, err
}