-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
314 lines (271 loc) · 8.42 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
package main
import (
"archive/zip"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/parnurzeal/gorequest"
"github.com/tidwall/gjson"
)
const (
ATTEMPTS_TO_RETRIEVE_DOWNLOAD = 10 // It will attempt 10 times before saying "Server is Busy"
TIME_BEFORE_CHECKING_IF_RETRIEVED = time.Second * 3 // waits 3 seconds each attempt
)
type WorkshopDownloader struct {
folder string
updateLabel *widget.Label
pathLabel *widget.Label
}
func (downloader *WorkshopDownloader) SetFolder(arg string) {
if arg[0:7] == "file://" {
arg = arg[7:]
}
downloader.folder = arg
downloader.pathLabel.SetText(filepath.Base(arg))
}
func (downloader *WorkshopDownloader) FolderSet() bool {
return downloader.folder != ""
}
func (downloader *WorkshopDownloader) SetUpdateLabel(arg *widget.Label) {
downloader.updateLabel = arg
}
func (downloader *WorkshopDownloader) SetPathLabel(arg *widget.Label) {
downloader.pathLabel = arg
}
func (downloader *WorkshopDownloader) DownloadFile(idURL, url string) error {
file, err := os.Create(idURL + ".zip")
if err != nil {
return err
}
defer file.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return nil
}
func (downloader *WorkshopDownloader) Unzip(id string) ([]string, error) {
defaultWorkingDIrectory, err := os.Getwd()
if err != nil {
return nil, err
}
defer os.Chdir(defaultWorkingDIrectory)
var src string = id + ".zip"
var dest string = path.Join(downloader.folder + "\\" + id)
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
os.Chdir(downloader.folder)
fpath := filepath.Join(dest, f.Name)
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
continue
}
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
rc, err := f.Open()
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return filenames, err
}
}
return filenames, nil
}
func (downloader *WorkshopDownloader) UpdateStatus(status string) {
downloader.updateLabel.SetText(status)
}
func (downloader *WorkshopDownloader) HandleDownload(url *url.URL) error {
idUrl := url.Query().Get("id")
if idUrl == "" {
return fmt.Errorf("e: Invalid URL")
}
downloader.UpdateStatus("Checking if Avaliable")
request := gorequest.New()
resp, body, _ := request.Post("https://backend-02-prd.steamworkshopdownloader.io/api/download/request").
Set("Content-Type", "application/json").
Send(`{"publishedFileId":` + idUrl + `, "collectionId":0, "extract":true, "hidden":false, "direct":false, "autodownload":true}`).
End()
if resp.StatusCode != 200 {
return fmt.Errorf("e: Unavaliable or server is Down")
} else {
downloader.UpdateStatus("Item Avaliable")
}
// Download request //
uid := gjson.Get(body, "uuid").String()
var readyFile = false
for i := 0; i < ATTEMPTS_TO_RETRIEVE_DOWNLOAD; i++ {
_, body, _ := request.Post("https://backend-02-prd.steamworkshopdownloader.io/api/download/status").
Set("Content-Type", "application/json").
Send(`{"uuids": ["` + uid + `"]}`).
End()
downloader.UpdateStatus(strings.Title(gjson.Get(body, uid+".status").String()))
if strings.Contains(body, "prepared") {
readyFile = true
downloader.UpdateStatus("Initializing Download...")
break
}
time.Sleep(TIME_BEFORE_CHECKING_IF_RETRIEVED)
}
if readyFile {
err := downloader.DownloadFile(idUrl, "https://backend-02-prd.steamworkshopdownloader.io/api/download/transmit?uuid="+uid)
if err != nil {
return err
} else {
downloader.UpdateStatus("Trying to Decompress")
_, err := downloader.Unzip(idUrl)
if err != nil {
return err
}
downloader.UpdateStatus("Successfuly Decompressed")
err = os.Remove(idUrl + ".zip")
if err != nil {
return err
}
downloader.UpdateStatus("Download Finished")
return nil
}
} else {
return fmt.Errorf("e: Server is Busy")
}
}
func FolderOpenHandler(win fyne.Window, downloader *WorkshopDownloader) func(fyne.ListableURI, error) {
return func(list fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, win)
return
}
if list == nil {
return
}
downloader.SetFolder(list.String())
}
}
func MakeDivisor(icon fyne.Resource, placeholder string, Lab *widget.Label) *fyne.Container {
return container.NewAdaptiveGrid(1, container.NewHBox(container.NewHBox(widget.NewIcon(icon), (widget.NewLabel(placeholder+": ")), Lab)))
// return container.NewAdaptiveGrid(1, container.NewHBox(container.NewHBox(widget.NewIcon(icon), container.NewCenter(widget.NewLabel(placeholder+": ")), Lab)))
}
func ValidURL(URL string) (bool, *url.URL) {
if URL != "" {
if _, err := strconv.Atoi(URL); err == nil {
URL = "https://steamcommunity.com/sharedfiles/filedetails/?id=" + URL
}
ParsedURL, err := url.ParseRequestURI(URL)
if err != nil {
return false, nil
}
if strings.ToLower(ParsedURL.Host) == "steamcommunity.com" {
return true, ParsedURL
} else {
return false, nil
}
} else {
return false, nil
}
}
func main() {
var Downloader WorkshopDownloader
//var ClipboardContents *string
Application := app.New()
Application.Settings().SetTheme(theme.DarkTheme())
win := Application.NewWindow("Workshop Downloader (v0.2.3)")
win.Resize(fyne.NewSize(843, 421))
win.CenterOnScreen()
UpdatesTextStyle := fyne.TextStyle{Bold: true, Italic: false, Monospace: false}
// Update Labels
updateLabel := widget.NewLabel("Not Started")
pathLabel := widget.NewLabel("Not Specified")
timeLabel := widget.NewLabel("---")
ItemID := widget.NewLabel("Not Specified")
// Style
updateLabel.Alignment = fyne.TextAlignTrailing
updateLabel.TextStyle = UpdatesTextStyle
pathLabel.Alignment = fyne.TextAlignTrailing
pathLabel.TextStyle = UpdatesTextStyle
timeLabel.Alignment = fyne.TextAlignTrailing
timeLabel.TextStyle = UpdatesTextStyle
ItemID.Alignment = fyne.TextAlignTrailing
ItemID.TextStyle = UpdatesTextStyle
URLInput := widget.NewEntry()
URLInput.PlaceHolder = "URL or Item ID"
form := &widget.Form{
Items: []*widget.FormItem{
{Text: "Download Path", Widget: widget.NewButton("Choose", func() {
dialog.ShowFolderOpen(FolderOpenHandler(win, &Downloader), win)
})}},
OnSubmit: func() {
if Downloader.FolderSet() {
IsValid, ParsedURL := ValidURL(URLInput.Text)
if IsValid {
URLInput.Disable()
timeLabel.SetText("...")
start := time.Now()
ItemID.SetText(ParsedURL.Query().Get("id"))
err := Downloader.HandleDownload(ParsedURL)
if err != nil {
dialog.ShowError(err, win)
Downloader.UpdateStatus(err.Error())
timeLabel.SetText((time.Since(start)).String())
URLInput.Text = ""
URLInput.Enable()
return
}
timeLabel.SetText((time.Since(start)).String())
URLInput.Text = ""
URLInput.Enable()
timeLabel.SetText((time.Since(start)).String())
} else {
dialog.ShowError(fmt.Errorf("e: Invalid URL"), win)
}
} else {
dialog.ShowError(fmt.Errorf("e: Download Folder not Specified"), win)
}
},
}
form.Append("URL", URLInput)
Downloader.SetPathLabel(pathLabel)
Downloader.SetUpdateLabel(updateLabel)
MainContainer := container.NewVSplit(form,
container.NewCenter(container.NewAdaptiveGrid(1,
MakeDivisor(theme.InfoIcon(), "Status", updateLabel),
MakeDivisor(theme.FolderIcon(), "Folder", pathLabel),
MakeDivisor(theme.DownloadIcon(), "Time Taken", timeLabel),
MakeDivisor(theme.DocumentIcon(), "Item ID", ItemID))))
win.SetContent(MainContainer)
win.ShowAndRun()
}