-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider_Self.go
executable file
·114 lines (98 loc) · 1.99 KB
/
spider_Self.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
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/boltdb/bolt"
"github.com/celrenheit/spider"
)
// Main data structures
type SelfData struct {
Html string
Date string
}
// Database functions
func (p *SelfData) Get(bucketName string) error {
if !open {
return fmt.Errorf("db must be opened before saving!")
}
err := db.View(func(tx *bolt.Tx) error {
var err error
b := tx.Bucket([]byte(bucketName))
if b == nil {
return nil
}
k := []byte("SelfData")
val := b.Get(k)
if val == nil {
return nil
}
err = p.decode(val)
if err != nil {
return err
}
return nil
})
if err != nil {
fmt.Printf("Could not get SelfData: %s", err)
return err
}
return nil
}
func (p *SelfData) save() error {
bucketName := Today()
if !open {
return fmt.Errorf("db must be opened before saving!")
}
err := db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
enc, err := p.encode()
if err != nil {
return fmt.Errorf("could not encode SelfData: %s", err)
}
err = bucket.Put([]byte("SelfData"), enc)
return err
})
return err
}
func (p *SelfData) encode() ([]byte, error) {
enc, err := json.Marshal(p)
if err != nil {
return nil, err
}
return enc, nil
}
func (p *SelfData) decode(data []byte) error {
err := json.Unmarshal(data, &p)
if err != nil {
return err
}
return nil
}
// Define the spider
var SelfSpider spider.Spider
func loadSelfSpider() {
SelfSpider = spider.Get("http://"+ExternalIP+"/", func(ctx *spider.Context) error {
fmt.Print(time.Now())
fmt.Println("SelfSpider")
if _, err := ctx.DoRequest(); err != nil {
fmt.Println(err)
return err
}
htmlparser, err := ctx.HTMLParser()
if err != nil {
fmt.Println(err)
return err
}
html, _ := htmlparser.Html()
p := SelfData{html, time.Now().String()}
err = p.save()
if err != nil {
return fmt.Errorf("error saving SelfData")
}
return nil
})
}