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
/
deezer_source_plugin.go
91 lines (79 loc) · 1.9 KB
/
deezer_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
89
90
91
package source_plugins
import (
"encoding/json"
"errors"
"io"
"github.com/disgoorg/disgolink/lavalink"
)
const (
SearchTypeDeezer lavalink.SearchType = "dzsearch"
SearchTypeDeezerISRC lavalink.SearchType = "dzisrc"
)
var (
_ lavalink.SourcePlugin = (*DeezerPlugin)(nil)
_ lavalink.AudioTrack = (*DeezerAudioTrack)(nil)
)
func NewDeezerPlugin() *DeezerPlugin {
return &DeezerPlugin{}
}
type DeezerPlugin struct{}
func (p *DeezerPlugin) SourceName() string {
return "deezer"
}
func (p *DeezerPlugin) Encode(track lavalink.AudioTrack, w io.Writer) error {
deezerTrack, ok := track.(*DeezerAudioTrack)
if !ok {
return errors.New("track is not a *DeezerAudioTrack")
}
return EncodeISRCArtworkURL(deezerTrack.ISRC, deezerTrack.ArtworkURL, w)
}
func (p *DeezerPlugin) Decode(info lavalink.AudioTrackInfo, r io.Reader) (lavalink.AudioTrack, error) {
isrc, artworkURL, err := DecodeISRCArtworkURL(r)
if err != nil {
return nil, err
}
return &DeezerAudioTrack{
AudioTrack: &lavalink.BasicAudioTrack{
AudioTrackInfo: info,
},
ISRC: isrc,
ArtworkURL: artworkURL,
}, nil
}
type DeezerAudioTrack struct {
lavalink.AudioTrack
ISRC *string `json:"isrc"`
ArtworkURL *string `json:"artwork_url"`
}
func (t *DeezerAudioTrack) 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 *DeezerAudioTrack) 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 &DeezerAudioTrack{
AudioTrack: t.AudioTrack.Clone(),
ISRC: isrc,
ArtworkURL: artworkURL,
}
}