-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
691 lines (608 loc) · 15.2 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"html"
"html/template"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/alexflint/go-arg"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/dustin/go-humanize"
)
const (
defTemplate = "{{.title}} [GOG]"
sanRegexStr = `[\/:*?"><|]`
siteUrl = "https://www.gog.com"
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/"+
"537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
)
var (
jar, _ = cookiejar.New(nil)
client = &http.Client{Transport: &Transport{}, Jar: jar}
)
var resolvePlatform = map[string]string{
"windows": "1,2,4,8,4096,16384",
"linux": "1024,2048,8192",
"mac": "16,32",
}
var languages = []string{
"en", "cz", "de", "es", "fr", "it",
"hu", "nl", "pl", "pt", "br", "sv",
"tr", "uk", "ru", "ar", "ko", "cn",
"jp", "all",
}
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Referer", siteUrl+"/")
req.Header.Add("Origin", siteUrl)
return http.DefaultTransport.RoundTrip(req)
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
var speed int64 = 0
n := len(p)
wc.Downloaded += int64(n)
percentage := float64(wc.Downloaded) / float64(wc.Total) * float64(100)
wc.Percentage = int(percentage)
toDivideBy := time.Now().UnixMilli() - wc.StartTime
if toDivideBy != 0 {
speed = int64(wc.Downloaded) / toDivideBy * 1000
}
fmt.Printf("\r%d%% @ %s/s, %s/%s ", wc.Percentage, humanize.Bytes(uint64(speed)),
humanize.Bytes(uint64(wc.Downloaded)), wc.TotalStr)
return n, nil
}
func handleErr(errText string, err error, _panic bool) {
errString := errText + "\n" + err.Error()
if _panic {
panic(errString)
}
fmt.Println(errString)
}
func wasRunFromSrc() bool {
buildPath := filepath.Join(os.TempDir(), "go-build")
return strings.HasPrefix(os.Args[0], buildPath)
}
func getScriptDir() (string, error) {
var (
ok bool
err error
fname string
)
runFromSrc := wasRunFromSrc()
if runFromSrc {
_, fname, _, ok = runtime.Caller(0)
if !ok {
return "", errors.New("Failed to get script filename.")
}
} else {
fname, err = os.Executable()
if err != nil {
return "", err
}
}
return filepath.Dir(fname), nil
}
func readConfig() (*Config, error) {
data, err := os.ReadFile("config.json")
if err != nil {
return nil, err
}
var obj Config
err = json.Unmarshal(data, &obj)
if err != nil {
return nil, err
}
return &obj, nil
}
func parseArgs() *Args {
var args Args
arg.MustParse(&args)
return &args
}
func makeDirs(path string) error {
err := os.MkdirAll(path, 0755)
return err
}
func checkLang(userLang string) bool {
for _, lang := range languages {
if lang == userLang {
return true
}
}
return false
}
func parseCfg() (*Config, error) {
cfg, err := readConfig()
if err != nil {
return nil, err
}
args := parseArgs()
query := strings.TrimSpace(args.Query)
if query != "" && len(query) < 3 {
return nil, errors.New("query must be at least two characters")
}
cfg.Query = args.Query
if args.Platform != "" {
cfg.Platform = args.Platform
}
if args.Language != "" {
cfg.Language = args.Language
}
if args.FolderTemplate != "" {
cfg.FolderTemplate = args.FolderTemplate
}
if args.Goodies {
cfg.Goodies = args.Goodies
}
if strings.TrimSpace(cfg.Platform) == "" || strings.TrimSpace(cfg.Language) == "" {
return nil, errors.New("platform and language are required")
}
lang := strings.ToLower(cfg.Language)
if !checkLang(lang) {
return nil, errors.New("invalid language: " + cfg.Language)
}
args.Language = lang
platform := strings.ToLower(cfg.Platform)
if platform == "win" {
platform = "windows"
} else if platform == "osx" {
platform = "mac"
}
platformIds, ok := resolvePlatform[platform]
if !ok {
return nil, errors.New("invalid platform: " + cfg.Platform)
}
cfg.Platform = platform
cfg.PlatformIDs = platformIds
if args.OutPath != "" {
cfg.OutPath = args.OutPath
}
if cfg.OutPath == "" {
cfg.OutPath = "GOG downloads"
}
if strings.TrimSpace(cfg.FolderTemplate) == "" {
cfg.FolderTemplate = defTemplate
}
return cfg, nil
}
func readCookies() ([]*Cookie, error) {
data, err := os.ReadFile("cookies.json")
if err != nil {
return nil, err
}
var obj []*Cookie
err = json.Unmarshal(data, &obj)
if err != nil {
return nil, err
}
return obj, nil
}
func setCookies(_cookies []*Cookie) error {
var cookies []*http.Cookie
for _, _cookie := range _cookies {
name := _cookie.Name
value := _cookie.Value
cookie := &http.Cookie{
Domain: _cookie.Domain,
Name: name,
Path: _cookie.Path,
Secure: _cookie.Secure,
Value: value,
}
cookies = append(cookies, cookie)
}
u, err := url.Parse(siteUrl)
if err != nil {
return err
}
client.Jar.SetCookies(u, cookies)
return nil
}
func checkCookies() (bool, error) {
req, err := client.Get(siteUrl+"/userData.json")
if err != nil {
return false, err
}
defer req.Body.Close()
if req.StatusCode != http.StatusOK {
return false, errors.New(req.Status)
}
var obj UserData
err = json.NewDecoder(req.Body).Decode(&obj)
if err != nil {
return false, err
}
ok := obj.IsLoggedIn
if ok {
fmt.Println("Signed in as " + obj.Username + ".\n")
}
return ok, nil
}
func search(queryStr, platformIds, lang string) ([]Product, error) {
req, err := http.NewRequest(
http.MethodGet, siteUrl+"/account/getFilteredProducts", nil)
if err != nil {
return nil, err
}
pageNum := 1
query := url.Values{}
query.Set("hiddenFlag", "0")
if lang != "all" {
query.Set("language", lang)
}
query.Set("mediaType", "1")
query.Set("sortBy", "date_purchased")
if queryStr != "" {
query.Set("search", queryStr)
}
query.Set("system", platformIds)
query.Set("totalPages", "1")
var products []Product
for {
query.Set("page", strconv.Itoa(pageNum))
req.URL.RawQuery = query.Encode()
do, err := client.Do(req)
if err != nil {
return nil, err
}
if do.StatusCode != http.StatusOK {
do.Body.Close()
return nil, errors.New(do.Status)
}
var obj Search
err = json.NewDecoder(do.Body).Decode(&obj)
do.Body.Close()
if err != nil {
return nil, err
}
if obj.TotalPages == 0 {
break
}
products = append(products, obj.Products...)
if pageNum == obj.TotalPages {
break
}
pageNum++
time.Sleep(time.Second*1)
}
return products, nil
}
func getUserGameIdx(products []Product, prodLen int) (int, error) {
var (
idx int
opts []string
)
for _, p := range products {
opts = append(opts, p.Title)
}
prompt := &survey.Select{Options: opts}
err := survey.AskOne(
prompt, &idx, survey.WithValidator(survey.Required),
survey.WithPageSize(10))
if err != nil {
return 0, err
}
return idx, nil
}
func selectGameId(products []Product, queryStr string) (int, error) {
prodLen := len(products)
if prodLen == 1 {
return products[0].ID, nil
}
if queryStr != "" {
fmt.Println("Your search yielded more than one result.")
}
idx, err := getUserGameIdx(products, prodLen)
if err != nil {
return 0, err
}
return products[idx].ID, nil
}
func getGameMeta(id int) (*GameMeta, error) {
req, err := client.Get(
siteUrl + "/account/gameDetails/" + strconv.Itoa(id) + ".json")
if err != nil {
return nil, err
}
defer req.Body.Close()
if req.StatusCode != http.StatusOK {
return nil, errors.New(req.Status)
}
var obj GameMeta
err = json.NewDecoder(req.Body).Decode(&obj)
if err != nil {
return nil, err
}
return &obj, nil
}
func parseDownloads(meta *GameMeta, platform string, goodies bool) ([]*Download, error) {
var parsedDloads []*Download
// Shambles, get structs working.
downloads := meta.Downloads[0][1].(map[string]interface{})[platform].([]interface{})
for _, _d := range downloads {
d := _d.(map[string]interface{})
ver := "<no ver>"
if d["version"] != nil {
ver = d["version"].(string)
}
parsedDload := &Download{
ManualURL: siteUrl + d["manualUrl"].(string),
Name: d["name"].(string),
Version: ver,
Date: d["date"].(string),
Size: d["size"].(string),
}
parsedDloads = append(parsedDloads, parsedDload)
}
if goodies {
for _, e := range meta.Extras {
e.ManualURL = siteUrl + e.ManualURL
parsedDloads = append(parsedDloads, e)
}
}
return parsedDloads, nil
}
func getLongestNameLen(downloads []*Download) int {
var longest int
for _, d := range downloads {
curLen := len(d.Name)
if curLen > longest {
longest = curLen
}
}
return longest
}
func getUserDloadIndexes(downloads []*Download) ([]int, error) {
var (
indexes []int
opts []string
)
longestNameLen := getLongestNameLen(downloads)
for _, d := range downloads {
ver := d.Version
if ver == "" {
ver = "<no ver>"
}
spaces := strings.Repeat(" ", longestNameLen-len(d.Name))
opts = append(opts, d.Name + spaces + " - " + ver + ", " + d.Size)
}
prompt := &survey.MultiSelect{Options: opts}
err := survey.AskOne(
prompt, &indexes, survey.WithValidator(survey.Required),
survey.WithPageSize(10))
if err != nil {
return nil, err
}
return indexes, nil
}
func selectDownloads(downloads []*Download) ([]*Download, error) {
// prodLen := len(products)
// if prodLen == 1 {
// return products[0].ID, nil
// }
var selectedDloads []*Download
indexes, err := getUserDloadIndexes(downloads)
if err != nil {
return nil, err
}
for _, idx := range indexes {
selectedDloads = append(selectedDloads, downloads[idx])
}
return selectedDloads, nil
}
func fileExists(path string) (bool, int64, error) {
f, err := os.Stat(path)
if err == nil {
return !f.IsDir(), f.Size(), nil
} else if os.IsNotExist(err) {
return false, 0, nil
}
return false, 0, err
}
func sanitise(fname string) string {
regex := regexp.MustCompile(sanRegexStr)
fname = regex.ReplaceAllString(fname, "_")
return fname
}
func getFname(itemUrl string) (string, error) {
req, err := client.Head(itemUrl)
if err != nil {
return "", err
}
if req.StatusCode != http.StatusOK {
return "", errors.New(req.Status)
}
fname := path.Base(req.Request.URL.String())
fname, err = url.PathUnescape(fname)
if err != nil {
return "", err
}
return fname, nil
}
func getBase(fname string) string {
ext := filepath.Ext(fname)
if ext == ".gz" {
ext = ".tar.gz"
}
base := fname[:len(fname)-len(ext)]
return base
}
func parseTempMeta(title string) map[string]string {
parsed := map[string]string{
"title": title,
"titlePeriods": strings.ReplaceAll(title, " ", "."),
}
return parsed
}
func parseTemplate(text string, meta map[string]string) string {
var buffer bytes.Buffer
for {
err := template.Must(template.New("").Parse(text)).Execute(&buffer, meta)
if err == nil {
break
}
fmt.Println("Failed to parse template. Default will be used instead.")
text = defTemplate
buffer.Reset()
}
return html.UnescapeString(buffer.String())
}
func downloadItem(download *Download, outPath string) error {
var startByte int64
itemUrl := download.ManualURL
fname, err := getFname(itemUrl)
if err != nil {
return err
}
outPath = filepath.Join(outPath, fname)
exists, _, err := fileExists(outPath)
if err != nil {
return err
}
if exists {
fmt.Println("Item already exists locally.")
return nil
}
base := getBase(outPath)
incompPath := filepath.Join(base + ".incomplete")
exists, size, err := fileExists(incompPath)
if err != nil {
return err
}
if exists {
startByte = size
fmt.Println("Incomplete item exists locally. Resuming...")
}
req, err := http.NewRequest(http.MethodGet, download.ManualURL, nil)
if err != nil {
return err
}
req.Header.Add(
"Range", "bytes=" + strconv.FormatInt(startByte, 10) + "-")
do, err := client.Do(req)
if err != nil {
return err
}
defer do.Body.Close()
if do.StatusCode != http.StatusOK && do.StatusCode != http.StatusPartialContent {
return errors.New(do.Status)
}
f, err := os.OpenFile(incompPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
return err
}
totalBytes := do.ContentLength + startByte
counter := &WriteCounter{
Total: totalBytes,
TotalStr: humanize.Bytes(uint64(totalBytes)),
StartTime: time.Now().UnixMilli(),
Downloaded: startByte,
}
_, err = io.Copy(f, io.TeeReader(do.Body, counter))
f.Close()
err = os.Rename(incompPath, outPath)
fmt.Println("")
return err
}
func init() {
fmt.Println(`
_____ _____ _____ ____ _ _
| __| | __| | \ ___ _ _ _ ___| |___ ___ _| |___ ___
| | | | | | | | | | . | | | | | | . | .'| . | -_| _|
|_____|_____|_____| |____/|___|_____|_|_|_|___|__,|___|___|_|
`)
}
func main() {
scriptDir, err := getScriptDir()
if err != nil {
panic(err)
}
err = os.Chdir(scriptDir)
if err != nil {
panic(err)
}
cfg, err := parseCfg()
if err != nil {
handleErr("failed to parse config/args", err, true)
}
err = makeDirs(cfg.OutPath)
if err != nil {
handleErr("failed to make output folder(s)", err, true)
}
cookies, err := readCookies()
if err != nil {
handleErr("failed to read cookies", err, true)
}
err = setCookies(cookies)
if err != nil {
handleErr("failed to set cookies", err, true)
}
ok, err := checkCookies()
if err != nil {
handleErr("failed to check cookies", err, true)
}
if !ok {
panic("bad cookies")
}
products, err := search(cfg.Query, cfg.PlatformIDs, cfg.Language)
if err != nil {
panic(err)
}
if len(products) == 0 {
fmt.Println("No search results.")
os.Exit(1)
}
id, err := selectGameId(products, cfg.Query)
if err != nil {
if err == terminal.InterruptErr {
os.Exit(0)
}
handleErr("failed to select game id", err, true)
}
gameMeta, err := getGameMeta(id)
if err != nil {
handleErr("failed to get game meta", err, true)
}
fmt.Println("--" + gameMeta.Title + "--")
downloads, err := parseDownloads(gameMeta, cfg.Platform, cfg.Goodies)
if err != nil {
handleErr("failed to parse items", err, true)
}
downloads, err = selectDownloads(downloads)
if err != nil {
if err == terminal.InterruptErr {
os.Exit(0)
}
handleErr("failed to select items", err, true)
}
itemTotal := len(downloads)
templateMeta := parseTempMeta(gameMeta.Title)
template := parseTemplate(cfg.FolderTemplate, templateMeta)
outPath := filepath.Join(cfg.OutPath, sanitise(template))
err = makeDirs(outPath)
if err != nil {
handleErr("failed to make game folder", err, true)
}
for i, item := range downloads {
fmt.Printf("Item %d of %d:\n", i+1, itemTotal)
fmt.Println(item.Name)
err = downloadItem(item, outPath)
if err != nil {
handleErr("failed to download item", err, false)
}
}
}