feat(media): download and decrypt media

This commit is contained in:
Mathis Maquenne
2024-10-14 16:21:21 +02:00
parent 142dcfa30a
commit b700b6483c
6 changed files with 137 additions and 4 deletions
+39
View File
@@ -0,0 +1,39 @@
package crypto
import (
"crypto/cipher"
"crypto/md5"
"fmt"
"golang.org/x/crypto/blowfish"
)
const (
SecretKey = ""
IV = ""
)
func GetBlowfishKey(songID string) []byte {
hash := md5.Sum([]byte(songID))
hashHex := fmt.Sprintf("%x", hash)
key := []byte(SecretKey)
for i := 0; i < len(hash); i++ {
key[i] = key[i] ^ hashHex[i] ^ hashHex[i+16]
}
return key
}
func DecryptBlowfish(data, key []byte) ([]byte, error) {
block, err := blowfish.NewCipher(key)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, []byte(IV))
decrypted := make([]byte, len(data))
mode.CryptBlocks(decrypted, data)
return decrypted, nil
}
+72
View File
@@ -1,5 +1,13 @@
package deezer
import (
"fmt"
"net/http"
"os"
"github.com/mathismqn/godeez/internal/crypto"
)
type Media struct {
Errors []MediaError `json:"errors"`
Data []struct {
@@ -25,3 +33,67 @@ type Source struct {
URL string `json:"url"`
Provider string `json:"provider"`
}
const ChunkSize = 2048
func (m *Media) Download(filename, songID string) error {
url := m.Data[0].Media[0].Sources[0].URL
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
key := crypto.GetBlowfishKey(songID)
buffer := make([]byte, ChunkSize)
for chunk := 0; ; chunk++ {
totalRead := 0
for totalRead < ChunkSize {
n, err := resp.Body.Read(buffer[totalRead:])
if err != nil {
if err.Error() == "EOF" {
break
}
return err
}
if n > 0 {
totalRead += n
}
}
if totalRead == 0 {
break
}
if chunk%3 == 0 && totalRead == ChunkSize {
buffer, err = crypto.DecryptBlowfish(buffer, key)
if err != nil {
return err
}
}
_, err = file.Write(buffer[:totalRead])
if err != nil {
return err
}
if totalRead < ChunkSize {
break
}
}
return nil
}
+8 -2
View File
@@ -12,12 +12,14 @@ type Song struct {
ID string `json:"SNG_ID"`
ArtistName string `json:"ART_NAME"`
Title string `json:"SNG_TITLE"`
Version string `json:"VERSION"`
TrackToken string `json:"TRACK_TOKEN"`
}
const LicenseToken = ""
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)
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 {
@@ -25,6 +27,10 @@ func (s *Song) GetMediaData() (*Media, error) {
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusBadRequest {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
var media Media