refactor: use methods instead of functions
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package deezer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Album struct {
|
||||
Data struct {
|
||||
ID string `json:"ALB_ID"`
|
||||
Name string `json:"ALB_TITLE"`
|
||||
ArtistID string `json:"ART_ID"`
|
||||
ArtistName string `json:"ART_NAME"`
|
||||
CoverID string `json:"ALB_PICTURE"`
|
||||
ReleaseData string `json:"PHYSICAL_RELEASE_DATE"`
|
||||
} `json:"DATA"`
|
||||
Songs struct {
|
||||
Data []Song `json:"data"`
|
||||
} `json:"SONGS"`
|
||||
}
|
||||
|
||||
func GetAlbumData(id string) (*Album, error) {
|
||||
url := fmt.Sprintf("https://www.deezer.com/en/album/%s", id)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
re := regexp.MustCompile(`window\.__DZR_APP_STATE__ = (\{.*\})`)
|
||||
matches := re.FindStringSubmatch(string(body))
|
||||
if len(matches) != 2 {
|
||||
return nil, fmt.Errorf("error parsing response")
|
||||
}
|
||||
|
||||
var album Album
|
||||
err = json.Unmarshal([]byte(matches[1]), &album)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &album, nil
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package deezer
|
||||
|
||||
type Media struct {
|
||||
Errors []MediaError `json:"errors"`
|
||||
Data []struct {
|
||||
Media []struct {
|
||||
Type string `json:"media_type"`
|
||||
Cipher Cipher `json:"cipher"`
|
||||
Format string `json:"format"`
|
||||
Sources []Source `json:"sources"`
|
||||
}
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type MediaError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Cipher struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
URL string `json:"url"`
|
||||
Provider string `json:"provider"`
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package deezer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Song struct {
|
||||
ID string `json:"SNG_ID"`
|
||||
ArtistName string `json:"ART_NAME"`
|
||||
Title string `json:"SNG_TITLE"`
|
||||
TrackToken string `json:"TRACK_TOKEN"`
|
||||
}
|
||||
|
||||
func (s *Song) GetMediaData() (*Media, error) {
|
||||
licenseToken := ""
|
||||
reqBody := fmt.Sprintf(`{"license_token":"%s","media":[{"type":"FULL","formats":[{"cipher":"BF_CBC_STRIPE","format":"FLAC"}]}],"track_tokens":["%s"]}`, licenseToken, s.TrackToken)
|
||||
|
||||
resp, err := http.Post("https://media.deezer.com/v1/get_url", "application/json", bytes.NewBuffer([]byte(reqBody)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
|
||||
var media Media
|
||||
err = json.Unmarshal(body, &media)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(media.Errors) > 0 {
|
||||
if media.Errors[0].Code == 1000 {
|
||||
return nil, fmt.Errorf("invalid license token")
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%s", media.Errors[0].Message)
|
||||
}
|
||||
|
||||
return &media, nil
|
||||
}
|
||||
Reference in New Issue
Block a user