Skip to content

Commit

Permalink
#6 fix for artist
Browse files Browse the repository at this point in the history
  • Loading branch information
adithayyil committed Jun 28, 2024
1 parent a1e916d commit 3957a75
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bsdl artist lovbug
```
### Download a track from a link
```shell
bsdl track https://www.beatstars.com/beat/lovbug-skoolio-renter_135bpm-15063259
bsdl beat https://www.beatstars.com/beat/lovbug-skoolio-renter_135bpm-15063259
```

## Installation
Expand Down
12 changes: 10 additions & 2 deletions src/artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bsdl
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -105,7 +106,11 @@ func getArtistTracks(permalink string, client *http.Client) []Track {
var allTracks []Track

artistIDURL := fmt.Sprintf("https://main.v2.beatstars.com/musician?permalink=%s", permalink)
bodyTextArtistID := makeHTTPRequest(client, "GET", artistIDURL, nil)
artistInfoResp := makeHTTPRequest(client, "GET", artistIDURL, nil)
bodyTextArtistID, err := io.ReadAll(artistInfoResp.Body)
checkError(err)
defer artistInfoResp.Body.Close()

var musician MusicianResponse
json.Unmarshal(bodyTextArtistID, &musician)
userID := int(musician.Response.Data.Profile.UserID)
Expand All @@ -128,7 +133,10 @@ func getArtistTracks(permalink string, client *http.Client) []Track {
"filters": "",
"ruleContexts": []
}`, page, memberId)
artistDataResp := makeHTTPRequest(client, "POST", queryURL, strings.NewReader(data))
queryResp := makeHTTPRequest(client, "POST", queryURL, strings.NewReader(data))
artistDataResp, err := io.ReadAll(queryResp.Body)
checkError(err)
defer queryResp.Body.Close()

var artistData ArtistData
json.Unmarshal(artistDataResp, &artistData)
Expand Down
18 changes: 9 additions & 9 deletions src/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var rootCmd = &cobra.Command{
Use: "bsdl",
Version: "1.0",
Version: "1.1",
Short: `BeatStars Music Downloader`,
}

Expand All @@ -16,20 +16,20 @@ var threads int
var artist = &cobra.Command{
Use: "artist [permalink]",
Aliases: []string{"a"},
Short: "Download all tracks from an artist",
Long: `Download all tracks from an artist on BeatStars.`,
Short: "Download all beats from an artist",
Long: `Download all beats from an artist on BeatStars.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
artistPermalink := args[0]
downloadArtistTracks(artistPermalink, streamOnly, threads)
},
}

var track = &cobra.Command{
Use: "track [link]",
var beat = &cobra.Command{
Use: "beat [link]",
Aliases: []string{"t"},
Short: "Download a track from a link",
Long: `Download a track from a link on BeatStars.`,
Short: "Download a beat from a link",
Long: `Download a beat from a link on BeatStars.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
link := args[0]
Expand All @@ -44,7 +44,7 @@ func Execute() {
func init() {
artist.PersistentFlags().BoolVar(&streamOnly, "stream-only", false, "Get streams only and don't embed metadata")
artist.PersistentFlags().IntVar(&threads, "threads", 6, "Number of concurrent downloads")
track.PersistentFlags().BoolVar(&streamOnly, "stream-only", false, "Get streams only and don't embed metadata")
beat.PersistentFlags().BoolVar(&streamOnly, "stream-only", false, "Get streams only and don't embed metadata")
rootCmd.AddCommand(artist)
rootCmd.AddCommand(track)
rootCmd.AddCommand(beat)
}
5 changes: 2 additions & 3 deletions src/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ func downloadFile(track Track, client *http.Client, single bool, streamOnly bool
artistName := track.ArtistName
filename := track.Title

resp, err := client.Get(url)
checkError(err)
defer resp.Body.Close()
resp := makeHTTPRequest(client, "GET", url, nil)

bar := p.AddBar(resp.ContentLength,
mpb.PrependDecorators(
Expand All @@ -47,6 +45,7 @@ func downloadFile(track Track, client *http.Client, single bool, streamOnly bool
proxyReader := bar.ProxyReader(resp.Body)
bodyBytes, err := io.ReadAll(proxyReader)
checkError(err)
defer resp.Body.Close()

contentType := http.DetectContentType(bodyBytes[:512])
var extension string
Expand Down
6 changes: 5 additions & 1 deletion src/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bsdl
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"regexp"
Expand Down Expand Up @@ -59,7 +60,10 @@ func downloadTrack(link string, streamOnly bool) {

func getTrack(trackID string, client *http.Client) Track {
trackDataURL := fmt.Sprintf("https://main.v2.beatstars.com/beat?id=%s&fields=details", trackID)
trackDataReq := makeHTTPRequest(client, "GET", trackDataURL, nil)
trackDataResp := makeHTTPRequest(client, "GET", trackDataURL, nil)
trackDataReq, err := io.ReadAll(trackDataResp.Body)
checkError(err)
defer trackDataResp.Body.Close()

var trackData TrackData
json.Unmarshal(trackDataReq, &trackData)
Expand Down
29 changes: 18 additions & 11 deletions src/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bsdl

import (
"io"
"log"
"net/http"
"strings"
Expand All @@ -13,25 +12,33 @@ func checkError(err error, info ...string) {
}
}

func makeHTTPRequest(client *http.Client, method, url string, data *strings.Reader) []byte {
var req *http.Request
func checkStatusCode(statusCode int, info ...string) {
if statusCode == 404 {
log.Fatal("404 Not found ", info)
}

if statusCode != 200 {
log.Fatal(statusCode, info)
}
}

func makeHTTPRequest(client *http.Client, method, url string, data *strings.Reader) *http.Response {
var request *http.Request
var err error

if data != nil {
req, err = http.NewRequest(method, url, data)
request, err = http.NewRequest(method, url, data)
} else {
req, err = http.NewRequest(method, url, nil)
request, err = http.NewRequest(method, url, nil)
}
checkError(err)

req.Header = getDefaultHeaders()
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
request.Header = getDefaultHeaders()
response, err := client.Do(request)
checkError(err)
checkStatusCode(response.StatusCode, "Trouble accessing: ", url)

return bodyText
return response
}

func getDefaultHeaders() http.Header {
Expand Down

0 comments on commit 3957a75

Please sign in to comment.