-
Notifications
You must be signed in to change notification settings - Fork 5
/
image.go
144 lines (120 loc) Β· 3.11 KB
/
image.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
package binglib
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/Harry-zklcdc/bing-lib/lib/request"
"golang.org/x/net/html"
)
const (
bingImageCreateUrl = "%s/images/create?q=%s&rt=4&FORM=GENCRE"
bingImageResult = "%s/images/create/async/results/%s"
)
func NewImage(cookies string) *Image {
return &Image{
cookies: cookies,
BingBaseUrl: bingBaseUrl,
}
}
func (image *Image) Clone() *Image {
return &Image{
cookies: image.cookies,
xff: image.xff,
bypassServer: image.bypassServer,
BingBaseUrl: image.BingBaseUrl,
}
}
func (image *Image) SetBingBaseUrl(bingBaseUrl string) *Image {
image.BingBaseUrl = bingBaseUrl
return image
}
func (image *Image) SetCookies(cookies string) *Image {
image.cookies = cookies
return image
}
func (image *Image) SetXFF(xff string) *Image {
image.xff = xff
return image
}
func (image *Image) SetBypassServer(bypassServer string) *Image {
image.bypassServer = bypassServer
return image
}
func (image *Image) GetBingBaseUrl() string {
return image.BingBaseUrl
}
func (image *Image) GetCookies() string {
return image.cookies
}
func (image *Image) GetXFF() string {
return image.xff
}
func (image *Image) GetBypassServer() string {
return image.bypassServer
}
func (image *Image) Image(q string) ([]string, string, error) {
var res []string
c := request.NewRequest()
if image.xff != "" {
c.SetHeader("X-Forwarded-For", image.xff)
}
c.Post().SetUrl(bingImageCreateUrl, image.BingBaseUrl, url.QueryEscape(q)).
SetBody(strings.NewReader(url.QueryEscape(fmt.Sprintf("q=%s&qs=ds", q)))).
SetContentType("application/x-www-form-urlencoded").
SetHeader("Cookie", image.cookies).
SetHeader("User-Agent", userAgent).
SetHeader("Origin", "https://www.bing.com").
SetHeader("Referer", "https://www.bing.com/images/create/").
Do()
if c.Result.Status != 302 {
return res, "", fmt.Errorf("status code: %d", c.Result.Status)
}
u, _ := url.Parse(fmt.Sprintf("%s%s", image.BingBaseUrl, c.GetHeader("Location")))
c.Get().SetUrl("%s%s", image.BingBaseUrl, c.GetHeader("Location")).Do()
if c.Result.Status != 200 {
return res, "", fmt.Errorf("status code: %d", c.Result.Status)
}
id := u.Query().Get("id")
// fmt.Println(id)
i := 0
for i < 120 {
time.Sleep(1 * time.Second)
i++
c.Get().SetUrl(bingImageResult, image.BingBaseUrl, id).Do()
if len(c.GetBodyString()) > 1 && strings.Contains(c.GetHeader("Content-Type"), "text/html") {
break
}
}
if i >= 120 {
return res, "", fmt.Errorf("timeout")
}
// fmt.Println(c.GetBodyString())
body, err := html.Parse(strings.NewReader(c.GetBodyString()))
if err != nil {
return res, id, err
}
findImgs(body, &res)
var tmp []string
for i := range res {
if !strings.Contains(res[i], "/rp/") {
url, _ := url.Parse(res[i])
url.RawQuery = ""
tmp = append(tmp, url.String())
}
}
return tmp, id, nil
}
func findImgs(n *html.Node, vals *[]string) {
if n.Type == html.ElementNode && n.Data == "img" {
for _, a := range n.Attr {
if a.Key == "src" {
*vals = append(*vals, a.Val)
break
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
findImgs(c, vals)
}
}