feat(album): get data by ID

This commit is contained in:
Mathis Maquenne
2024-10-08 01:35:53 +02:00
parent 4f82007a49
commit 47ebaf143b
3 changed files with 52 additions and 0 deletions
+40
View File
@@ -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
}
+7
View File
@@ -0,0 +1,7 @@
package models
type Album struct {
Songs struct {
Data []Song `json:"DATA"`
} `json:"SONGS"`
}
+5
View File
@@ -0,0 +1,5 @@
package models
type Song struct {
Token string `json:"TRACK_TOKEN"`
}