-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (172 loc) · 4.59 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/jxeng/site-info-crawler/tool"
"github.com/jxeng/site-info-crawler/types"
"github.com/chromedp/chromedp"
)
var ctx context.Context
func main() {
var items []types.Item
tool.ReadJsonFile("./raw.json", &items)
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", false),
chromedp.Flag("ignore-certificate-errors", true),
)
var cancel context.CancelFunc
ctx, _ = chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
for i := range items {
err := crawl(&items[i])
log.Println(items[i].Url, err)
}
tool.WriteJsonFile("./filled.json", &items)
}
func crawl(item *types.Item) error {
var urlstr = item.Url
var title = ""
var desc = ""
var iconPath = ""
var iconExists = false
iconFile := "./icons/" + item.Id
if _, err := os.Stat(iconFile + ".png"); err == nil {
iconExists = true
}
if _, err := os.Stat(iconFile + ".svg"); err == nil {
iconExists = true
}
if item.Description != "" && iconExists {
return nil
}
// use index.html to fetch site info
var iconJs = `
const iconElement = document.querySelector("link[rel~=icon]");
const iconPath = (iconElement && iconElement.href) || '';
iconPath
`
var descJs = `
let descElement = document.querySelector('meta[name="description"]');
if (!descElement) {
descElement = document.querySelector('meta[property="og:description"]');
}
const desc = (descElement && descElement.content) || "";
desc
`
resp, err := tool.Request(urlstr)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
if buf, err := ioutil.ReadAll(resp.Body); err == nil {
// title info
r, _ := regexp.Compile(`<title>(.+?)<\/title>`)
res := r.FindSubmatch(buf)
if len(res) > 1 {
title = string(res[1])
}
// description
r, _ = regexp.Compile(`<meta.+?(?:name|property)="(?:og:)?description" content="(.+?)"`)
res = r.FindSubmatch(buf)
if len(res) > 1 {
desc = string(res[1])
}
// favicon
r, _ = regexp.Compile(`<link.+?rel=".{0,20}?icon.{0,20}?".+?href="(.+?)"`)
res = r.FindSubmatch(buf)
if len(res) > 1 {
iconPath = string(res[1])
}
if iconPath == "" {
r, _ = regexp.Compile(`<link.+?href="(.+?)".+?rel=".{0,20}?icon.{0,20}?"`)
res = r.FindSubmatch(buf)
if len(res) > 1 {
iconPath = string(res[1])
}
}
} else {
log.Println(err)
}
} else {
log.Println(resp.Status)
}
// use chromedp to fetch site info
if title == "" || desc == "" || iconPath == "" {
if err := chromedp.Run(ctx,
chromedp.Navigate(urlstr),
chromedp.Sleep(5*time.Second),
); err != nil {
log.Println("chromedp error:", err)
}
if title == "" {
chromedp.Run(ctx, chromedp.Title(&title))
}
if iconPath == "" {
chromedp.Run(ctx, chromedp.Evaluate(iconJs, &iconPath))
}
if desc == "" {
chromedp.Run(ctx, chromedp.Evaluate(descJs, &desc))
}
}
tmp := strings.Split(string(title), " - ")
title = strings.Trim(tmp[0], " ")
if len(tmp) > 1 && desc == "" {
desc = strings.Trim(tmp[1], " ")
}
item.Title = title
item.Description = desc
if iconExists {
return nil
}
u, err := url.Parse(urlstr)
if err != nil {
return err
}
// favicon download url
var dlUrl string
if iconPath == "" {
dlUrl = fmt.Sprintf("%s://%s/favicon.ico", u.Scheme, u.Host)
} else if strings.HasPrefix(iconPath, "data:image") {
iconFile += ".png"
item.Favicon = iconFile
if err = tool.SaveIcon(iconPath, iconFile); err != nil {
return err
}
return nil
} else if strings.HasPrefix(iconPath, "http") {
dlUrl = iconPath
} else if strings.HasPrefix(iconPath, "//") {
dlUrl = "https:" + iconPath
} else if strings.HasPrefix(iconPath, "/") {
dlUrl = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, iconPath)
} else if strings.HasPrefix(iconPath, "../") {
fmt.Println(u.Host, u.Path)
dlUrl = fmt.Sprintf("%s://%s%s/%s", u.Scheme, u.Host, u.Path, iconPath)
} else {
path := ""
index := strings.LastIndex(u.Path, "/")
if index > -1 {
path = path[0:index]
}
dlUrl = fmt.Sprintf("%s://%s%s/%s", u.Scheme, u.Host, path, strings.TrimLeft(strings.TrimLeft(iconPath, "."), "/"))
}
if strings.HasSuffix(dlUrl, ".svg") {
iconFile += ".svg"
} else {
iconFile += ".png"
}
item.Favicon = iconFile
if err = tool.Download(dlUrl, iconFile); err != nil {
return err
}
return nil
}