-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
290 lines (279 loc) · 7.01 KB
/
Copy pathmain.go
File metadata and controls
290 lines (279 loc) · 7.01 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"encoding/json"
"fmt"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/explorer"
clipB "github.com/atotto/clipboard"
"github.com/changharry/iqdbAPI"
"image"
"image/color"
"image/jpeg"
_ "image/jpeg"
"image/png"
_ "image/png"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
go func() {
w := app.NewWindow(app.Title("DanbooruTag"))
if err := loop(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
type (
C = layout.Context
D = layout.Dimensions
)
type ImageResult struct {
Error error
Format string
Image image.Image
}
func loop(w *app.Window) error {
expl := explorer.NewExplorer(w)
var openBtn widget.Clickable
var boilDurationInput widget.Editor
var outputText widget.Editor
var startButton widget.Clickable
var startButton2 widget.Clickable
var clipboardButton widget.Clickable
var extension string
var output string
inputString := boilDurationInput.Text()
inputString = strings.TrimSpace(inputString)
th := material.NewTheme(gofont.Collection())
imgChan := make(chan ImageResult)
var img ImageResult
var ops op.Ops
var req string
for {
select {
case img = <-imgChan:
w.Invalidate()
case e := <-w.Events():
expl.ListenEvents(e)
switch e := e.(type) {
case system.DestroyEvent:
return e.Err
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
if openBtn.Clicked() {
go func() {
file, err := expl.ChooseFile("png", "jpeg", "jpg")
if err != nil {
err = fmt.Errorf("failed opening image file: %w", err)
imgChan <- ImageResult{Error: err}
return
}
defer file.Close()
imgData, format, err := image.Decode(file)
extension = format
imgChan <- ImageResult{Image: imgData, Format: format}
home, _ := os.UserHomeDir()
filePath, _ := filepath.Abs(home + "/req." + format)
f, err := os.Create(filePath)
if err != nil {
panic(e)
}
defer f.Close()
switch format {
case "jpeg":
if err := jpeg.Encode(f, imgData, nil); err != nil {
return
}
case "png":
if err := png.Encode(f, imgData); err != nil {
return
}
}
}()
}
if clipboardButton.Clicked() {
err := clipB.WriteAll(output)
if err != nil {
return err
}
}
if startButton.Clicked() {
home, _ := os.UserHomeDir()
req = iqdbAPI.API(iqdbAPI.Options{
FilePath: home + "/req." + extension})
var response iqdbAPI.Response
err := json.Unmarshal([]byte(req), &response)
if err != nil {
return nil
}
body := response.Results
for i := 0; i < len(body); i++ {
if body[i].Head == "Best match" || body[i].Head == "Additional match" {
if strings.Contains(body[i].Url, "danbooru") {
regexp1, _ := regexp.Compile(`\bTags.*\b`)
r := regexp1.FindString(body[i].Titles)
r = strings.ReplaceAll(r, " ", ",")
r = strings.ReplaceAll(r, "_", " ")
output = r[6:]
}
} else {
output = "NULL"
}
}
}
if startButton2.Clicked() {
req = iqdbAPI.API(iqdbAPI.Options{
Url: boilDurationInput.Text()})
var response iqdbAPI.Response
err := json.Unmarshal([]byte(req), &response)
if err != nil {
return nil
}
body := response.Results
for i := 0; i < len(body); i++ {
if body[i].Head == "Best match" || body[i].Head == "Additional match" {
if strings.Contains(body[i].Url, "danbooru") {
regexp1, _ := regexp.Compile(`\bTags.*\b`)
r := regexp1.FindString(body[i].Titles)
r = strings.ReplaceAll(r, " ", ",")
r = strings.ReplaceAll(r, "_", " ")
output = r[6:]
}
} else {
output = "NULL"
}
}
}
layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(material.Button(th, &openBtn, "Open Image").Layout),
layout.Flexed(1, func(gtx C) D {
if img.Error == nil && img.Image == nil {
return D{}
} else if img.Error != nil {
return material.H6(th, img.Error.Error()).Layout(gtx)
}
return widget.Image{
Src: paint.NewImageOp(img.Image),
Fit: widget.Contain,
}.Layout(gtx)
}),
layout.Rigid(material.H6(th, "Image URL:").Layout),
layout.Rigid(
func(gtx C) D {
ed := material.Editor(th, &boilDurationInput, "https://www.abc.com/xxx/123.jpg")
boilDurationInput.SingleLine = true
boilDurationInput.Alignment = text.Middle
margins := layout.Inset{
Top: unit.Dp(0),
Bottom: unit.Dp(40),
}
border := widget.Border{
Color: color.NRGBA{R: 204, G: 204, B: 204, A: 255},
CornerRadius: unit.Dp(3),
Width: unit.Dp(2),
}
return margins.Layout(gtx,
func(gtx C) D {
return border.Layout(gtx, ed.Layout)
},
)
},
),
layout.Rigid(
func(gtx C) D {
ed := material.Editor(th, &outputText, "Output will show here")
outputText.SetText(output)
outputText.Alignment = text.Middle
margins := layout.Inset{
Top: unit.Dp(0),
Bottom: unit.Dp(0),
}
border := widget.Border{
Color: color.NRGBA{R: 204, G: 204, B: 204, A: 255},
CornerRadius: unit.Dp(3),
Width: unit.Dp(2),
}
return margins.Layout(gtx,
func(gtx C) D {
return border.Layout(gtx, ed.Layout)
},
)
},
),
layout.Rigid(
func(gtx C) D {
// margins
margins := layout.Inset{
Top: unit.Dp(10),
Bottom: unit.Dp(25),
Right: unit.Dp(300),
Left: unit.Dp(300),
}
return margins.Layout(gtx,
func(gtx C) D {
var text string
text = "copy"
btn := material.Button(th, &clipboardButton, text)
return btn.Layout(gtx)
},
)
},
),
layout.Rigid(
func(gtx C) D {
// margins
margins := layout.Inset{
Top: unit.Dp(25),
Bottom: unit.Dp(25),
Right: unit.Dp(35),
Left: unit.Dp(35),
}
return margins.Layout(gtx,
func(gtx C) D {
var text string
text = "Search using local image"
btn := material.Button(th, &startButton, text)
return btn.Layout(gtx)
},
)
},
),
layout.Rigid(
func(gtx C) D {
// margins
margins := layout.Inset{
Top: unit.Dp(25),
Bottom: unit.Dp(25),
Right: unit.Dp(35),
Left: unit.Dp(35),
}
return margins.Layout(gtx,
func(gtx C) D {
var text string
text = "Search using url"
btn := material.Button(th, &startButton2, text)
return btn.Layout(gtx)
},
)
},
),
)
e.Frame(gtx.Ops)
}
}
}
}