This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_source_plugin.go
88 lines (76 loc) · 1.86 KB
/
spotify_source_plugin.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
package source_plugins
import (
"encoding/json"
"errors"
"io"
"github.com/disgoorg/disgolink/lavalink"
)
const SearchTypeSpotify lavalink.SearchType = "spsearch"
var (
_ lavalink.SourcePlugin = (*SpotifyPlugin)(nil)
_ lavalink.AudioTrack = (*SpotifyAudioTrack)(nil)
)
func NewSpotifyPlugin() *SpotifyPlugin {
return &SpotifyPlugin{}
}
type SpotifyPlugin struct{}
func (p *SpotifyPlugin) SourceName() string {
return "spotify"
}
func (p *SpotifyPlugin) Encode(track lavalink.AudioTrack, w io.Writer) error {
spotifyTrack, ok := track.(*SpotifyAudioTrack)
if !ok {
return errors.New("track is not a *SpotifyAudioTrack")
}
return EncodeISRCArtworkURL(spotifyTrack.ISRC, spotifyTrack.ArtworkURL, w)
}
func (p *SpotifyPlugin) Decode(info lavalink.AudioTrackInfo, r io.Reader) (lavalink.AudioTrack, error) {
isrc, artworkURL, err := DecodeISRCArtworkURL(r)
if err != nil {
return nil, err
}
return &SpotifyAudioTrack{
AudioTrack: &lavalink.BasicAudioTrack{
AudioTrackInfo: info,
},
ISRC: isrc,
ArtworkURL: artworkURL,
}, nil
}
type SpotifyAudioTrack struct {
lavalink.AudioTrack
ISRC *string `json:"isrc"`
ArtworkURL *string `json:"artwork_url"`
}
func (t *SpotifyAudioTrack) UnmarshalJSON(data []byte) error {
var v struct {
*lavalink.BasicAudioTrack
ISRC *string `json:"isrc"`
ArtworkURL *string `json:"artwork_url"`
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
t.AudioTrack = v.BasicAudioTrack
t.ISRC = v.ISRC
t.ArtworkURL = v.ArtworkURL
return nil
}
func (t *SpotifyAudioTrack) Clone() lavalink.AudioTrack {
var (
isrc, artworkURL *string
)
if t.ISRC != nil {
isrc = new(string)
*isrc = *t.ISRC
}
if t.ArtworkURL != nil {
artworkURL = new(string)
*artworkURL = *t.ArtworkURL
}
return &SpotifyAudioTrack{
AudioTrack: t.AudioTrack.Clone(),
ISRC: isrc,
ArtworkURL: artworkURL,
}
}