Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(golint): put the files inside cmd/foo and add more go style #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions cmd/fetch/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
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++
}
}
bigcnt++
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!")
}
47 changes: 47 additions & 0 deletions cmd/loader/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

/*
so like wow there's no neural networks for go, CNN anyone?
idea is this and if you cheat and use python you are a big cheater
give all alleged street sign images 0.4 of street sign
and give all other images 0.01 chance of street sign
and maybe with the magic of neural networks we will learn?

TODO: don't be cheater and use python only golang pull request accepted
*/

import (
"image"
"image/png"
"log"
"os"
"path/filepath"
)

func main() {
var imgarr []image.Image

filepath.Walk("imgs/", func(path string, finfo os.FileInfo, err error) error {
if finfo.IsDir() {
return nil
}

println(path)

f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}

defer f.Close()

img, err := png.Decode(f)
if err != nil {
log.Fatal(err)
}

imgarr = append(imgarr, img)

return nil
})
}
144 changes: 0 additions & 144 deletions fetch.go

This file was deleted.

35 changes: 0 additions & 35 deletions loader.go

This file was deleted.