Last active
January 15, 2023 16:26
-
-
Save arpanetus/3b84de5d9fc984f534d9d3fbb1c34e72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"time" | |
"encoding/json" | |
"github.com/cavaliercoder/grab" | |
) | |
type myFilename struct { | |
name string | |
link string | |
} | |
func fileLinks(jsonstr []byte) []myFilename { | |
jsonMap := make(map[string]string) | |
_ = json.Unmarshal(jsonstr, &jsonMap) | |
mfs := make([]myFilename, 0) | |
for k, v := range jsonMap { | |
mfs = append(mfs, myFilename{k, v}) | |
} | |
return mfs | |
} | |
//func down() | |
func main() { | |
filename := os.Args[1] | |
dat, _ := ioutil.ReadFile(filename) | |
folderName := filename[:len(filename)-5] | |
fmt.Println(folderName) | |
mfs := fileLinks(dat) | |
_ = os.Mkdir(folderName, 0755) | |
reqs := make([]*grab.Request, 0) | |
client := grab.NewClient() | |
for _, v := range mfs { | |
req, _ := grab.NewRequest( | |
folderName+"/"+v.name+".mp3", | |
v.link) | |
reqs = append(reqs, req) | |
} | |
resp := <-client.DoBatch(100, reqs...) | |
t := time.NewTicker(500 * time.Millisecond) | |
defer t.Stop() | |
Loop: | |
for { | |
select { | |
case <-t.C: | |
fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n", | |
resp.BytesComplete(), | |
resp.Size, | |
100*resp.Progress()) | |
case <-resp.Done: | |
println("done!") | |
break Loop | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment