-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
179 lines (161 loc) · 4.09 KB
/
client.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dyatl
import (
"bytes"
"crypto/tls"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"golang.org/x/text/encoding/charmap"
)
type Client struct {
http.Client
UserAgent string
AcceptLanguage string
}
func NewClient() Client {
c := Client{
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
AcceptLanguage: "ru-RU, ru;q=0.9, en-US;q=0.8, en;q=0.7",
}
c.Timeout = 5 * time.Second
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
TLSHandshakeTimeout: c.Timeout,
}
return c
}
var (
htmlRegex = regexp.MustCompile(`(?i)\s*<!DOCTYPE`)
redirectRegex = regexp.MustCompile(`<script[^>]*>\s*window.location.href = "([^"]+)"\s*</script>`)
)
func (c Client) Get(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.UserAgent)
req.Header.Add("Accept-Language", c.AcceptLanguage)
return c.Do(req)
}
var (
NotHttpUrl = errors.New("not http url")
PreviewNotFound = errors.New("preview not found")
)
type Preview struct {
Title string
ThumbnailUrl string
YoutubeId string
}
func (c Client) Preview(link string) (Preview, error) {
u, err := url.Parse(link)
if err != nil {
return Preview{}, err
}
if !(u.Scheme == "http" || u.Scheme == "https") {
return Preview{}, NotHttpUrl
}
if id := YoutubeIdFromUrl(u); id != "" {
data, _ := c.GetYoutubeData(id)
if data.ThumbnailUrl != "" {
p := Preview{
Title: data.Title,
ThumbnailUrl: data.ThumbnailUrl,
YoutubeId: id,
}
return p, nil
}
}
return c.previewFromContent(u)
}
func (c Client) previewFromContent(u *url.URL) (Preview, error) {
resp, err := c.Get(u.String())
if err != nil {
return Preview{}, err
}
defer resp.Body.Close()
ct := resp.Header.Get("Content-Type")
switch {
case ct == "":
buf := make([]byte, 20*1024)
if _, err := resp.Body.Read(buf); err != nil && err != io.EOF {
return Preview{}, err
}
if htmlRegex.Match(buf) {
return c.fromGoquery(u, bytes.NewReader(buf))
}
case strings.HasPrefix(ct, "text/html"):
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Preview{}, nil
}
bits := strings.Split(strings.ToLower(ct), "charset=")
if len(bits) == 2 {
switch bits[1] { // FIXME: all codepages
case "windows-1251":
dec := charmap.Windows1251.NewDecoder()
body, err = dec.Bytes(body)
if err != nil {
return Preview{}, errors.Wrap(err, "decode cp1251 fail")
}
}
}
if m := redirectRegex.FindSubmatch(body); len(m) > 0 {
if newLink := string(m[1]); newLink != u.String() {
return c.Preview(newLink)
}
}
return c.fromGoquery(u, bytes.NewReader(body))
case strings.HasPrefix(ct, "image/"):
return Preview{ThumbnailUrl: u.String()}, nil
}
return Preview{}, PreviewNotFound
}
func (c Client) fromGoquery(base *url.URL, r io.Reader) (Preview, error) {
q, err := goquery.NewDocumentFromReader(r)
if err != nil {
return Preview{}, errors.Wrap(err, "NewDocumentFromReader fail")
}
title := strings.Join(strings.Fields(q.Find("title").Text()), " ")
p := Preview{Title: title}
q.Find("meta").Each(func(i int, selection *goquery.Selection) {
switch selection.AttrOr("property", "") {
case "og:title":
if v := selection.AttrOr("content", ""); v != "" {
p.Title = v
}
case "og:image":
if v := selection.AttrOr("content", ""); v != "" {
switch {
case strings.HasPrefix(v, "//"):
p.ThumbnailUrl = "http:" + v
case strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "https://"):
p.ThumbnailUrl = v
case strings.HasPrefix(v, "/"):
p.ThumbnailUrl = base.Scheme + "://" + base.Host + v
}
}
}
})
return p, nil
}
func IsTimeout(err error) bool {
if err == nil {
return false
}
if err, ok := err.(net.Error); ok && err.Timeout() {
return true
}
if strings.Contains(err.Error(), "Client.Timeout exceeded") {
return true
}
return false
}