-
Notifications
You must be signed in to change notification settings - Fork 0
/
iqdbAPI.go
77 lines (66 loc) · 1.36 KB
/
iqdbAPI.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
package iqdbAPI
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
)
func API(input Options) string {
if input.FilePath != "" {
return API2(input.FilePath)
} else {
if input.Url != "" {
return API1(input.Url)
}
}
return "Invalid Input!"
}
func API1(target string) string {
// Request the HTML page.
res, err := http.PostForm("https://iqdb.org/",
url.Values{"url": {target}})
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
result := HtmlProcessor(res)
return result
}
func API2(filename string) string {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
// this step is very important
fileWriter, err := bodyWriter.CreateFormFile("file", filename)
if err != nil {
fmt.Println("error writing to buffer")
log.Fatal(err)
}
// open file handle
fh, err := os.Open(filename)
if err != nil {
fmt.Println("error opening file")
log.Fatal(err)
}
defer fh.Close()
//iocopy
_, err = io.Copy(fileWriter, fh)
if err != nil {
log.Fatal(err)
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post("https://iqdb.org/", contentType, bodyBuf)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
result := HtmlProcessor(resp)
return result
}