/
spider_Cowyo.go
executable file
·174 lines (155 loc) · 3.45 KB
/
spider_Cowyo.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
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/boltdb/bolt"
"github.com/celrenheit/spider"
)
// Main data structures
type CowyoGroup struct {
Data []CowyoItem
}
type CowyoItem struct {
Name string
}
// Database functions
func (p *CowyoGroup) Get(bucketName string, typeData 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(typeData)
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 CowyoGroup: %s", err)
return err
}
return nil
}
func (p *CowyoGroup) save(typeData string) 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 CowyoGroup: %s", err)
}
err = bucket.Put([]byte(typeData), enc)
return err
})
return err
}
func (p *CowyoGroup) encode() ([]byte, error) {
enc, err := json.Marshal(p)
if err != nil {
return nil, err
}
return enc, nil
}
func (p *CowyoGroup) decode(data []byte) error {
err := json.Unmarshal(data, &p)
if err != nil {
return err
}
return nil
}
// Define the spider
var GroceriesSpider spider.Spider
var CoolListSpider spider.Spider
var TodoListSpider spider.Spider
func init() {
GroceriesSpider = spider.Get("http://cowyo.com/grocerylist", func(ctx *spider.Context) error {
fmt.Print(time.Now())
fmt.Println("GroceriesSpider")
if _, err := ctx.DoRequest(); err != nil {
return err
}
htmlparser, err := ctx.HTMLParser()
if err != nil {
return err
}
var p CowyoGroup
p.Data = []CowyoItem{}
dats := htmlparser.Find(`textarea `).Text()
for _, dat := range strings.Split(dats, "\n") {
if len(dat) > 0 {
p.Data = append(p.Data, CowyoItem{dat})
}
}
err = p.save("GroceryList")
if err != nil {
return fmt.Errorf("error saving CowyoGroup")
}
return nil
})
CoolListSpider = spider.Get("http://cowyo.com/cool", func(ctx *spider.Context) error {
fmt.Print(time.Now())
fmt.Println("CoolListSpider")
if _, err := ctx.DoRequest(); err != nil {
return err
}
htmlparser, err := ctx.HTMLParser()
if err != nil {
return err
}
var p CowyoGroup
p.Data = []CowyoItem{}
dats := htmlparser.Find(`textarea `).Text()
for _, dat := range strings.Split(dats, "\n") {
if len(dat) > 0 {
p.Data = append(p.Data, CowyoItem{dat})
}
}
err = p.save("CoolList")
if err != nil {
return fmt.Errorf("error saving CowyoGroup")
}
return nil
})
TodoListSpider = spider.Get("http://cowyo.com/todo", func(ctx *spider.Context) error {
fmt.Print(time.Now())
fmt.Println("TodoListSpider")
if _, err := ctx.DoRequest(); err != nil {
return err
}
htmlparser, err := ctx.HTMLParser()
if err != nil {
return err
}
var p CowyoGroup
p.Data = []CowyoItem{}
dats := htmlparser.Find(`textarea `).Text()
for _, dat := range strings.Split(dats, "\n") {
if len(dat) > 0 {
p.Data = append(p.Data, CowyoItem{dat})
}
}
err = p.save("TodoList")
if err != nil {
return fmt.Errorf("error saving TodoList")
}
return nil
})
}