mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-19 17:55:22 -04:00
Add download retries
This commit is contained in:
parent
730cd80c46
commit
e57b9a17cf
2 changed files with 50 additions and 36 deletions
|
@ -32,7 +32,7 @@ func getDefaultCert(progressWindow *ProgressWindow, client *grab.Client) ([]byte
|
||||||
return cetkData[0x350 : 0x350+0x300], nil
|
return cetkData[0x350 : 0x350+0x300], nil
|
||||||
}
|
}
|
||||||
cetkDir := path.Join(os.TempDir(), "cetk")
|
cetkDir := path.Join(os.TempDir(), "cetk")
|
||||||
if err := downloadFile(progressWindow, client, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/000500101000400a/cetk", cetkDir); err != nil {
|
if err := downloadFile(progressWindow, client, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/000500101000400a/cetk", cetkDir, true); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cetkData, err := os.ReadFile(cetkDir)
|
cetkData, err := os.ReadFile(cetkDir)
|
||||||
|
|
|
@ -18,6 +18,11 @@ import (
|
||||||
"github.com/gotk3/gotk3/gtk"
|
"github.com/gotk3/gotk3/gtk"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxRetries = 5
|
||||||
|
retryDelay = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
type ProgressWindow struct {
|
type ProgressWindow struct {
|
||||||
Window *gtk.Window
|
Window *gtk.Window
|
||||||
box *gtk.Box
|
box *gtk.Box
|
||||||
|
@ -81,20 +86,22 @@ func CreateProgressWindow(parent *gtk.Window) (ProgressWindow, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadURL string, dstPath string) error {
|
func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadURL string, dstPath string, doRetries bool) error {
|
||||||
|
filePath := filepath.Base(dstPath)
|
||||||
|
|
||||||
|
for attempt := 1; attempt <= maxRetries; attempt++ {
|
||||||
req, err := grab.NewRequest(dstPath, downloadURL)
|
req, err := grab.NewRequest(dstPath, downloadURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := filepath.Base(dstPath)
|
|
||||||
|
|
||||||
resp := client.Do(req)
|
resp := client.Do(req)
|
||||||
progressWindow.bar.SetFraction(resp.Progress())
|
progressWindow.bar.SetFraction(resp.Progress())
|
||||||
progressWindow.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(resp.BytesComplete())), humanize.Bytes(uint64(resp.Size())), humanize.Bytes(uint64(resp.BytesPerSecond()))))
|
progressWindow.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(resp.BytesComplete())), humanize.Bytes(uint64(resp.Size())), humanize.Bytes(uint64(resp.BytesPerSecond()))))
|
||||||
for gtk.EventsPending() {
|
for gtk.EventsPending() {
|
||||||
gtk.MainIteration()
|
gtk.MainIteration()
|
||||||
}
|
}
|
||||||
|
|
||||||
t := time.NewTicker(500 * time.Millisecond)
|
t := time.NewTicker(500 * time.Millisecond)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
|
|
||||||
|
@ -115,11 +122,18 @@ Loop:
|
||||||
}
|
}
|
||||||
case <-resp.Done:
|
case <-resp.Done:
|
||||||
if err := resp.Err(); err != nil {
|
if err := resp.Err(); err != nil {
|
||||||
|
if doRetries && attempt < maxRetries {
|
||||||
|
fmt.Printf("[Error] Download attempt %d failed: %+v\n", attempt, err)
|
||||||
|
time.Sleep(retryDelay)
|
||||||
|
break Loop
|
||||||
|
}
|
||||||
return fmt.Errorf("download error: %+v", err)
|
return fmt.Errorf("download error: %+v", err)
|
||||||
}
|
}
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +157,7 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
|
||||||
client := grab.NewClient()
|
client := grab.NewClient()
|
||||||
downloadURL := fmt.Sprintf("%s/%s", baseURL, "tmd")
|
downloadURL := fmt.Sprintf("%s/%s", baseURL, "tmd")
|
||||||
tmdPath := filepath.Join(outputDir, "title.tmd")
|
tmdPath := filepath.Join(outputDir, "title.tmd")
|
||||||
if err := downloadFile(progressWindow, client, downloadURL, tmdPath); err != nil {
|
if err := downloadFile(progressWindow, client, downloadURL, tmdPath, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +173,7 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
|
||||||
|
|
||||||
tikPath := filepath.Join(outputDir, "title.tik")
|
tikPath := filepath.Join(outputDir, "title.tik")
|
||||||
downloadURL = fmt.Sprintf("%s/%s", baseURL, "cetk")
|
downloadURL = fmt.Sprintf("%s/%s", baseURL, "cetk")
|
||||||
if err := downloadFile(progressWindow, client, downloadURL, tikPath); err != nil {
|
if err := downloadFile(progressWindow, client, downloadURL, tikPath, false); err != nil {
|
||||||
titleKey, err := generateKey(titleID)
|
titleKey, err := generateKey(titleID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -235,14 +249,14 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
|
||||||
}
|
}
|
||||||
filePath := filepath.Join(outputDir, fmt.Sprintf("%08X.app", id))
|
filePath := filepath.Join(outputDir, fmt.Sprintf("%08X.app", id))
|
||||||
downloadURL = fmt.Sprintf("%s/%08X", baseURL, id)
|
downloadURL = fmt.Sprintf("%s/%08X", baseURL, id)
|
||||||
if err := downloadFile(progressWindow, client, downloadURL, filePath); err != nil {
|
if err := downloadFile(progressWindow, client, downloadURL, filePath, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if tmdData[offset+7]&0x2 == 2 {
|
if tmdData[offset+7]&0x2 == 2 {
|
||||||
filePath = filepath.Join(outputDir, fmt.Sprintf("%08X.h3", id))
|
filePath = filepath.Join(outputDir, fmt.Sprintf("%08X.h3", id))
|
||||||
downloadURL = fmt.Sprintf("%s/%08X.h3", baseURL, id)
|
downloadURL = fmt.Sprintf("%s/%08X.h3", baseURL, id)
|
||||||
if err := downloadFile(progressWindow, client, downloadURL, filePath); err != nil {
|
if err := downloadFile(progressWindow, client, downloadURL, filePath, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var content contentInfo
|
var content contentInfo
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue