Skip to content

Commit

Permalink
add types and methods;
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVasconcelos committed Feb 17, 2020
1 parent f428fb1 commit f89ff29
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 10 deletions.
63 changes: 56 additions & 7 deletions cmd/tidal.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,73 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/lucaslg26/tidal-go/pkg/tidal"
)

func main() {

enc, _ := ioutil.ReadFile("input.flac")
uid, _ := strconv.Atoi(os.Getenv("USER_ID"))

MyMedia := &tidal.Media{
Media: enc,
EncryptionKey: []byte(""),
Tidal := &tidal.Client{
Token: "4zx46pyr9o8qZNRw",
Username: os.Getenv("TIDAL_USERNAME"),
Password: os.Getenv("TIDAL_PASSWORD"),
SoundQuality: "LOSSLESS",
User: &tidal.User{
UserID: uid,
SessionID: os.Getenv("SESSION_ID"),
CountryCode: "BR",
},
}

MyMedia.GetKey()
MyMedia.DecryptMedia()
//Tidal.Login()

err := ioutil.WriteFile("output.flac", MyMedia.Media, 0644)
fmt.Printf("%v\n", Tidal.User)

// search, _ := Tidal.Search("Garden Of The Titans")

// fmt.Println("Artists:")
// for _, v := range search.Artists.Items {
// fmt.Printf("%s\n", v.Name)
// }

// fmt.Println("Playlists:")
// for _, v := range search.Playlists.Items {
// fmt.Printf("%s\n", v.Title)
// }

// fmt.Println("Albums:")
// for _, v := range search.Albums.Items {
// fmt.Printf("%s\n", v.Title)
// }

// fmt.Println("Tracks:")
// for _, v := range search.Tracks.Items {
// fmt.Printf("%s\n", v.Title)
// }

track, err := Tidal.GetStream(&tidal.Track{ID: 1267842})

if err != nil {
fmt.Println(err)
}

media, err := Tidal.GetMedia(track)

if err != nil {
fmt.Println(err)
}

media.GetKey()

media.DecryptMedia()

err = ioutil.WriteFile("media/output.flac", media.Media, 0644)

if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/tidal/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/hex"
)

var master_key, _ = hex.DecodeString("5089534C432698B7C6A30A3F502EB4C761F8E56E8C74681345FA3FBA6838EF9E")
var masterKey, _ = hex.DecodeString("5089534C432698B7C6A30A3F502EB4C761F8E56E8C74681345FA3FBA6838EF9E")

func (m *Media) GetKey() error {

Expand All @@ -17,7 +17,7 @@ func (m *Media) GetKey() error {
return err
}

block, err := aes.NewCipher(master_key)
block, err := aes.NewCipher(masterKey)

if err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions pkg/tidal/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tidal

import (
"encoding/json"
"net/url"
)

func (c *Client) Login() error {

var loginResponse *User

resp, err := c.post("/login/username",
url.Values{
"username": {c.Username},
"password": {c.Password},
},
url.Values{
"X-Tidal-Token": {c.Token},
},
)

if err != nil {
return err
}

err = json.Unmarshal(resp, &loginResponse)

if err != nil {
return err
}

c.User = loginResponse

return nil
}
159 changes: 159 additions & 0 deletions pkg/tidal/tidal.go
Original file line number Diff line number Diff line change
@@ -1 +1,160 @@
package tidal

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)

func (c *Client) Search(search string) (*Search, error) {

var result *Search

resp, err := c.get(c.getEndpoint("/search"),
url.Values{
"query": {search},
"countryCode": {c.User.CountryCode},
},
url.Values{
"X-Tidal-Token": {c.Token},
},
)

if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &result)

if err != nil {
return nil, err
}

return result, nil
}

func (c *Client) GetStream(track *Track) (*Stream, error) {

var stream *Stream

resp, err := c.get(c.getEndpoint("/tracks/"+strconv.Itoa(track.ID)+"/streamUrl"),
url.Values{
"soundQuality": {c.SoundQuality},
"countryCode": {c.User.CountryCode},
"sessionId": {c.User.SessionID},
},
url.Values{
"X-Tidal-Token": {c.Token},
},
)

if err != nil {
return nil, err
}

err = json.Unmarshal(resp, &stream)

if err != nil {
return nil, err
}

return stream, nil
}

func (c *Client) GetMedia(stream *Stream) (Media, error) {

var media Media

resp, err := http.Get(stream.URL)

if err != nil {
return media, err
}

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return media, err
}

media.Media = body
media.EncryptionKey = []byte(stream.EncryptionKey)

return media, nil
}

func (c *Client) post(path string, form url.Values, headers map[string][]string) ([]byte, error) {

client := http.Client{}

req, err := http.NewRequest("POST", c.getEndpoint(path), strings.NewReader(form.Encode()))

if err != nil {
return nil, err
}

req.PostForm = form

req.Header = headers

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(req)

if err != nil {
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, errors.New(string(body))
}

return body, err
}

func (c *Client) get(url string, query url.Values, headers map[string][]string) ([]byte, error) {

client := http.Client{}

req, err := http.NewRequest("GET", url, nil)

if err != nil {
return nil, err
}

req.URL.RawQuery = query.Encode()

req.Header = headers

resp, err := client.Do(req)

if err != nil {
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)

if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, errors.New(string(body))
}

return body, err
}

func (c *Client) getEndpoint(path string) string {
return "https://api.tidalhifi.com/v1" + path
}
Loading

0 comments on commit f89ff29

Please sign in to comment.