package main import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "github.com/PuerkitoBio/goquery" ) //M is matrix type M struct { Matrix struct { Name string Mp3 string `json:"rendered_audio_url"` } } func main() { const pages = 43 const host = "https://www.beatlab.com" for i := 1; i < pages; i++ { d, err := goquery.NewDocument(fmt.Sprintf("https://www.beatlab.com/users/10745/matrices?id=10745&page=%d", i)) if err != nil { panic(err) } d.Find(".preview_track").Each( func(i int, s *goquery.Selection) { if v, ok := s.Attr("metadata_url"); ok { m, err := decode(host + v) if err != nil { fmt.Println(err) return } download(m) } }) } } func decode(url string) (matrix M, err error) { resp, err := http.Get(url) if err != nil { return } defer resp.Body.Close() err = json.NewDecoder(resp.Body).Decode(&matrix) return } func download(matrix M) { log.Println("Downloading: ", matrix.Matrix.Name, matrix.Matrix.Mp3) resp, err := http.Get(matrix.Matrix.Mp3) if err != nil { fmt.Println(err) return } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } err = ioutil.WriteFile(matrix.Matrix.Name+".mp3", data, 0644) if err != nil { fmt.Println(err) return } }