Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
implement spotify source extension
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jan 25, 2022
1 parent 2920b41 commit c52357e
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
# spotify-plugin
[![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.
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
80 changes: 80 additions & 0 deletions spotify_plugin.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit c52357e

Please sign in to comment.