This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
308 lines (292 loc) · 7.3 KB
/
main.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"encoding/json"
"image/color"
"io"
"os"
"github.com/adrg/sysfont"
rl "github.com/gen2brain/raylib-go/raylib"
rg "github.com/jlortiz0/multisav/raygui-go"
)
var resolveMap map[string]Resolver
var siteReddit RedditSite
var sitePixiv PixivSite
var siteLemmy LemmySite
const TEXT_SIZE = 18
var font rl.Font
const (
SITE_LOCAL = iota
SITE_REDDIT
SITE_TWITTER_OBSELETE
SITE_PIXIV
SITE_LEMMY
)
type SavedListing struct {
Name string
Persistent interface{} `json:",omitempty"`
Args []interface{} `json:",omitempty"`
Site int
Kind int
}
var saveData struct {
Reddit string
Pixiv string
Lemmy struct {
Base string
User string
Pass string
}
Downloads string
Listings []SavedListing
Settings struct {
SaveOnX bool
HideOnZ bool
PixivBookPriv bool
}
}
func loadSaveData() error {
f, err := os.Open("multiSav.json")
if err != nil {
if os.IsNotExist(err) {
saveData.Downloads = "Downloads"
return nil
}
return err
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return err
}
return json.Unmarshal(data, &saveData)
}
func loginToSites() {
if _, err := os.Stat(saveData.Downloads); err != nil {
os.MkdirAll(saveData.Downloads, 0600)
}
siteReddit = NewRedditSite(saveData.Reddit)
resolveMap = make(map[string]Resolver)
for _, x := range siteReddit.GetResolvableDomains() {
resolveMap[x] = siteReddit
}
img := NewImgurResolver(ImgurID)
for _, x := range img.GetResolvableDomains() {
resolveMap[x] = img
}
sitePixiv = NewPixivSite(saveData.Pixiv)
for _, x := range sitePixiv.GetResolvableDomains() {
resolveMap[x] = sitePixiv
}
siteLemmy = NewLemmyClient(saveData.Lemmy.Base, saveData.Lemmy.User, saveData.Lemmy.Pass)
for _, x := range siteLemmy.GetResolvableDomains() {
resolveMap[x] = siteLemmy
}
temp := BlockingResolver{}
for _, x := range temp.GetResolvableDomains() {
resolveMap[x] = temp
}
for _, x := range StripQueryResolver(temp).GetResolvableDomains() {
resolveMap[x] = StripQueryResolver(temp)
}
for _, x := range PropOGImageResolver(temp).GetResolvableDomains() {
resolveMap[x] = PropOGImageResolver(temp)
}
for _, x := range RedgifsResolver(temp).GetResolvableDomains() {
resolveMap[x] = RedgifsResolver(temp)
}
for _, x := range PropOGVideoResolver(temp).GetResolvableDomains() {
resolveMap[x] = PropOGVideoResolver(temp)
}
for _, x := range RetryWOQueryResolver(temp).GetResolvableDomains() {
resolveMap[x] = RetryWOQueryResolver(temp)
}
saveSaveData()
}
func saveSaveData() error {
data, err := json.Marshal(saveData)
if err != nil {
return err
}
f, err := os.OpenFile("multiSav.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return err
}
for len(data) > 0 {
n, err := f.Write(data)
if err != nil {
f.Close()
return err
}
data = data[n:]
}
return f.Close()
}
func main() {
rl.SetConfigFlags(rl.FlagVsyncHint)
rl.SetConfigFlags(rl.FlagWindowResizable)
rl.InitWindow(1024, 768, "multiSav")
finder := sysfont.NewFinder(nil)
font = rl.LoadFontEx(finder.Match("Ubuntu Medium").Filename, TEXT_SIZE, nil, 0)
rl.SetExitKey(0)
rg.GuiSetFont(font)
rg.GuiSetStyle(rg.LABEL, rg.TEXT_COLOR_NORMAL, 0xf5f5f5ff)
// rg.GuiSetStyle(rg.LABEL, rg.TEXT_ALIGNMENT, rg.TEXT_ALIGN_RIGHT)
if _, err := os.Stat("jlortiz_TEST"); err == nil {
os.Chdir("jlortiz_TEST")
}
os.Mkdir("Downloads", 0700)
err := loadSaveData()
if err != nil {
panic(err)
}
loginToSites()
fadeOut(func() { rl.ClearBackground(rl.Black) })
MainLoop:
for {
names := make([]string, len(saveData.Listings)+1, len(saveData.Listings)+4)
names[0] = "Downloads"
for i, v := range saveData.Listings {
names[i+1] = v.Name
}
names = append(names, "Edit Listings", "Options", "Quit")
cm := NewChoiceMenu(names)
if stdEventLoop(cm) != LOOP_EXIT {
break
}
sel := cm.Selected
cm.Destroy()
switch sel {
case len(saveData.Listings) + 1:
if EditListings() {
break MainLoop
}
case len(saveData.Listings) + 2:
if SetUpSites() {
break MainLoop
}
case len(saveData.Listings) + 3:
break MainLoop
case 0:
menu := NewImageMenu(func() <-chan ImageProducer {
ch := make(chan ImageProducer)
go func() {
ch <- NewOfflineImageProducer(saveData.Downloads)
}()
return ch
})
if stdEventLoop(menu) == LOOP_QUIT {
break MainLoop
}
menu.Destroy()
rl.SetWindowTitle("multiSav")
default:
data := saveData.Listings[sel-1]
menu := NewImageMenu(func() <-chan ImageProducer {
ch := make(chan ImageProducer)
go func() {
var producer ImageProducer
switch data.Site {
case SITE_LOCAL:
producer = NewOfflineImageProducer(data.Args[0].(string))
case SITE_REDDIT:
producer = NewRedditProducer(siteReddit, data.Kind, data.Args, data.Persistent)
case SITE_PIXIV:
producer = NewPixivProducer(sitePixiv, data.Kind, data.Args, data.Persistent)
case SITE_LEMMY:
producer = NewLemmyProducer(siteLemmy, data.Kind, data.Args, data.Persistent)
}
ch <- producer
}()
return ch
})
if stdEventLoop(menu) == LOOP_QUIT {
break MainLoop
}
if menu.Producer != nil {
listing := menu.Producer.GetListing()
if listing != nil {
saveData.Listings[sel-1].Persistent = listing.GetPersistent()
}
}
menu.Destroy()
rl.SetWindowTitle("multiSav")
}
}
fadeIn(func() { rl.ClearBackground(rl.Black) })
rl.UnloadFont(font)
rl.CloseWindow()
siteReddit.Destroy()
sitePixiv.Destroy()
err = saveSaveData()
if err != nil {
panic(err)
}
}
func drawMessage(text string) *rl.Image {
vec := rl.MeasureTextEx(font, text, TEXT_SIZE, 0)
img := rl.GenImageColor(int(vec.X)+16, int(vec.Y)+10, rl.RayWhite)
rl.ImageDrawTextEx(img, rl.Vector2{X: 8, Y: 5}, font, text, TEXT_SIZE, 0, rl.Black)
return img
}
func messageOverlay(text string, menu Menu) LoopStatus {
msgI := drawMessage(text)
msg := rl.LoadTextureFromImage(msgI)
rl.UnloadImage(msgI)
defer rl.UnloadTexture(msg)
x := (int32(rl.GetScreenWidth()) - msg.Width) / 2
y := (int32(rl.GetScreenHeight()) - msg.Height) / 2
for !rl.WindowShouldClose() {
if rl.GetKeyPressed() != 0 || rl.IsMouseButtonPressed(rl.MouseLeftButton) {
return LOOP_BACK
}
if rl.IsWindowResized() {
x = (int32(rl.GetScreenWidth()) - msg.Width) / 2
y = (int32(rl.GetScreenHeight()) - msg.Height) / 2
menu.RecalcTarget()
}
menu.Prerender()
rl.BeginDrawing()
rl.ClearBackground(color.RGBA{R: 64, G: 64, B: 64})
menu.Renderer()
col := color.RGBA{A: 128}
if saveData.Settings.HideOnZ {
col = color.RGBA{R: 64, G: 64, B: 64, A: 255}
}
rl.DrawRectangle(0, 0, int32(rl.GetScreenWidth()), int32(rl.GetScreenHeight()), col)
rl.DrawTexture(msg, x, y, rl.White)
rl.EndDrawing()
}
return LOOP_QUIT
}
func stdEventLoop(menu Menu) LoopStatus {
fadeIn(menu.Renderer)
for !rl.WindowShouldClose() {
key := rl.GetKeyPressed()
for key != 0 {
ret := menu.HandleKey(key)
if ret != LOOP_CONT {
if ret != LOOP_QUIT {
fadeOut(menu.Renderer)
}
return ret
}
key = rl.GetKeyPressed()
}
if rl.IsWindowResized() {
menu.RecalcTarget()
}
ret := menu.Prerender()
if ret != LOOP_CONT {
if ret != LOOP_QUIT {
fadeOut(menu.Renderer)
}
return ret
}
rl.BeginDrawing()
rl.ClearBackground(color.RGBA{R: 64, G: 64, B: 64})
menu.Renderer()
rl.EndDrawing()
}
fadeOut(menu.Renderer)
return LOOP_QUIT
}