-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (61 loc) · 1.43 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/djavorszky/brink"
)
func main() {
config := flag.String("conf", "brink.toml", "Specify the configuration filename to be used")
out := flag.String("out", "std", "Specify where to log")
flag.Parse()
if *out != "std" {
f, err := os.Create(*out)
if err != nil {
fmt.Printf("Failed creating logfile: %v", err)
os.Exit(1)
}
defer f.Close()
log.SetOutput(f)
}
c, err := brink.NewCrawlerFromToml(*config)
if err != nil {
fmt.Printf("Failed initializing crawler: %v\n", err)
os.Exit(1)
}
ch := make(chan os.Signal, 2)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
c.Stop()
}()
c.HandleDefaultFunc(handler)
c.HandleFunc(http.StatusNotFound, notFoundHandler)
c.Start()
}
var oks int
func handler(linkedFrom, url string, status int, body string, cached bool) {
oks++
log.Printf("%d: %s", status, url)
if oks%100 == 0 {
log.Printf("Links seen: %d", oks)
}
if cached {
return
}
if strings.Contains(body, "Use the buttons below to create it or to search for the words in the title.") {
log.Printf("%s -> %s: linked wiki article does not exist", linkedFrom, url)
}
}
func notFoundHandler(linkedFrom, url string, status int, body string, cached bool) {
if cached {
log.Printf("404: CACHED: %s -> %s", linkedFrom, url)
} else {
log.Printf("404: %s -> %s", linkedFrom, url)
}
}