Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/soundcloud client #15

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 4 additions & 2 deletions cmd/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
func scdl(args []string) {
url := args[0]

// download song
soundcloud.Download(url)
// Create a new SoundCloud client
sc := soundcloud.NewClient("", nil)
// Download the song
sc.Download(url)

}
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func Execute() {
// Persistent Flags
// TODO: implement search functionality
//rootCmd.PersistentFlags().BoolVarP(&Find, "search", "s", false, "Option for searching for songs")

// TODO: implement private downloads
//rootCmd.PersistentFlags().BoolVarP(&Private, "private", "p", false, "Option for downloading private songs")
// Execute the command :)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
3 changes: 3 additions & 0 deletions pkg/soundcloud/artwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package soundcloud

type ArtworkService service
14 changes: 6 additions & 8 deletions pkg/soundcloud/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ import (
)

// Download queries the SoundCloud api and receives a m3u8 file, then binds the segments received into a .mp3 file
func Download(url string) {
// TODO: This client should be created higher up in the stack
soundcloud := NewClient()
func (s *Soundcloud) Download(url string) {

req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
}

// set Non Hacker User Agent
req.Header.Set("Accept", soundcloud.userAgent)
req.Header.Set("Accept", s.UserAgent)

resp, err := soundcloud.Client.Do(req)
resp, err := s.Client.Do(req)
if err != nil {
log.Println(err)
}
Expand All @@ -34,17 +32,17 @@ func Download(url string) {
log.Println(err)
}

streamURL, err := soundcloud.ConstructStreamURL(doc)
streamURL, err := s.ConstructStreamURL(doc)
if err != nil {
log.Println(err)
}

songName, err := soundcloud.GetTitle(doc)
songName, err := s.GetTitle(doc)
if err != nil {
log.Println(err)
}

artwork, err := soundcloud.GetArtwork(doc)
artwork, err := s.GetArtwork(doc)
if err != nil {
log.Println(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/soundcloud/get_client_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ func (s *Soundcloud) GetClientID() (string, error) {
log.Println("client_id not found")
return "", fmt.Errorf("client_id not found")
}

return clientID, nil
}
7 changes: 0 additions & 7 deletions pkg/soundcloud/service.go

This file was deleted.

41 changes: 37 additions & 4 deletions pkg/soundcloud/soundcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,51 @@ package soundcloud

import "net/http"

const (
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
version = "2.3.8"
)

// Soundcloud struct represents a http client to make requests to SoundCloud's API
type Soundcloud struct {
Client *http.Client // http client
// Client is the http client used to make requests
Client *http.Client

// UserAgent is the User-Agent header used for requests
UserAgent string
// AuthToken is the token used for authenticated requests
AuthToken string

// reuse a single struct instead of allocating one for each service on the heap.
common service

// Services used for talking to different parts of the Soundcloud

// Tracks is used for talking to the tracks endpoints
Tracks *TracksService
// Artwork is used for talking to the artwork endpoints
Artwork *ArtworkService
// User is used for talking to the user endpoints
// User *UsersService
}

userAgent string // User-Agent header
type service struct {
client *Soundcloud
}

// NewClient returns a new Soundcloud client
func NewClient() *Soundcloud {
func NewClient(authToken string, httpClient *http.Client) *Soundcloud {
if httpClient == nil {
httpClient = &http.Client{}
}

// TODO: Add a version header

// TODO: consume authToken for authenticated requests

return &Soundcloud{
Client: &http.Client{},

userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
UserAgent: userAgent,
}
}
3 changes: 3 additions & 0 deletions pkg/soundcloud/tracks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package soundcloud

type TracksService service
1 change: 0 additions & 1 deletion vendor/github.com/PuerkitoBio/goquery/.gitattributes

This file was deleted.

16 changes: 0 additions & 16 deletions vendor/github.com/PuerkitoBio/goquery/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions vendor/github.com/PuerkitoBio/goquery/LICENSE

This file was deleted.

198 changes: 0 additions & 198 deletions vendor/github.com/PuerkitoBio/goquery/README.md

This file was deleted.

Loading
Loading