Starts to build the bash script now. Needs to be more resiliant tho.

This commit is contained in:
Kevin Ruffin 2022-02-18 00:58:26 -05:00
parent 4b22e35c3b
commit 855e7ff186

100
main.go
View File

@ -17,6 +17,9 @@ import (
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"fmt" "fmt"
"regexp"
"strings"
"os"
) )
var ( var (
@ -29,10 +32,13 @@ var (
func saveToken(tok *oauth2.Token, filename string) { func saveToken(tok *oauth2.Token, filename string) {
j, err := json.MarshalIndent(tok, "", "\t") j, err := json.MarshalIndent(tok, "", "\t")
if err != nil {
log.Fatalf("Failed to marshal token: %v", err)
}
err = ioutil.WriteFile(filename, j, 0664)
if err != nil { if err != nil {
log.Fatalf("Failed to save token: %v", err) log.Fatalf("Failed to save token: %v", err)
} }
ioutil.WriteFile(filename, j, 0777)
} }
func loadToken(filename string) *oauth2.Token { func loadToken(filename string) *oauth2.Token {
@ -134,6 +140,7 @@ type Product struct {
Installers [] struct { Installers [] struct {
Name string `json:"name"` Name string `json:"name"`
Os string `json:"os"` Os string `json:"os"`
Language string `json:"language"`
Files [] struct { Files [] struct {
Id string `json:"id"` Id string `json:"id"`
Size int64 `json:"size"` Size int64 `json:"size"`
@ -168,6 +175,9 @@ func getProduct(id int) *Product {
type DownloadLinks struct { type DownloadLinks struct {
Downlink string `json:"downlink"` Downlink string `json:"downlink"`
Checksum string `json:"checksum"` Checksum string `json:"checksum"`
InstallerName string
Filename string
Md5 string
} }
func (dl *DownloadLinks) String() string { func (dl *DownloadLinks) String() string {
@ -195,6 +205,8 @@ func getDownloadLinks(p *Product) []*DownloadLinks {
if err != nil { if err != nil {
log.Fatalf("Failed to parse file download response: %v", err) log.Fatalf("Failed to parse file download response: %v", err)
} }
dl.InstallerName = sanitizeName(i.Name)
links = append(links, dl) links = append(links, dl)
} }
} }
@ -202,6 +214,69 @@ func getDownloadLinks(p *Product) []*DownloadLinks {
return links return links
} }
func sanitizeName(name string) string {
s := strings.Replace(name, " ", "_", -1)
s = strings.Replace(s, "'", "", -1)
s = strings.Replace(s, "™", "", -1)
return s
}
func writeChecksumIfNeeded(dl *DownloadLinks, folder string) {
// TODO: reuse an existing checksum file somehow
res, err := client.Get(dl.Checksum)
if err != nil {
log.Fatalf("Failed to get file checksum info: %v", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("Failed to parse file checksum body: %v", err)
}
re := regexp.MustCompile(`<file name="([^"]+)".*md5="([^"]+)`)
m := re.FindSubmatch(body)
if m != nil {
dl.Filename = sanitizeName(string(m[1]))
dl.Md5 = string(m[2])
// err = ioutil.WriteFile(folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + ".md5", m[2], 0664)
// if err != nil {
// log.Fatalf("Failed to write the checksum file: %v", err)
// }
}
}
func updateBashScriptProductFolder(p *Product, folder string, scriptName string) {
f, err := os.OpenFile("./" + scriptName, os.O_APPEND|os.O_WRONLY, 0664)
if err != nil {
log.Fatalf("Failed to open the script file: %v", err)
}
defer f.Close()
_, err = f.WriteString("mkdir -p " + folder + "/" + sanitizeName(p.Title) + "\n")
if err != nil {
log.Fatalf("Failed to update the script: %v", err)
}
}
func writeBashScript(dl *DownloadLinks, folder string, scriptName string) {
line := "wget --no-clobber --continue --quiet --show-progress -O \"" + folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + "\" \"" + dl.Downlink + "\"\n"
f, err := os.OpenFile("./" + scriptName, os.O_APPEND|os.O_WRONLY, 0664)
if err != nil {
log.Fatalf("Failed to open the script file: %v", err)
}
defer f.Close()
_, err = f.WriteString(line)
if err != nil {
log.Fatalf("Failed to update the script: %v", err)
}
}
func login(url string) { func login(url string) {
log.Println(color.CyanString("You will now be taken to your browser for authentication")) log.Println(color.CyanString("You will now be taken to your browser for authentication"))
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
@ -209,6 +284,8 @@ func login(url string) {
} }
func main() { func main() {
productsFolder := "./products"
scriptName := "grab_stuff.sh"
ctx = context.Background() ctx = context.Background()
conf = &oauth2.Config{ conf = &oauth2.Config{
ClientID: "46899977096215655", ClientID: "46899977096215655",
@ -220,12 +297,11 @@ func main() {
}, },
// my own callback URL // my own callback URL
RedirectURL: "https://embed.gog.com/on_login_success?origin=client", RedirectURL: "https://embed.gog.com/on_login_success?origin=client",
//"http://127.0.0.1:9999/oauth/callback",
} }
// add transport for self-signed certificate to context // add transport
tr := &http.Transport{ tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{},
} }
sslcli := &http.Client{Transport: tr} sslcli := &http.Client{Transport: tr}
ctx = context.WithValue(ctx, oauth2.HTTPClient, sslcli) ctx = context.WithValue(ctx, oauth2.HTTPClient, sslcli)
@ -250,9 +326,19 @@ func main() {
gl := getGameList() gl := getGameList()
log.Println(gl.Owned) log.Println(gl.Owned)
p := getProduct(gl.Owned[0]) ioutil.WriteFile("./" + scriptName, []byte("#!/bin/bash\n"), 0775)
log.Println(p)
for index, id := range gl.Owned {
log.Printf("Inspecting product: %d of %d - %d", index, len(gl.Owned), id)
p := getProduct(id)
dl := getDownloadLinks(p) dl := getDownloadLinks(p)
log.Printf("%+v", dl) updateBashScriptProductFolder(p, productsFolder, scriptName)
for _, d := range dl {
writeChecksumIfNeeded(d, productsFolder + "/" + sanitizeName(p.Title))
writeBashScript(d, productsFolder, scriptName)
}
}
} }