This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
extract_test.go
180 lines (151 loc) · 3.66 KB
/
extract_test.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
180
package swan
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
// So that we can read all the images
_ "image/gif"
_ "image/png"
)
type Result struct {
URL string
Expected Expected
}
type ExpectedTopImage struct {
Bytes uint
Confidence uint `json:"confidence_score"`
Height uint
Src string
Width uint
}
type Expected struct {
Authors []string
CleanedText string `json:"cleaned_text"`
Links int
MetaDescription string `json:"meta_description"`
MetaFavicon string `json:"meta_favicon"`
MetaKeywords string `json:"meta_keywords"`
MetaLang string `json:"meta_lang"`
OpenGraph map[string]string
PublishDate string `json:"publish_date"`
TopImage ExpectedTopImage `json:"top_image"`
Tags []string
Title string
}
var hijackOnce sync.Once
func hijiackHTTP() {
hijackOnce.Do(func() {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sum := sha1.Sum([]byte(r.URL.String()))
hash := hex.EncodeToString(sum[:])
path := fmt.Sprintf("test_data/imgs/%s", hash)
errPath := fmt.Sprintf("%s.err", path)
_, dataErr := os.Stat(path)
_, errErr := os.Stat(errPath)
if os.IsNotExist(dataErr) && os.IsNotExist(errErr) {
resp, err := http.Get(r.URL.String())
if err != nil || resp.StatusCode != 200 {
err = ioutil.WriteFile(errPath, nil, 0644)
if err != nil {
panic(err)
}
} else {
defer resp.Body.Close()
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// If an image, attempt to make it a single color to save
// space in the repo
b := bytes.NewBuffer(d)
img, _, err := image.Decode(b)
if err == nil {
b.Reset()
g := image.NewGray(img.Bounds())
err = jpeg.Encode(b, g, &jpeg.Options{Quality: 1})
if err == nil {
d = b.Bytes()
}
}
err = ioutil.WriteFile(path, d, 0644)
if err != nil {
panic(err)
}
}
}
if _, err := os.Stat(errPath); err == nil {
http.Error(w, "not found", 404)
} else {
d, _ := ioutil.ReadFile(path)
w.Write(d)
}
})
s := httptest.NewServer(h)
httpClient.Transport = http.DefaultTransport
purl, _ := url.Parse(s.URL)
httpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(purl),
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
})
}
func runPyTests(
t *testing.T,
dir string,
fn func(t *testing.T, name string, a *Article, r *Result)) {
hijiackHTTP()
filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
t.Fatalf("error while walking directory %s: %s", dir, err)
}
if info.IsDir() || !strings.HasSuffix(path, ".json") {
return nil
}
r := Result{}
name := strings.Replace(path, ".json", "", -1)
jsonf, err := os.Open(path)
if err != nil {
t.Fatalf("%s: %s", name, err)
}
defer jsonf.Close()
if err = json.NewDecoder(jsonf).Decode(&r); err != nil {
t.Fatalf("%s: %s", name, err)
}
h := strings.Replace(path, ".json", ".html", -1)
htmlf, err := os.Open(h)
if err != nil {
t.Fatalf("%s: %s", name, err)
}
defer htmlf.Close()
html, err := ioutil.ReadAll(htmlf)
if err != nil {
t.Fatalf("%s: %s", name, err)
}
a, err := FromHTML(r.URL, html)
if err != nil {
t.Fatalf("%s: %s", name, err)
}
fn(t, name, a, &r)
return nil
})
}