-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
507 lines (478 loc) · 11.4 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package main
import (
"compress/gzip"
"encoding/xml"
"flag"
"fmt"
"golang.org/x/net/html"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
)
const (
version = "1.0"
defaultUA = "Optimus Sitemap Generator/" + version + " (http://patrickmylund.com/projects/osg/)"
)
var (
// Sitemap time format is 2012-05-06T02:23:17+00:00
SitemapTimeFormat = strings.Replace(time.RFC3339, "Z", "+", -1)
MaxDepth = 500
throttle *uint = flag.Uint("c", 1, "pages to crawl at once")
maxCrawl *uint = flag.Uint("max-crawl", 0, "maximum number of pages to crawl (0 = unlimited)")
useragent *string = flag.String("ua", defaultUA, "User-Agent header to send")
gzipLevel *int = flag.Int("gzip-level", gzip.DefaultCompression, "compression level when generating a sitemap.xml.gz file (1 = fastest, 9 = best, -1 = default)")
verbose *bool = flag.Bool("v", false, "show additional information about the generation process")
nowarn *bool = flag.Bool("no-warn", false, "don't warn about pages that were not opened successfully")
noRobots *bool = flag.Bool("no-robots", false, "ignores domains' robots.txt and page nofollow directives")
noLastmod *bool = flag.Bool("no-lastmod", false, "don't include last-modified information in sitemap entries")
indent *bool = flag.Bool("indent", false, "indent the entries with a tab")
)
var (
oneCrawl chan bool
sem chan bool
client = http.DefaultClient
all = map[string]*Result{}
mu = sync.Mutex{}
roots []*url.URL
)
type TagType int
const (
Unknown TagType = iota
A
Meta
)
type tag struct {
Type TagType
Href string
Rel string
HttpEquiv string
Content string
}
type Crawler struct {
Root *url.URL
Ch chan<- *Result
wg *sync.WaitGroup
}
type Result struct {
Loc string
Lastmod string
}
func (c *Crawler) Crawl(u *url.URL, root *url.URL, lastmod string) {
defer c.wg.Done()
<-sem
var (
since *time.Time
err error
)
s := u.String()
mu.Lock()
_, found := all[s]
if found {
if *verbose {
log.Println("Already crawled", s+"; skipping")
}
mu.Unlock()
return
}
if lastmod != "" {
parsed, err := time.Parse(SitemapTimeFormat, lastmod)
if err != nil {
log.Println("Couldn't parse Lastmod value in sitemap entry for", s+":", err)
} else {
since = &parsed
}
}
r := &Result{
Loc: s,
}
all[s] = r
mu.Unlock()
// TODO: abort if changefreq hasn't been reached
defer func() {
c.Ch <- r
}()
if *maxCrawl > 0 {
_, ok := <-oneCrawl
if !ok {
return
}
}
if *verbose {
log.Println("Opening", s)
}
res, err := Get(s, since)
if err != nil {
log.Println("Error opening", s+":", err)
return
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
log.Println("Link to", s, "is broken (404 Not Found)")
return
}
lm, err := time.Parse(http.TimeFormat, res.Header.Get("Last-Modified"))
r.Lastmod = lm.Format(SitemapTimeFormat)
ct := res.Header.Get("Content-Type")
var dontCrawl bool
if strings.HasPrefix(ct, "text/html") {
// Proceed
} else if strings.HasPrefix(ct, "text/xml") {
// TODO: Read sitemaps
log.Println("Found a sitemap (child sitemap discovery not yet implemented)")
} else {
if *verbose {
log.Println("Not crawling", s, "(content type "+ct+")")
}
dontCrawl = true
}
if !dontCrawl {
links := c.GetLinks(res.Body, u, root)
c.wg.Add(len(links))
for _, v := range links {
if *verbose {
log.Println("Following link on", u, "to", v)
}
sem <- true
go c.Crawl(v, root, "")
}
}
}
func (c *Crawler) GetLinks(r io.Reader, u *url.URL, root *url.URL) []*url.URL {
shouldInclude := func(u *url.URL) bool {
s := u.String()
for _, v := range roots {
for _, ov := range []string{"http://" + v.Host, "https://" + v.Host} {
if strings.Contains(s, ov) {
return true
}
}
}
return false
}
links := GetLinks(r, u)
if *verbose && len(links) == 0 {
log.Println("Found no links on", u)
}
var nlinks []*url.URL
for _, v := range links {
if v.Scheme == "" {
v.Scheme = root.Scheme
}
if v.Host == "" {
v.Host = root.Host
}
if *verbose {
log.Println("Found a link on", u, "to", v)
}
if shouldInclude(v) {
nlinks = append(nlinks, v)
} else {
if *verbose {
log.Println(v, "outside specified domain(s); ignoring")
}
}
}
return nlinks
}
func GetLinks(r io.Reader, u *url.URL) (links []*url.URL) {
z := html.NewTokenizer(r)
depth := 0
tree := make([]*tag, MaxDepth)
L:
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
err := z.Err()
if err != io.EOF {
log.Println("Couldn't process", u, "- Error:", err.Error())
}
break L
case html.StartTagToken:
depth++
if depth >= MaxDepth {
log.Println("Reached MaxDepth on", u.String()+"; skipping")
return links
}
t := new(tag)
tree[depth] = t
tn, _ := z.TagName()
switch string(tn) {
default:
t.Type = Unknown
case "a":
t.Type = A
case "meta":
t.Type = Meta
}
for {
key, val, more := z.TagAttr()
k := strings.TrimSpace(string(key)) // Trim just to be sure
v := strings.TrimSpace(string(val))
// vi, _ := strconv.Atoi(v)
switch t.Type {
case A:
switch k {
case "href":
t.Href = v
case "rel":
t.Rel = v
}
case Meta:
switch k {
case "http-equiv":
t.HttpEquiv = v
case "content":
t.Content = v
}
}
if !more {
break
}
}
case html.EndTagToken:
t := tree[depth]
if t == nil {
log.Println("Malformed page", u, "- cannot reliably determine start/beginning of tags; aborting")
return nil // TODO: Remember the starting type and recover some bugs?
}
if t.Type == Meta && strings.Contains(t.Content, "nofollow") {
if *noRobots {
if *verbose {
log.Println("Ignoring nofollow directive on", u)
}
} else {
if *verbose {
log.Println(u, "has a nofollow directive; skipping page")
}
return links
}
} else if t.Type == A && t.Href != "" {
ignore := false
if t.Href[0] == '#' {
ignore = true
}
if strings.Contains(t.Rel, "nofollow") {
if *noRobots {
if *verbose {
log.Println("Ignoring nofollow directive on link to", t.Href, "on page", u)
}
} else {
if *verbose {
log.Println("Link to", t.Href, "on page", u, "has a nofollow directive; skipping link")
}
ignore = true
}
}
if !ignore {
u, err := url.Parse(t.Href)
if err != nil {
log.Println("Error parsing URL", t.Href, "on", u.String()+": ", err.Error())
} else {
links = append(links, u)
}
}
}
depth--
}
}
return links
}
type Url struct {
Loc string `xml:"loc"`
Lastmod string `xml:"lastmod,omitempty"`
// Changefreq string `xml:"changefreq"`
// Priority float64 `xml:"priority"`
}
type Urlset struct {
// TODO: How to add Schema links/info?
XMLName xml.Name `xml:"urlset"`
Url []Url `xml:"url"`
}
func Get(url string, ifmod *time.Time) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if ifmod != nil {
timeStr := ifmod.Format(http.TimeFormat)
req.Header.Add("If-Modified-Since", timeStr)
}
req.Header.Add("User-Agent", *useragent)
return client.Do(req)
}
func GetUrlsFromSitemap(r io.Reader) (urlset *Urlset, err error) {
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("Panic unserializing sitemap: %v", x)
}
}()
err = xml.NewDecoder(r).Decode(&urlset)
return
}
func generateSitemap(path string, urls []*url.URL) (*Urlset, error) {
ch := make(chan *Result, 1)
wg := new(sync.WaitGroup)
l := len(urls)
wg.Add(l)
cs := make(map[string]*Crawler)
for _, v := range urls {
r := getRoot(v)
roots = append(roots, r)
c := &Crawler{r, ch, wg}
cs[r.String()] = c
go func() {
sem <- true
c.Crawl(v, r, "")
}()
}
var fs io.ReadCloser
fs, err := os.Open(path)
if err != nil {
log.Println("Couldn't read existing sitemap", path+":", err.Error()+"; continuing")
} else {
defer fs.Close()
if strings.HasSuffix(path, ".gz") {
if *verbose {
log.Println("Extracting compressed data")
}
nfs, err := gzip.NewReader(fs)
if err != nil {
log.Println("Couldn't start decompression for gzipped file", path)
} else {
fs = nfs
defer fs.Close()
}
}
urlset, err := GetUrlsFromSitemap(fs)
if err != nil {
log.Println("Couldn't parse existing sitemap", path+":", err.Error()+"; continuing")
} else {
for _, v := range urlset.Url {
u, err := url.Parse(v.Loc)
if err != nil {
log.Println("Couldn't parse URL", v.Loc, "from existing sitemap", path+":", err.Error()+"; continuing")
} else {
r := getRoot(u)
s := r.String()
oc, found := cs[s]
if found {
go oc.Crawl(u, r, v.Lastmod)
} else if *verbose {
log.Println("Skipping URL", v.Loc, "not on the same domain ("+s+")")
}
}
}
}
}
go func() {
wg.Wait()
close(ch)
}()
sm := new(Urlset)
for {
res, ok := <-ch
if !ok {
break
}
if res != nil {
u := Url{
Loc: res.Loc,
}
if *noLastmod {
u.Lastmod = ""
} else {
u.Lastmod = res.Lastmod
}
sm.Url = append(sm.Url, u)
}
}
return sm, nil
}
func getRoot(u *url.URL) *url.URL {
r := new(url.URL)
r.Scheme = u.Scheme
r.Host = u.Host
return r
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
fmt.Println("Optimus Sitemap Generator", version)
fmt.Println("http://patrickmylund.com/projects/osg/")
fmt.Println("-----")
flag.Usage()
fmt.Println("")
fmt.Println("Examples:")
fmt.Println(" ", os.Args[0], "sitemap.xml example.com")
fmt.Println(" ", os.Args[0], "sitemap.xml site1.com site2.com https://site3.com")
fmt.Println(" ", os.Args[0], "-c 10 sitemap.xml example.com")
fmt.Println("")
fmt.Println("Use OSG on an existing sitemap to crawl only pages which have changed since the last run.")
return
}
sem = make(chan bool, *throttle)
path := flag.Arg(0)
urlStrings := flag.Args()[1:]
var urls []*url.URL
for _, v := range urlStrings {
if !strings.HasPrefix(v, "http://") && !strings.HasPrefix(v, "https://") {
v = "http://" + v
}
u, err := url.Parse(v)
if err != nil {
log.Println("Couldn't parse URL", v+": "+err.Error())
continue
}
if u.Path == "" {
u.Path = "/"
}
urls = append(urls, u)
}
if *maxCrawl > 0 {
oneCrawl = make(chan bool)
go func() {
for i := uint(0); i < *maxCrawl; i++ {
oneCrawl <- true
}
if *verbose {
log.Println("Maximum crawl limit of", *maxCrawl, "reached; not crawling any more pages")
}
close(oneCrawl) // when channel is closed, crawler.Crawl will not open any more pages
}()
}
sm, err := generateSitemap(path, urls)
if err != nil {
log.Fatalln("Error generating sitemap:", err)
}
var f io.WriteCloser
f, err = os.Create(path)
if err != nil {
log.Fatalln("Couldn't open", path, "for writing"+": "+err.Error())
}
defer f.Close()
if strings.HasSuffix(path, ".gz") {
if *verbose {
log.Println("Enabling compression")
}
f, err = gzip.NewWriterLevel(f, *gzipLevel)
if err != nil {
log.Fatalln("Couldn't create gzip writer for", path)
}
defer f.Close()
}
fmt.Fprintf(f, `<?xml version="1.0" encoding="UTF-8"?>`)
enc := xml.NewEncoder(f)
if *indent {
enc.Indent("", "\t")
}
err = enc.Encode(sm)
if err != nil {
log.Fatalln("Couldn't serialize sitemap", path+":", err)
}
}