From c52357e5a66fb6329c8f8d716574395669ea77b6 Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Tue, 25 Jan 2022 02:13:52 +0100 Subject: [PATCH] implement spotify source extension --- .gitignore | 1 + README.md | 63 ++++++++++++++++++++++++++++++++++++- go.mod | 14 +++++++++ go.sum | 20 ++++++++++++ spotify_plugin.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 spotify_plugin.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 5253691..818d3c2 100644 --- a/README.md +++ b/README.md @@ -1 +1,62 @@ -# spotify-plugin \ No newline at end of file +[![Go Reference](https://pkg.go.dev/badge/github.com/DisgoOrg/spotify-plugin.svg)](https://pkg.go.dev/github.com/DisgoOrg/spotify-plugin) +[![Go Report](https://goreportcard.com/badge/github.com/DisgoOrg/spotify-plugin)](https://goreportcard.com/report/github.com/DisgoOrg/spotify-plugin) +[![Go Version](https://img.shields.io/github/go-mod/go-version/DisgoOrg/spotify-plugin)](https://golang.org/doc/devel/release.html) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/DisgoOrg/spotify-plugin/blob/master/LICENSE) +[![Disgo Version](https://img.shields.io/github/v/tag/DisgoOrg/spotify-plugin?label=release)](https://github.com/DisgoOrg/spotify-plugin/releases/latest) +[![Disgo Discord](https://discord.com/api/guilds/817327181659111454/widget.png)](https://discord.gg/NFmvZYmZMF) + +# spotify-plugin + +spotify-plugin is a [Lavalink](https://github.com/freyacodes/Lavalink) Client which supports the latest Lavalink 3.4 release + +## Getting Started + +### Installing + +```sh +go get github.com/DisgoOrg/spotify-plugin +``` + +## Usage + +```go +import ( + "github.com/DisgoOrg/disgolink/lavalink" + "github.com/DisgoOrg/spotify-plugin" +) +// create new lavalink and add the spotify plugin +link := lavalink.New( + lavalink.WithUserID("user_id_here"), + lavalink.WithPlugins(spotify.New()), +) + +// when loading track you can type cast the track to a spotify track to access extra data +_ = link.BestRestClient().LoadItemHandler("https://open.spotify.com/track/3yk51U329nwdpeIHV0O5ez", lavalink.NewResultHandler( + func (track lavalink.AudioTrack) { + if spotifyTrack, ok := track.(*spotify.AudioTrack); ok { + println("ISRC: ", spotifyTrack.AudioTrackInfo.ISRC) + println("ArtworkURL: ", spotifyTrack.AudioTrackInfo.ArtworkURL) + } + }, + func (playlist lavalink.AudioPlaylist) {}, + func (tracks []lavalink.AudioTrack) {}, + func () {}, + func (ex lavalink.FriendlyException) {}, +)) +``` + +## Example + +You can find an example under [_example](https://github.com/DisgoOrg/spotify-plugin/tree/master/_example) + +## Troubleshooting + +For help feel free to open an issues or reach out on [Discord](https://discord.gg/NFmvZYmZMF) + +## Contributing + +Contributions are welcomed but for bigger changes please first reach out via [Discord](https://discord.gg/NFmvZYmZMF) or create an issue to discuss your intentions and ideas. + +## License + +Distributed under the [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/DisgoOrg/spotify-plugin/blob/master/LICENSE). See LICENSE for more information. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ebd1584 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module github.com/DisgoOrg/spotify-plugin + +go 1.17 + +require ( + github.com/DisgoOrg/disgolink v1.1.1 +) + +require ( + github.com/DisgoOrg/log v1.1.2 // indirect + github.com/DisgoOrg/snowflake v1.0.1 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/pkg/errors v0.9.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e68e309 --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/DisgoOrg/disgolink v1.1.1 h1:AYs4gZa920ejBaj8QZZD15rZTidUzCVaEe7zGAOy+4w= +github.com/DisgoOrg/disgolink v1.1.1/go.mod h1:6iqOB9yi9MbNB804oVlhFO9Ba5A31Tb/4q95wKhm0bc= +github.com/DisgoOrg/log v1.1.2 h1:tGJS4jaH1PyjPRHybHp8WpYJ/4fR3fYWT4Mv1PoDGBM= +github.com/DisgoOrg/log v1.1.2/go.mod h1:tSMofXaNhQNvzLRoL4tAiCG9yGY1ES5DLvduh7e9GRU= +github.com/DisgoOrg/snowflake v1.0.1 h1:+UKgfjOnj2KBj/20tWKFNzhbo9awzThkY12ZaOQzaSg= +github.com/DisgoOrg/snowflake v1.0.1/go.mod h1:jIQVlVmElm2OGt6v52ITf/71ODaU09chUCflxt8+3yM= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/spotify_plugin.go b/spotify_plugin.go new file mode 100644 index 0000000..462cdd2 --- /dev/null +++ b/spotify_plugin.go @@ -0,0 +1,80 @@ +package spotify + +import ( + "errors" + "io" + + "github.com/DisgoOrg/disgolink/lavalink" +) + +const SearchTypeSpotify lavalink.SearchType = "spsearch" + +var ( + _ lavalink.SourceExtension = (*Plugin)(nil) +) + +func New() *Plugin { + return &Plugin{} +} + +type Plugin struct{} + +func (p *Plugin) SourceName() string { + return "spotify" +} + +func (p *Plugin) Encode(track lavalink.AudioTrack, w io.Writer) (err error) { + spotifyTrack, ok := track.(*AudioTrack) + if !ok { + return errors.New("track is not a SpotifyAudioTrack") + } + + if err = lavalink.WriteNullableString(w, spotifyTrack.AudioTrackInfo.ISRC); err != nil { + return + } + return lavalink.WriteNullableString(w, spotifyTrack.AudioTrackInfo.ArtworkURL) +} + +func (p *Plugin) Decode(track string, info lavalink.AudioTrackInfo, r io.Reader) (spotifyTrack lavalink.AudioTrack, err error) { + var isrc, artworkURL *string + + if isrc, err = lavalink.ReadNullableString(r); err != nil { + return + } + if artworkURL, err = lavalink.ReadNullableString(r); err != nil { + return + } + + return &AudioTrack{ + AudioTrack: track, + AudioTrackInfo: &AudioTrackInfo{ + AudioTrackInfo: info, + ISRC: isrc, + ArtworkURL: artworkURL, + }, + }, nil +} + +var ( + _ lavalink.AudioTrack = (*AudioTrack)(nil) + _ lavalink.AudioTrackInfo = (*AudioTrackInfo)(nil) +) + +type AudioTrack struct { + AudioTrack string `json:"track"` + AudioTrackInfo *AudioTrackInfo `json:"info"` +} + +func (t *AudioTrack) Track() string { + return t.AudioTrack +} + +func (t *AudioTrack) Info() lavalink.AudioTrackInfo { + return t.AudioTrackInfo +} + +type AudioTrackInfo struct { + lavalink.AudioTrackInfo + ISRC *string `json:"isrc"` + ArtworkURL *string `json:"artwork_url"` +}