diff --git a/cmd/get.go b/cmd/get.go index 999efe0..3d79df2 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -96,7 +96,7 @@ func get(urls []string) error { return err } if len(urls) > 1 { - if outputNameFlag != "" { + if outputNameFlag != "" || utils.CheckIfMultipleInArray(urls, url) { path = fmt.Sprintf("%s-%d.mp4", path[:len(path)-4], i) } } @@ -153,10 +153,11 @@ func get(urls []string) error { } func download(downloader *utils.Downloader, video *types.Video) error { + fmt.Printf("\n%s", video.HentaiVideo.Name) if utils.CheckIfPathExists(video.OutputPath) { - return fmt.Errorf("error: file '%s' already exists", video.OutputPath) + fmt.Printf("\nwarning: file '%s' already exists, skipping\n", video.OutputPath) + return nil } - fmt.Printf("\n%s", video.HentaiVideo.Name) tmpPath = fmt.Sprintf("%s-%d", video.OutputPath[:len(video.OutputPath)-4], video.VideosManifest.Servers[0].Streams[video.StreamIndex].ID) err := downloader.Download(fmt.Sprintf("%s%d", apiM3U8, video.VideosManifest.Servers[0].Streams[video.StreamIndex].ID), tmpPath, video.OutputPath) if err != nil { @@ -218,6 +219,8 @@ func getStreamIndex(streams []types.Stream) (int, error) { func getOutputPath(slug string, quality string) (string, error) { var outputName string if outputNameFlag != "" { + re := regexp.MustCompile(`[^a-zA-Z0-9._-]`) + outputNameFlag = re.ReplaceAllString(outputNameFlag, "") if !strings.HasSuffix(outputNameFlag, ".mp4") { outputNameFlag += ".mp4" } diff --git a/cmd/root.go b/cmd/root.go index ddc9d44..434578d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,7 +9,7 @@ import ( var rootCmd = &cobra.Command{ Use: "hanime", - Version: "1.0.2", + Version: "1.0.3", Short: "Command-line tool to download videos from hanime.tv", Long: "Command-line tool to download videos from hanime.tv\n\nComplete documentation is available at https://github.com/IchBinLeoon/hanime", } diff --git a/utils/downloader.go b/utils/downloader.go index 477f5dd..27b232e 100644 --- a/utils/downloader.go +++ b/utils/downloader.go @@ -8,12 +8,17 @@ import ( "net/http" "os" "path/filepath" + "runtime" "strconv" "sync" "github.com/grafov/m3u8" ) +func init() { + runtime.GOMAXPROCS(runtime.NumCPU()) +} + type Downloader struct { Client *http.Client } @@ -112,7 +117,7 @@ func (downloader *Downloader) Download(m3u8Url string, tmpPath string, outputPat func createTmpFolder(path string) error { if CheckIfPathExists(path) { - fmt.Print(fmt.Errorf("\nerror: cannot create temporary folder, path '%s' already exists", path)) + fmt.Print(fmt.Errorf("error: cannot create temporary folder, path '%s' already exists", path)) os.Exit(1) } @@ -185,8 +190,7 @@ func getKey(client *http.Client, url string) ([]byte, error) { return data, nil } - -func downloadTS(client *http.Client, url string, path string, key []byte, iv[]byte) error { +func downloadTS(client *http.Client, url string, path string, key []byte, iv []byte) error { body, err := Request("GET", client, url, nil, nil) if err != nil { return err diff --git a/utils/http.go b/utils/http.go index da4313c..723d0a7 100644 --- a/utils/http.go +++ b/utils/http.go @@ -5,10 +5,14 @@ import ( "io" "net/http" "net/url" + "time" ) func DefaultClient(proxyUrl string) (*http.Client, error) { - transport := &http.Transport{DisableCompression: true} + transport := &http.Transport{ + DisableCompression: true, + IdleConnTimeout: 30 * time.Second, + } if proxyUrl != "" { proxy, err := url.Parse(proxyUrl) if err != nil { @@ -16,7 +20,10 @@ func DefaultClient(proxyUrl string) (*http.Client, error) { } transport.Proxy = http.ProxyURL(proxy) } - return &http.Client{Transport: transport}, nil + return &http.Client{ + Transport: transport, + Timeout: 5 * time.Minute, + }, nil } type HttpError struct { diff --git a/utils/progress.go b/utils/progress.go index 80125e7..b2f693c 100644 --- a/utils/progress.go +++ b/utils/progress.go @@ -26,14 +26,14 @@ func (bar *Bar) Next() { bar.percent = bar.getPercent() if bar.percent != last { var i int64 = 0 - for ; i < bar.percent - last; i++ { + for ; i < bar.percent-last; i++ { bar.rate += bar.graph } - fmt.Printf("\r[%-50s]%3d%% %8d/%d", bar.rate, bar.percent * 2, bar.current, bar.total) + fmt.Printf("\r[%-50s]%3d%% %8d/%d", bar.rate, bar.percent*2, bar.current, bar.total) } } -func (bar *Bar) Finish(){ +func (bar *Bar) Finish() { fmt.Println() } diff --git a/utils/utils.go b/utils/utils.go index 4d50c6e..09f9398 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -27,3 +27,16 @@ func CheckIfInArray(arr []string, val string) bool { } return false } + +func CheckIfMultipleInArray(arr []string, val string) bool { + counter := 0 + for _, i := range arr { + if i == val { + counter++ + } + if counter > 1 { + return true + } + } + return false +}