Can pull products, all installer links and the download + checksum

links.
This commit is contained in:
Kevin Ruffin 2022-02-17 22:33:45 -05:00
parent 6b1ec66ef3
commit 4b22e35c3b

81
main.go
View File

@ -15,6 +15,8 @@ import (
"github.com/webview/webview" "github.com/webview/webview"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"io/ioutil" "io/ioutil"
"strconv"
"fmt"
) )
var ( var (
@ -125,6 +127,81 @@ func getGameList() *GameList {
return gl return gl
} }
type Product struct {
Id int `json:"id"`
Title string `json:"title"`
Downloads struct {
Installers [] struct {
Name string `json:"name"`
Os string `json:"os"`
Files [] struct {
Id string `json:"id"`
Size int64 `json:"size"`
Downlink string `json:"downlink"`
} `json:"files"`
} `json:"installers"`
} `json:"downloads`
}
func getProduct(id int) *Product {
url := "https://api.gog.com/products/" + strconv.Itoa(id) + "?expand=downloads"
res, err := client.Get(url)
if err != nil {
log.Fatalf("Failed to get product info: %v", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("Failed to parse product body: %v", err)
}
//log.Println(string(body))
var p = new(Product)
err = json.Unmarshal(body, p)
if err != nil {
log.Fatalf("Failed to parse product response: %v", err)
}
return p
}
type DownloadLinks struct {
Downlink string `json:"downlink"`
Checksum string `json:"checksum"`
}
func (dl *DownloadLinks) String() string {
return fmt.Sprintf("{downlink: %s, checksum: %s}", dl.Downlink, dl.Checksum)
}
func getDownloadLinks(p *Product) []*DownloadLinks {
var links []*DownloadLinks
for _, i := range p.Downloads.Installers {
for _, f := range i.Files {
res, err := client.Get(f.Downlink)
if err != nil {
log.Fatalf("Failed to get file download info: %v", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("Failed to parse file download body: %v", err)
}
//log.Println(string(body))
var dl = new(DownloadLinks)
err = json.Unmarshal(body, dl)
if err != nil {
log.Fatalf("Failed to parse file download response: %v", err)
}
links = append(links, dl)
}
}
return links
}
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)
@ -173,5 +250,9 @@ func main() {
gl := getGameList() gl := getGameList()
log.Println(gl.Owned) log.Println(gl.Owned)
p := getProduct(gl.Owned[0])
log.Println(p)
dl := getDownloadLinks(p)
log.Printf("%+v", dl)
} }