diff --git a/internal/album/main.go b/internal/album/main.go new file mode 100644 index 0000000..572b9a7 --- /dev/null +++ b/internal/album/main.go @@ -0,0 +1,40 @@ +package album + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "regexp" + + "github.com/mathismqn/godeez/internal/models" +) + +func GetDataByID(id string) (*models.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 models.Album + err = json.Unmarshal([]byte(matches[1]), &album) + if err != nil { + return nil, err + } + + return &album, nil +} diff --git a/internal/models/album.go b/internal/models/album.go new file mode 100644 index 0000000..9f26825 --- /dev/null +++ b/internal/models/album.go @@ -0,0 +1,7 @@ +package models + +type Album struct { + Songs struct { + Data []Song `json:"DATA"` + } `json:"SONGS"` +} diff --git a/internal/models/song.go b/internal/models/song.go new file mode 100644 index 0000000..95da414 --- /dev/null +++ b/internal/models/song.go @@ -0,0 +1,5 @@ +package models + +type Song struct { + Token string `json:"TRACK_TOKEN"` +}