-
Notifications
You must be signed in to change notification settings - Fork 0
/
musixmatch.go
152 lines (122 loc) · 4.28 KB
/
musixmatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Package musixmatch provides a client for using the Musixmatch API.
*/
package musixmatch
import (
"fmt"
"github.com/fr05t1k/musixmatch/config"
"github.com/fr05t1k/musixmatch/entity/lyrics"
"github.com/fr05t1k/musixmatch/entity/snippet"
"github.com/fr05t1k/musixmatch/entity/subtitle"
"github.com/fr05t1k/musixmatch/entity/track"
"github.com/fr05t1k/musixmatch/entity/track/list"
"github.com/fr05t1k/musixmatch/http"
"github.com/fr05t1k/musixmatch/http/methods"
"net/url"
)
type Client struct {
ApiKey string
}
func NewClient(apiKey string) Client {
return Client{ApiKey: apiKey}
}
// Get the lyrics of a track.
func (m *Client) GetLyrics(trackId uint32) (*lyrics.Lyrics, error) {
params := m.paramsWithApiKey()
params.Add(config.TrackId, fmt.Sprintf("%d", trackId))
var lyricsResponse lyrics.Response
requestUrl := http.GetURLString(methods.TrackLyricsGet, params)
err := http.RequestEntity(requestUrl, &lyricsResponse)
if err != nil {
return nil, err
}
return &lyricsResponse.Message.Body.Lyrics, nil
}
// Get the snippet for a given track.
//
// A lyrics snippet is a very short representation of a song lyrics.
// It’s usually twenty to a hundred characters long and it’s calculated
// extracting a sequence of words from the lyrics.
func (m *Client) GetSnippet(trackId uint32) (*snippet.Snippet, error) {
params := m.paramsWithApiKey()
params.Add(config.TrackId, fmt.Sprintf("%d", trackId))
requestUrl := http.GetURLString(methods.TrackSnippetGet, params)
var snippetResponse snippet.Response
err := http.RequestEntity(requestUrl, &snippetResponse)
if err != nil {
return nil, err
}
return &snippetResponse.Message.Body.Snippet, nil
}
// Retrieve the subtitle of a track.
//
// Return the subtitle of a track in LRC or DFXP format.
func (m *Client) GetSubtitles(trackId uint32) (*subtitle.Subtitle, error) {
params := m.paramsWithApiKey()
params.Add(config.TrackId, fmt.Sprintf("%d", trackId))
var subtitleResponse subtitle.Response
requestUrl := http.GetURLString(methods.TrackSubtitleGet, params)
err := http.RequestEntity(requestUrl, &subtitleResponse)
if err != nil {
return nil, err
}
return &subtitleResponse.Message.Body.Subtitle, nil
}
// Search tracks using the given criteria.
func (m *Client) SearchTrack(request http.SearchRequest) ([]list.TrackList, error) {
params := m.paramsWithApiKey()
params.Add(config.Query, request.Query)
params.Add(config.QueryArtist, request.QueryArtist)
params.Add(config.QueryTrack, request.QueryTrack)
params.Add(config.Page, fmt.Sprintf("%d", request.Page))
params.Add(config.PageSize, fmt.Sprintf("%d", request.PageSize))
if request.HasLyrics {
params.Add(config.FHasLyrics, "1")
} else {
params.Add(config.FHasLyrics, "0")
}
var tracksResponse list.Response
requestUrl := http.GetURLString(methods.TrackSearch, params)
err := http.RequestEntity(requestUrl, &tracksResponse)
if err != nil {
return []list.TrackList{}, err
}
return tracksResponse.Message.Body.Tracks, nil
}
// Get a track info from a database: title, artist, instrumental flag and cover art.
func (m *Client) GetTrack(id uint32) (*track.Track, error) {
params := m.paramsWithApiKey()
params.Add(config.TrackId, fmt.Sprintf("%d", id))
var trackResponse track.Response
requestUrl := http.GetURLString(methods.TrackGet, params)
err := http.RequestEntity(requestUrl, &trackResponse)
if err != nil {
return nil, err
}
return &trackResponse.Message.Body.Track, nil
}
// Match your song against a database.
func (m *Client) GetMatchingTrack(qTrack string, qArtist string) (*track.Track, error) {
params := m.paramsWithApiKey()
params.Add(config.QueryTrack, qTrack)
params.Add(config.QueryArtist, qArtist)
var trackResponse track.Response
requestUrl := http.GetURLString(methods.MatcherTrackGet, params)
err := http.RequestEntity(requestUrl, &trackResponse)
if err != nil {
return nil, err
}
return &trackResponse.Message.Body.Track, nil
}
// Make request with your parameters
func (m *Client) Request(method string, params url.Values) ([]byte, error) {
params.Add(config.ApiKey, m.ApiKey)
requestUrl := http.GetURLString(method, params)
return http.SendRequest(requestUrl)
}
// Retrieve the url.Values with predefined api key
func (m *Client) paramsWithApiKey() url.Values {
params := url.Values{}
params.Add(config.ApiKey, m.ApiKey)
return params
}