-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
274 lines (222 loc) · 5.85 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
package main
import (
"encoding/json"
"fmt"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
bolt "go.etcd.io/bbolt"
"io/ioutil"
"math/rand"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
type Brick struct {
Url string `json:"url"`
Likes int `json:"likes"`
Comment string `json:"comment"`
}
func (b Brick) Thumbnail() string {
if !strings.HasPrefix(b.Url, "https://tse") {
return b.Url
}
return b.Url + "&w=200"
}
func HandleErr(err error, title string) {
if err != nil {
if title == "" {
title = "Error"
}
fmt.Errorf("%s: %v", title, err)
}
}
func LoadBrick(s []byte) Brick {
var brick Brick
err := json.Unmarshal(s, &brick)
HandleErr(err, "Unmarshal Error")
//fmt.Println(brick)
return brick
}
func DumpBrick(brick Brick) []byte {
s, err := json.Marshal(brick)
HandleErr(err, "Marshal Error")
//fmt.Println(string(s))
return s
}
func GetBricks() []Brick {
var bricks []Brick
db, err := bolt.Open(DB_NAME, 0666, &bolt.Options{ReadOnly: true})
HandleErr(err, "DB Contection Error")
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(BUCKET_NAME)
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
bricks = append(bricks, LoadBrick(v))
}
return nil
})
HandleErr(err, "")
return bricks
}
func SaveBricks(bricks []Brick) {
ClearBricks()
db, err := bolt.Open(DB_NAME, 0666, nil)
HandleErr(err, "DB Contection Error")
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(BUCKET_NAME)
err = nil
for _, brick := range bricks {
key := []byte(brick.Url)
value := DumpBrick(brick)
err := b.Put(key, value)
HandleErr(err, "")
}
return err
})
HandleErr(err, "")
}
func ClearBricks() {
db, err := bolt.Open(DB_NAME, 0666, nil)
HandleErr(err, "DB Contection Error")
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(BUCKET_NAME)
c := b.Cursor()
err = nil
for k, _ := c.First(); k != nil; k, _ = c.Next() {
err := b.Delete(k)
HandleErr(err, "")
}
return err
})
HandleErr(err, "")
}
func ShuffleBricks(bs []Brick) []Brick {
rand.Seed(time.Now().UnixNano())
var i, j int
var temp Brick
for i = len(bs) - 1; i > 0; i-- {
j = rand.Intn(i + 1)
temp = bs[i]
bs[i] = bs[j]
bs[j] = temp
}
return bs
}
var DB_NAME = "data.db"
var BUCKET_NAME = []byte("bricks")
func main() {
db, err := bolt.Open(DB_NAME, 0666, nil)
HandleErr(err, "DB Contection Error")
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucket(BUCKET_NAME)
return nil
})
db.Close()
app := iris.New()
app.Logger().SetLevel("debug")
// Optionally, add two built'n handlers
// that can recover from any http-relative panics
// and log the requests to the terminal.
app.Use(recover.New())
app.Use(logger.New())
app.RegisterView(iris.HTML("./templates", ".html"))
app.Get("/", index)
app.Post("/upload_brick", uploadBrick)
app.Post("/delete_brick", deleteBrick)
app.Get("/clear", clear)
app.Get("/fetch", fetch)
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
func index(ctx iris.Context) {
bricks := GetBricks()
bricks = ShuffleBricks(bricks)
ctx.ViewData("bricks", bricks)
ctx.View("index.html")
}
func uploadBrick(ctx iris.Context) {
bricks := GetBricks()
url := ctx.FormValue("url")
if !strings.HasPrefix(url, "http") {
ctx.Redirect("/")
}
comment := ctx.FormValueDefault("comment", "")
brick := Brick{Url: url, Comment: comment}
bricks = append(bricks, brick)
SaveBricks(bricks)
ctx.Redirect("/")
}
func deleteBrick(ctx iris.Context) {
url := ctx.FormValue("url")
bricks := GetBricks()
for i, brick := range bricks {
if url == brick.Url {
bricks = append(bricks[:i], bricks[i+1:]...)
break
}
}
SaveBricks(bricks)
ctx.JSON(iris.Map{"status": "deleted"})
}
func clear(ctx iris.Context) {
ClearBricks()
ctx.JSON(iris.Map{"status": "clear"})
}
func fetch(ctx iris.Context) {
bricks := GetBricks()
//res, err := http.Get("https://www.google.com/search?q=gaudi&asearch=ichunk&async=_id:rg_s,_fmt:html")
//HandleErr(err, "")
//defer res.Body.Close()
//
//// Load the HTML document
//doc, err := goquery.NewDocumentFromReader(res.Body)
//fmt.Println(err)
//
//doc.Find("img").Each(func(i int, s *goquery.Selection) {
// // For each item found, get the band and title
// src, found := s.Attr("src")
// if !found {
// src, _ = s.Attr("data-src")
// }
// fmt.Println(src)
// brick := Brick{Url: src}
// bricks = append(bricks, brick)
//})
// https://cn.bing.com/images/async?q=gaudi&ensearch=1&mmasync=1&count=50&first=50
// 模拟采集与浏览器访问的结果有所不同,不明原因
n := ctx.URLParam("n")
if n == "" {
n = strconv.Itoa(rand.Intn(500))
}
url := "https://cn.bing.com/images/async?q=gaudi&ensearch=1&mmasync=1&count=50&first=" + n
//url = "http://127.0.0.1:8000"
fmt.Println(url)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
content := string(body)
//fmt.Println(res)
fmt.Println(content)
//exp := regexp.MustCompile(`src="(https://tse.*?)&`)
//exp := regexp.MustCompile(`"(https://tse\d.mm.bing.net/th\?id=.+?)&`)
exp1 := regexp.MustCompile(`(https://tse\d.mm.bing.net/th\?id=.+?)&`)
rs1 := exp1.FindAllStringSubmatch(content, -1)
// https://tse2-mm.cn.bing.net/th?id=OIP.N15LIiY_GkNFa5ZitU5dDwHaFj
exp2 := regexp.MustCompile(`(https://tse\d\-mm\.cn.bing.net/th\?id=.+?)&`)
rs2 := exp2.FindAllStringSubmatch(content, -1)
rs := append(rs1, rs2...)
for _, v := range rs {
fmt.Println(v[0], v[1])
brick := Brick{Url: v[1]}
bricks = append(bricks, brick)
}
SaveBricks(bricks)
ctx.Redirect("/")
}