-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
402 lines (347 loc) · 13.6 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"fmt"
"io"
"mime"
"net/http"
"path"
"regexp"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/lostdusty/gobalt"
)
var (
verifyLink = regexp.MustCompile(`[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?`)
blockClip bool
GualtoWin fyne.Window
GualtoApp fyne.App
cobaltInstances []string
)
func discoverCobaltInstances() {
asyncGetCobaltInstances, err := gobalt.GetCobaltInstances()
if err != nil {
dialog.ShowError(fmt.Errorf("failed to fetch more cobalt instances"), GualtoWin)
cobaltInstances = append(cobaltInstances, gobalt.CobaltApi)
return
}
for _, dcobaltInstances := range asyncGetCobaltInstances {
cobaltInstances = append(cobaltInstances, fmt.Sprintf("https://%v", dcobaltInstances.URL))
}
}
func main() {
newDownload := gobalt.CreateDefaultSettings() //Create default settings for downloading
GualtoApp = app.NewWithID("com.lostdusty.gualto")
GualtoWin = GualtoApp.NewWindow("Gualto")
GualtoWin.CenterOnScreen()
GualtoWin.Resize(fyne.Size{Width: 600, Height: 400})
/* APP SETTINGS GETTERS
*/
storedInstance := GualtoApp.Preferences().StringWithFallback("instance", gobalt.CobaltApi)
checkClipboard := GualtoApp.Preferences().BoolWithFallback("clipboard", true)
shouldRememberPath := GualtoApp.Preferences().BoolWithFallback("remember-path", true)
GualtoApp.Preferences().StringWithFallback("path", "")
newTheme := GualtoApp.Preferences().BoolWithFallback("theme", false)
if newTheme {
GualtoApp.Settings().SetTheme(cobaltTheme{})
}
/* END OF THE APP SETTINGS SECTION
*/
//Async fetches cobalt instances. If it fails, add only the main instance to the list
go discoverCobaltInstances()
labelMain := widget.NewRichTextFromMarkdown("# Gualto\n\nSave what you love, no extra bullshit. Paste your url below to begin the download.")
labelMain.Wrapping = fyne.TextWrapWord
pasteURL := widget.NewEntry()
pasteURL.PlaceHolder = "Paste your url here..."
pasteURL.Validator = validation.NewRegexp(`[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?`, "Must be a valid link")
submitURL := widget.NewButtonWithIcon("", theme.MediaFastForwardIcon(), nil)
submitURL.Importance = widget.SuccessImportance
pasteURL.SetOnValidationChanged(func(err error) {
if err != nil {
submitURL.Disable()
} else {
submitURL.Enable()
}
})
/* Author: LD
PART I: Ui components for the download settings related children.
SECTION I: GENERAL OPTIONS
MODIFIED: 03/07/2024
*/
downloadSettingLabelGeneral := widget.NewRichTextFromMarkdown("### General")
// "General"
downloadSettingDisableMetadata := widget.NewCheck("Disable metadata?", func(b bool) {
newDownload.DisableVideoMetadata = b
})
//[] Disable metadata?
downloadSettingLabelFilenamePattern := widget.NewLabel("Name file as:")
downloadSettingSelectFilenamePattern := widget.NewSelect([]string{"classic", "basic", "pretty", "nerdy"}, func(s string) {
switch s {
case "basic":
newDownload.FilenamePattern = gobalt.Basic
case "classic":
newDownload.FilenamePattern = gobalt.Classic
case "nerdy":
newDownload.FilenamePattern = gobalt.Nerdy
case "pretty":
newDownload.FilenamePattern = gobalt.Pretty
}
})
downloadSettingSelectFilenamePattern.SetSelectedIndex(0)
//Name file as: [Basic]
boxFilenameSettings := container.NewHBox(downloadSettingLabelFilenamePattern, downloadSettingSelectFilenamePattern)
// Container to group checkbox & text to make: [] Disable metadata?
groupGeneralDownload := container.NewVBox(downloadSettingLabelGeneral, downloadSettingDisableMetadata, boxFilenameSettings)
//Merge them all like:
// ## General
// [] Disable metadata?
// Name file as: [Basic]
/* SECTION II: VIDEO SETTINGS */
downloadSettingLabelVideo := widget.NewRichTextFromMarkdown("### Video")
// ## Video
downloadSettingLabelQuality := widget.NewLabel("Video Quality:")
downloadSettingSelectQuality := widget.NewSelect([]string{"144", "240", "360", "480", "720", "1080", "1440", "2160"}, func(s string) {
newDownload.VideoQuality, _ = strconv.Atoi(s)
})
downloadSettingSelectQuality.Selected = "1080"
boxQualitySettings := container.NewHBox(downloadSettingLabelQuality, downloadSettingSelectQuality)
// Video Quality: [1080]
downloadSettingLabelVideoCodec := widget.NewLabel("Youtube Video Codec:")
downloadSettingSelectVideoCodec := widget.NewSelect([]string{"h264", "av1", "vp9"}, func(s string) {
//TODO: Move this to gobalt
switch s {
case "h264":
newDownload.VideoCodec = gobalt.H264
case "av1":
newDownload.VideoCodec = gobalt.AV1
case "vp9":
newDownload.VideoCodec = gobalt.VP9
}
})
downloadSettingSelectVideoCodec.Selected = "h264"
boxCodecSettings := container.NewHBox(downloadSettingLabelVideoCodec, downloadSettingSelectVideoCodec)
// Youtube Video Codec: [h264]
downloadSettingRemoveVideo := widget.NewCheck("Remove video?", func(b bool) {
newDownload.AudioOnly = b
})
// [] Remove video?
groupVideoDownload := container.NewVBox(downloadSettingLabelVideo, boxQualitySettings, boxCodecSettings, downloadSettingRemoveVideo)
//Merge them like:
// ## Video
// Video Quality: [1080]
// Youtube Video Codec: [h264]
// [] Remove video?
/* SECTION III: AUDIO */
downloadSettingLabelAudio := widget.NewRichTextFromMarkdown("### Audio")
// ## Audio
downloadSettingLabelAudioCodec := widget.NewLabel("Audio format:")
downloadSettingSelectAudioCodec := widget.NewSelect([]string{"best", "mp3", "ogg", "wav", "opus"}, func(s string) {
switch s {
case "best":
newDownload.AudioCodec = gobalt.Best
case "mp3":
newDownload.AudioCodec = gobalt.MP3
case "ogg":
newDownload.AudioCodec = gobalt.Ogg
case "wav":
newDownload.AudioCodec = gobalt.Wav
case "opus":
newDownload.AudioCodec = gobalt.Opus
}
})
downloadSettingSelectAudioCodec.SetSelectedIndex(0)
boxAudioSettings := container.NewHBox(downloadSettingLabelAudioCodec, downloadSettingSelectAudioCodec)
// Audio format: [best]
downloadSettingRemoveAudio := widget.NewCheck("Remove audio?", func(b bool) {
newDownload.VideoOnly = b
})
// [] Remove audio?
groupAudioDownload := container.NewVBox(downloadSettingLabelAudio, boxAudioSettings, downloadSettingRemoveAudio)
//Merge them like:
// ## Audio
// Audio format: [best]
// [] Remove audio?
/* SECTION IV: PLATFORM SPECIFIC SETTINGS */
downloadSettingLabelPlatform := widget.NewRichTextFromMarkdown("### Platform specific")
// ## Platform specific
downloadSettingTwitter := widget.NewCheck("Convert Tweets to gifs?", func(b bool) {
newDownload.ConvertTwitterGifs = b
})
downloadSettingTwitter.Checked = true
// [X] Convert Tweets to gifs?
downloadSettingTiktok := widget.NewCheck("Full tiktok audio?", func(b bool) {
newDownload.FullTikTokAudio = b
})
// [] Full tiktok audio?
groupPlatformSpecific := container.NewVBox(downloadSettingLabelPlatform, downloadSettingTwitter, downloadSettingTiktok)
//Merge them like:
// ## Platform specific
// [X] Convert Tweets to gifs?
// [] Full tiktok audio?
/* SECTION V: MERGE INTO A SINGLE CONTAINER */
leftOptions := container.NewVBox(groupGeneralDownload, groupVideoDownload)
rightOptions := container.NewVBox(groupAudioDownload, groupPlatformSpecific)
//sep := canvas.NewLine(theme.PrimaryColor())
downloadOptions := container.NewAdaptiveGrid(2, leftOptions, rightOptions)
/*
* END OF PART I.
*/
/* Author: LD
PART II: Layout for the tab "about".
SECTION I: Text Definition
MODIFIED: 04/07/2024
*/
aboutTabMainText := widget.NewRichTextFromMarkdown("# About\n\nSave what you love, no extra bullshit.\n\nUses [cobalt.tools](cobalt.tools) under the hood.\n\n### Thanks to..\nYou, Wukko, JJ & contributors")
aboutTab := container.NewTabItemWithIcon("", theme.InfoIcon(), aboutTabMainText)
/*
* END OF PART II.
*/
/* Author: LD
PART III: Layout for the tab "settings".
SECTION I: GET SETTINGS
MODIFIED: 04/07/2024
*/
tabSettingsSelectInstance := &widget.Select{
Selected: storedInstance,
Options: cobaltInstances,
}
/* SECTION II: CREATE LAYOUT TEXT + INSTANCE CHANGER */
tabSettingsLabelInstance := widget.NewRichTextFromMarkdown("# Settings\n\nThis settings allows you to use a custom instance.\n\nYou might want to change this if you're getting any issues with the selected instance.")
tabSettingsSelectInstance.OnChanged = func(s string) {
GualtoApp.Preferences().SetString("instance", s)
}
/* SECTION III: OPTION TO SCAN CLIPBOARD FOR DOWNLOADABLE LINKS */
tabSettingsCheckClip := widget.NewCheck("Check clipboard for links to download?", func(b bool) {
GualtoApp.Preferences().SetBool("clipboard", b)
})
tabSettingsCheckClip.Checked = checkClipboard
/* SECTION IV: CUSTOM COBALT THEME? */
tabSettingsCustomTheme := widget.NewCheck("Use new app theme?", func(b bool) {
GualtoApp.Preferences().SetBool("theme", b)
if b {
GualtoApp.Settings().SetTheme(cobaltTheme{})
} else {
GualtoApp.Settings().SetTheme(theme.DefaultTheme())
}
})
tabSettingsCustomTheme.Checked = newTheme
/* SECTION V: REMEMBER LAST PATH WHERE IT WAS DOWNLOADED? */
/* DESKTOP ONLY: ANDROID & iOS PICKER ALREADY DOES THAT */
tabSettingsLastPath := widget.NewCheck("Remember last folder saved?", func(b bool) {
GualtoApp.Preferences().SetBool("remember-path", b)
})
tabSettingsLastPath.Checked = shouldRememberPath
if fyne.CurrentApp().Driver().Device().IsMobile() {
tabSettingsLastPath.Hide()
}
tabSettingsContent := container.NewVBox(tabSettingsLabelInstance, tabSettingsSelectInstance, widget.NewSeparator(), tabSettingsCheckClip, widget.NewSeparator(), tabSettingsCustomTheme, widget.NewSeparator(), tabSettingsLastPath)
settingsTab := container.NewTabItemWithIcon("", theme.SettingsIcon(), tabSettingsContent)
submitURL.OnTapped = func() {
blockClip = true
submitURL.Disable()
statusProgressBar := dialog.NewCustomWithoutButtons("Downloading....", widget.NewProgressBarInfinite(), GualtoWin)
statusProgressBar.Show()
newDownload.Url = pasteURL.Text
err := downloadMedia(newDownload)
if err != nil {
errLab := widget.NewLabel(err.Error())
errLab.Wrapping = fyne.TextWrapWord
dialog.NewCustom("Somewent went wrong while downloading!", "close", errLab, GualtoWin)
statusProgressBar.Hide()
}
statusProgressBar.Hide()
submitURL.Enable()
blockClip = false
}
/* CREATE THE FINAL LAYOUT AND DISPLAY */
submitContainer := container.NewBorder(nil, nil, nil, submitURL, pasteURL)
downloadActions := container.NewVBox(labelMain, submitContainer)
tabMainContent := container.NewBorder(downloadActions, nil, nil, nil, container.NewScroll(downloadOptions))
mainTab := container.NewTabItemWithIcon("", theme.HomeIcon(), tabMainContent)
layoutTabs := container.NewAppTabs(mainTab, settingsTab, aboutTab)
layoutTabs.OnSelected = func(ti *container.TabItem) {
if layoutTabs.SelectedIndex() == 1 && len(cobaltInstances) > 1 {
tabSettingsSelectInstance.SetOptions(cobaltInstances) //Fix to set cobalt instances, for some reason it's not being set anymore.
}
}
layoutTabs.SetTabLocation(container.TabLocationLeading)
GualtoWin.SetContent(layoutTabs)
GualtoApp.Lifecycle().SetOnEnteredForeground(func() {
go func() {
if !blockClip && GualtoApp.Preferences().Bool("clipboard") { //Show clipboard paste if all of these are true
blockClip = true
isLink := verifyLink.MatchString(GualtoWin.Clipboard().Content())
if !isLink {
return
}
downloadClipAsk := dialog.NewConfirm("We found an link!", "Paste URL from clipboard?", func(b bool) {
if b {
pasteURL.SetText(GualtoWin.Clipboard().Content())
}
}, GualtoWin)
downloadClipAsk.Show()
downloadClipAsk.SetOnClosed(func() {
blockClip = false
})
} else {
return
}
}()
})
GualtoWin.ShowAndRun()
}
func downloadMedia(options gobalt.Settings) error {
cobaltRequestDownloadFile, err := gobalt.Run(options)
if err != nil {
return err
}
if cobaltRequestDownloadFile.Status == "picker" {
return fmt.Errorf("picker is not supported yet")
}
cobaltMediaResponse, err := http.Get(cobaltRequestDownloadFile.URL)
if err != nil {
return err
}
cobaltGetFileName := cobaltMediaResponse.Header.Get("Content-Disposition")
_, parseFileName, err := mime.ParseMediaType(cobaltGetFileName)
mediaFilename := parseFileName["filename"]
if err != nil {
mediaFilename = path.Base(cobaltMediaResponse.Request.URL.Path)
}
normalizeFileName := regexp.MustCompile(`[^[:word:][:punct:]\s]`)
normalFileName := normalizeFileName.ReplaceAllString(mediaFilename, "")
saveFileDialog := dialog.NewFileSave(func(uc fyne.URIWriteCloser, err error) {
savingFile := dialog.NewProgressInfinite("Downloading your file...", "might take a while.", GualtoWin)
savingFile.Show()
blockClip = true
if err != nil || uc == nil {
return
}
fromReqToFile, err := io.Copy(uc, cobaltMediaResponse.Body)
if err != nil {
dialog.ShowCustom("error", "close", container.NewVBox(&widget.Label{Text: err.Error(), Wrapping: fyne.TextWrapWord}), GualtoWin)
return
}
cobaltMediaResponse.Body.Close()
if GualtoApp.Preferences().Bool("remember-path") {
GualtoApp.Preferences().SetString("path", uc.URI().Path())
}
savingFile.Hide()
dialog.ShowInformation(fmt.Sprintf("Media (%d.2MB) saved with success!", (fromReqToFile/1000000)), fmt.Sprintf("Saved to %v", uc.URI().Path()), GualtoWin)
blockClip = false
}, GualtoWin)
if GualtoApp.Preferences().String("path") != "" {
u, _ := storage.ParseURI(GualtoApp.Preferences().String("path"))
ul, _ := storage.ListerForURI(u)
saveFileDialog.SetLocation(ul)
}
saveFileDialog.SetFileName(normalFileName)
go saveFileDialog.Show()
return nil
}