Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
IchBinLeoon committed Jul 18, 2021
1 parent 1158612 commit 3da6987
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
9 changes: 6 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
10 changes: 7 additions & 3 deletions utils/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ 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 {
return nil, err
}
transport.Proxy = http.ProxyURL(proxy)
}
return &http.Client{Transport: transport}, nil
return &http.Client{
Transport: transport,
Timeout: 5 * time.Minute,
}, nil
}

type HttpError struct {
Expand Down
6 changes: 3 additions & 3 deletions utils/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
13 changes: 13 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3da6987

Please sign in to comment.