From fcf6453ea1568620fd03132484eb8f13ab3a3afb Mon Sep 17 00:00:00 2001 From: Dehemi Weerakkody <39078645+stargazer39@users.noreply.github.com> Date: Sun, 3 Apr 2022 20:14:43 +0530 Subject: [PATCH] Added retrying on fail and bug fixes --- download.go | 17 ++++++++++++++--- main.go | 23 +++++++++++++++-------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/download.go b/download.go index 3b69634..fc6520b 100644 --- a/download.go +++ b/download.go @@ -10,6 +10,7 @@ import ( "os/exec" "path" "strings" + "time" "github.com/pieterclaerhout/go-waitgroup" ) @@ -41,7 +42,13 @@ func DownloadWithHttp(client *http.Client, u string, name string) error { for { line, _, err := reader.ReadLine() - if err == io.EOF { + if err != nil { + switch err { + case io.EOF: + break + default: + return err + } break } @@ -59,7 +66,7 @@ func DownloadWithHttp(client *http.Client, u string, name string) error { m3u8_path := path.Join(name, "out.m3u8") if err := DownloadURLToPath(client, u, m3u8_path); err != nil { - log.Panic(err) + return err } wg := waitgroup.NewWaitGroup(16) @@ -88,8 +95,12 @@ func DownloadWithHttp(client *http.Client, u string, name string) error { new_url := url.String() new_fPath := path.Join(name, fName) + retry: if err := DownloadURLToPath(client, new_url, new_fPath); err != nil { - log.Panic(err) + log.Printf("File %s faild to download with Error :\n%v\nRetrying...", fName, err) + time.Sleep(time.Second * 5) + + goto retry } progress <- 1 diff --git a/main.go b/main.go index 444e87e..71e1530 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func main() { defer func() { fmt.Println("Press any key to exit.") - reader.ReadLine() + reader.ReadRune() }() // Check for ffmpeg @@ -61,7 +61,8 @@ func main() { u, err := url.Parse(*ed_url) if err != nil { - log.Panic(err) + log.Println(err) + return } videoId := strings.TrimSpace(u.Query().Get("id")) @@ -92,7 +93,8 @@ func main() { doc, err := goquery.NewDocumentFromResponse(resp) if err != nil { - log.Panic(err) + log.Println(err) + return } user := strings.TrimSpace(doc.Find("#dropdown08").Text()) @@ -107,13 +109,15 @@ func main() { resp, rErr := client.Client.Get(*ed_url) if rErr != nil { - log.Panic(err) + log.Println(err) + return } doc2, err := goquery.NewDocumentFromResponse(resp) if err != nil { - log.Panic(err) + log.Println(err) + return } title := strings.TrimSpace(doc2.Find("#content-wrapper > div > div.col-md-12 > h2").Text()) @@ -131,13 +135,15 @@ func main() { query.Add("full", "ZnVsbA==") if err := client.GetJson("https://lecturecapture.sliit.lk/webservice.php", &res, &query); err != nil { - log.Panicln(err) + log.Println(err) + return } ur, err := url.Parse("https://lecturecapture.sliit.lk/webservice.php") if err != nil { - log.Panicln(err) + log.Println(err) + return } if *high_quality { @@ -157,7 +163,8 @@ func main() { log.Println("Starting Download...") if err := DownloadWithHttp(client.Client, ur.String(), videoName); err != nil { - log.Panicln(err) + log.Println(err) + return } }