-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfetch.go
161 lines (141 loc) · 3.01 KB
/
fetch.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
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/disintegration/imaging"
"golang.org/x/net/html"
"image"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"strings"
"time"
)
const apiKey string = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"
func fetchImg(ck string) image.Image {
// fetch the image
u, err := url.Parse("http://google.com/recaptcha/api2/payload")
if err != nil {
log.Fatal(err)
}
q := u.Query()
q.Set("c", ck)
q.Set("k", apiKey)
u.RawQuery = q.Encode()
// do fetch
imgresponse, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(imgresponse.Body)
if err != nil {
log.Fatal(err)
}
return img
}
func getChallengeKey() (string, string, image.Image) {
// build the request
u, err := url.Parse("http://google.com/recaptcha/api/fallback")
if err != nil {
log.Fatal(err)
}
q := u.Query()
q.Set("k", apiKey)
u.RawQuery = q.Encode()
//fmt.Println(u)
// fetch the webpage
response, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// print it
bodyBytes, _ := ioutil.ReadAll(response.Body)
z := html.NewTokenizer(ioutil.NopCloser(bytes.NewBuffer(bodyBytes)))
tmparr := []string{}
ck := ""
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
return ck, tmparr[3], fetchImg(ck)
case html.StartTagToken, html.SelfClosingTagToken:
tn, attr := z.TagName()
if string(tn) == "img" && attr {
for {
k, v, attr := z.TagAttr()
if string(k) == "src" {
//fmt.Println(string(v))
u, err := url.Parse(string(v))
if err != nil {
log.Fatal(err)
}
q := u.Query()
//fmt.Println(q)
if q["k"][0] != apiKey {
log.Fatal("apiKey doesn't match")
}
ck = q["c"][0]
}
if !attr {
break
}
}
}
case html.TextToken:
//fmt.Println(z.Token())
tmparr = append(tmparr, z.Token().String())
}
}
}
func downloader() {
bigcnt := 0
for {
// parse it
ck, typ, img := getChallengeKey()
h := md5.New()
io.WriteString(h, ck)
hh := hex.EncodeToString(h.Sum(nil))
typ = strings.Replace(typ, " ", "_", -1)
//fmt.Println(ck, typ, img.Bounds())
fmt.Println(bigcnt, hh, typ, img.Bounds())
if img.Bounds() != image.Rect(0, 0, 300, 300) {
log.Fatal("IMAGE IS THE WRONG SIZE")
}
// write it
os.MkdirAll("imgs/"+typ, 0755)
cnt := 0
for h := 0; h < 300; h += 100 {
for w := 0; w < 300; w += 100 {
lilimg := imaging.Crop(img, image.Rect(w, h, w+100, h+100))
fn := fmt.Sprintf("imgs/%s/%s_%d.png", typ, hh, cnt)
f, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
png.Encode(f, lilimg)
f.Close()
cnt += 1
}
}
bigcnt += 1
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
}
}
func main() {
fmt.Println("my first golang program")
/*for i := 0; i < 8; i += 1 {
go downloader()
}*/
downloader()
// move on
fmt.Println("still alive!")
}