spotify

package module
v0.0.0-...-e57e160 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2020 License: MIT Imports: 12 Imported by: 0

README

spotify

GoDoc Build Status Go Report Card

A Lightweight Spotify API Wrapper For Golang

Usage

import "github.com/tobiola/spotify"

Getting started

Official Spotfy API

https://developer.spotify.com/documentation/web-api/

Authentication
clientId := "[Client ID Here]"
clientSecret := "[Client Secret Here]"
redirectUri := "[URL that should handle the callback]"

func HandleLogin(w http.ResponseWriter, r *http.Request) {
 	scope := "streaming user-read-private user-read-email user-modify-playback-state"

 	redirectLink, _ := spotify.GetRedirectLink(redirectUri, clientId, scope)

 	http.Redirect(w, r, redirectLink, http.StatusSeeOther)
}

func HandleCallback(w http.ResponseWriter, r *http.Request) {
	client, _ := spotify.GetClientFromCallback(r.URL, redirectUri, clientId, clientSecret)
}
Player
// Play 
client.Play()
// or
client.PlayTrack(track)
// or
client.PlayTracks(tracks)

// Pause
client.Pause()
options := spotify.SearchOptions{Query: "hotline bling", Type: "track"}
result, _ := client.Search(options)

client.Play(result.Tracks.Items[0])
Progress
  • Authentication
  • Albums
  • Browse
  • Follow
  • Library
  • Personalization
  • Player
  • Playlists
  • Search
  • Tracks
  • Users Profile

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAccessTokenFromRefreshToken

func GetAccessTokenFromRefreshToken(refreshToken string, clientId string, clientSecret string) (string, error)
func GetRedirectLink(callbackUrl string, clientId string, scope string) (string, error)

Types

type Actions

type Actions struct {
	Disallows Disallows `json:"disallows"`
}

type Album

type Album struct {
	AlbumType            string            `json:"album_type"`
	Artists              []ArtistSimple    `json:"artists"`
	AvailableMarkets     []string          `json:"available_markets"`
	Copyrights           []Copyright       `json:"copyrights"`
	ExternalIds          map[string]string `json:"external_ids"`
	ExternalUrls         map[string]string `json:"external_urls"`
	Genres               []string          `json:"genres"`
	Href                 string            `json:"href"`
	Id                   string            `json:"id"`
	Images               []Image           `json:"images"`
	Label                string            `json:"label"`
	Name                 string            `json:"name"`
	Popularity           int               `json:"popularity"`
	ReleaseDate          string            `json:"release_date"`
	ReleaseDatePrecision string            `json:"release_date_precision"`
	Restrictions         Restrictions      `json:"restrictions"`
	Tracks               []TrackSimple     `json:"tracks"`
	Type                 string            `json:"type"`
	Uri                  string            `json:"uri"`
}

type AlbumPaging

type AlbumPaging struct {
	Href     string        `json:"href"`
	Items    []AlbumSimple `json:"items"`
	Limit    int           `json:"limit"`
	Next     string        `json:"next"`
	Offset   int           `json:"offset"`
	Previous string        `json:"previous"`
	Total    int           `json:"total"`
}

type AlbumSimple

type AlbumSimple struct {
	AlbumGroup           string            `json:"album_group"`
	AlbumType            string            `json:"album_type"`
	Artists              []ArtistSimple    `json:"artists"`
	AvailableMarkets     []string          `json:"available_markets"`
	ExternalUrls         map[string]string `json:"external_url"`
	Genres               []string          `json:"genres"`
	Href                 string            `json:"href"`
	Id                   string            `json:"id"`
	Images               []Image           `json:"images"`
	Name                 string            `json:"name"`
	ReleaseDate          string            `json:"release_date"`
	ReleaseDatePrecision string            `json:"release_date_precision"`
	Restrictions         Restrictions      `json:"restrictions"`
	Type                 string            `json:"type"`
	Uri                  string            `json:"uri"`
}

type Artist

type Artist struct {
	ExternalUrls map[string]string `json:"external_url"`
	Followers    Followers         `json:"followers"`
	Genres       []string          `json:"genres"`
	Href         string            `json:"href"`
	Id           string            `json:"id"`
	Images       []Image           `json:"images"`
	Name         string            `json:"name"`
	Popularity   int               `json:"popularity"`
	Uri          string            `json:"uri"`
	Type         string            `json:"type"`
}

type ArtistPaging

type ArtistPaging struct {
	Href     string   `json:"href"`
	Items    []Artist `json:"items"`
	Limit    int      `json:"limit"`
	Next     string   `json:"next"`
	Offset   int      `json:"offset"`
	Previous string   `json:"previous"`
	Total    int      `json:"total"`
}

type ArtistSimple

type ArtistSimple struct {
	ExternalUrls map[string]string `json:"external_url"`
	Href         string            `json:"href"`
	Id           string            `json:"id"`
	Name         string            `json:"name"`
	Uri          string            `json:"uri"`
	Type         string            `json:"type"`
}

type AudioFeatures

type AudioFeatures struct {
	Acousticness     float32 `json:"acousticness"`
	AnalysisUrl      string  `json:"analysis_url"`
	Danceability     float32 `json:"danceability"`
	DurationMs       int     `json:"duration_ms"`
	Energy           float32 `json:"energy"`
	Id               string  `json:"id"`
	Instrumentalness float32 `json:"instrumentalness"`
	Key              int     `json:"key"`
	Liveness         float32 `json:"liveness"`
	Loudness         float32 `json:"loudness"`
	Mode             int     `json:"mode"`
	Speechiness      float32 `json:"speechiness"`
	Tempo            float32 `json:"tempo"`
	TimeSignature    int     `json:"time_signature"`
	TrackHref        string  `json:"track_href"`
	Type             string  `json:"type"`
	Uri              string  `json:"uri"`
	Valence          float32 `json:"valence"`
}

type Category

type Category struct {
	Href  string  `json:"href"`
	Icons []Image `json:"icons"`
	Id    string  `json:"id"`
	Name  string  `json:"name"`
}

type Client

type Client struct {
	AccessToken  string
	RefreshToken string
	ClientId     string
	ClientSecret string
}

func GetClientFromCallback

func GetClientFromCallback(requestUrl *url.URL, redirectUri string, clientId string, clientSecret string) (Client, error)

func (*Client) GetAlbum

func (c *Client) GetAlbum(albumId string) (Album, error)

func (*Client) GetAlbumTracks

func (c *Client) GetAlbumTracks(albumId string) (TrackPaging, error)

func (*Client) GetCurrentPlayback

func (c *Client) GetCurrentPlayback() (CurrentlyPlayingContext, error)

Get information about the user’s current playback state, including track, track progress, and active device.

func (*Client) GetCurrentUserProfile

func (c *Client) GetCurrentUserProfile() (UserPrivate, error)

func (*Client) GetDevices

func (c *Client) GetDevices() ([]Device, error)

Get a User's Available Devices

func (*Client) GetRecentlyPlayedTracks

func (c *Client) GetRecentlyPlayedTracks() (PlayHistoryCursorBasedPaging, error)

Get tracks from the current user’s recently played tracks. Returns the most recent 20 tracks played by a user. Note that a track currently playing will not be visible in play history until it has completed. A track must be played for more than 30 seconds to be included in play history. Any tracks listened to while the user had “Private Session” enabled in their client will not be returned in the list of recently played tracks.

func (*Client) GetRecentlyPlayedTracksAfter

func (c *Client) GetRecentlyPlayedTracksAfter(after string) (PlayHistoryCursorBasedPaging, error)

The endpoint uses a bidirectional cursor for paging. Follow the next field with the before parameter to move back in time, or use the after parameter to move forward in time. If you supply no before or after parameter, the endpoint will return the most recently played songs, and the next link will page back in time.

func (*Client) GetRecentlyPlayedTracksBefore

func (c *Client) GetRecentlyPlayedTracksBefore(before string) (PlayHistoryCursorBasedPaging, error)

The endpoint uses a bidirectional cursor for paging. Follow the next field with the before parameter to move back in time, or use the after parameter to move forward in time. If you supply no before or after parameter, the endpoint will return the most recently played songs, and the next link will page back in time.

func (*Client) GetUserProfile

func (c *Client) GetUserProfile(userId string) (User, error)

func (*Client) Pause

func (c *Client) Pause() error

Pause a User's Playback

func (*Client) Play

func (c *Client) Play() error

Start/Resume a User's Playback

func (*Client) PlayTrack

func (c *Client) PlayTrack(track Track) error

Start/Resume a User's Playback

func (*Client) PlayTracks

func (c *Client) PlayTracks(tracks []Track) error

Start/Resume a User's Playback

func (*Client) RefreshAccessToken

func (c *Client) RefreshAccessToken() error

func (*Client) Search

func (c *Client) Search(options SearchOptions) (SearchResult, error)

Search for an item

type Context

type Context struct {
	Type         string            `json:"type"`
	Href         string            `json:"href"`
	ExternalUrls map[string]string `json:"external_urls"`
	Uri          string            `json:"uri"`
}
type Copyright struct {
	Text string `json:"text"`
	Type string `json:"type"`
}

type CurrentlyPlayingContext

type CurrentlyPlayingContext struct {
	Device               Device  `json:"device"`
	RepeatState          string  `json:"repeat_state"`
	ShuffleState         bool    `json:"shuffle_state"`
	Context              Context `json:"context"`
	Timestamp            int     `json:"timestamp"`
	ProgressMs           int     `json:"progress_ms"`
	IsPlaying            bool    `json:"is_playing"`
	Item                 Track   `json:"item"`
	CurrentlyPlayingType string  `json:"currently_playing_type"`
	Actions              Actions `json:"actions"`
}

type Cursor

type Cursor struct {
	After string `json:"after"`
}

type CursorBasedPaging

type CursorBasedPaging struct {
	Href    string      `json:"href"`
	Items   interface{} `json:"items"`
	Limit   int         `json:"limit"`
	Next    string      `json:"next"`
	Cursors Cursor      `json:"cursors"`
	Total   int         `json:"total"`
}

type Device

type Device struct {
	Id               string `json:"id"`
	IsActive         bool   `json:"is_active"`
	IsPrivateSession bool   `json:"is_private_session"`
	IsRestricted     bool   `json:"is_restricted"`
	Name             bool   `json:"name"`
	Type             string `json:"type"`
	VolumePercent    int    `json:"volume_percent"`
}

type DevicesPayload

type DevicesPayload struct {
	Devices []Device `json:"devices"`
}

type Disallows

type Disallows map[string]string

type Error

type Error struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
}

type ExternalIds

type ExternalIds map[string]string

type ExternalUrls

type ExternalUrls map[string]string

type Followers

type Followers struct {
	Href  string `json:"href"`
	Total int    `json:"total"`
}

type Image

type Image struct {
	Height int    `json:"height"`
	Url    string `json:"url"`
	Width  int    `json:"width"`
}

type Paging

type Paging struct {
	Href     string      `json:"href"`
	Items    interface{} `json:"items"`
	Limit    int         `json:"limit"`
	Next     string      `json:"next"`
	Offset   int         `json:"offset"`
	Previous string      `json:"previous"`
	Total    int         `json:"total"`
}

type PlayHistory

type PlayHistory struct {
	Track    TrackSimple `json:"track"`
	PlayedAt string      `json:"played_at"`
	Context  Context     `json:"context"`
}

type PlayHistoryCursorBasedPaging

type PlayHistoryCursorBasedPaging struct {
	Href    string      `json:"href"`
	Items   PlayHistory `json:"items"`
	Limit   int         `json:"limit"`
	Next    string      `json:"next"`
	Cursors Cursor      `json:"cursors"`
	Total   int         `json:"total"`
}

type PlayerError

type PlayerError struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
	Reason  string `json:"reason"`
}

type Playlist

type Playlist struct {
	Collaborative bool              `json:"collaborative"`
	Descriptions  string            `json:"descriptions"`
	ExternalUrls  map[string]string `json:"external_urls"`
	Followers     Followers         `json:"followers"`
	Href          string            `json:"href"`
	Id            string            `json:"id"`
	Images        []Image           `json:"images"`
	Name          string            `json:"name"`
	Owner         User              `json:"owner"`
	Public        bool              `json:"public"`
	SnapshotId    string            `json:"snapshot_id"`
	Tracks        Paging            `json:"tracks"`
	Type          string            `json:"type"`
	Uri           string            `json:"uri"`
}

type PlaylistPaging

type PlaylistPaging struct {
	Href     string           `json:"href"`
	Items    []PlaylistSimple `json:"items"`
	Limit    int              `json:"limit"`
	Next     string           `json:"next"`
	Offset   int              `json:"offset"`
	Previous string           `json:"previous"`
	Total    int              `json:"total"`
}

type PlaylistSimple

type PlaylistSimple struct {
	Collaborative bool              `json:"collaborative"`
	Descriptions  string            `json:"descriptions"`
	ExternalUrls  map[string]string `json:"external_urls"`
	Href          string            `json:"href"`
	Id            string            `json:"id"`
	Images        []Image           `json:"images"`
	Name          string            `json:"name"`
	Owner         User              `json:"owner"`
	Public        bool              `json:"public"`
	SnapshotId    string            `json:"snapshot_id"`
	Tracks        Paging            `json:"tracks"`
	Type          string            `json:"type"`
	Uri           string            `json:"uri"`
}

type PlaylistTrack

type PlaylistTrack struct {
	AddedAt Timestamp `json:"added_at"`
	AddedBy User      `json:"added_by"`
	IsLocal bool      `json:"is_local"`
	Track   Track     `json:"track"`
}

type RecommendationSeed

type RecommendationSeed struct {
	AfterFilteringSize int    `json:"after_filtering_size"`
	AfterRelinkingSize int    `json:"after_relinking_size"`
	Href               string `json:"href"`
	Id                 string `json:"id"`
	InitialPoolSize    int    `json:"initial_pool_size"`
	Type               string `json:"type"`
}

type Recommendations

type Recommendations struct {
	Seeds  []RecommendationSeed `json:"seeds"`
	Tracks []TrackSimple        `json:"tracks"`
}

type Restrictions

type Restrictions interface{}

type SavedAlbum

type SavedAlbum struct {
	AddedAt Timestamp `json:"added_at"`
	Album   Album     `json:"album"`
}

type SavedTrack

type SavedTrack struct {
	AddedAt Timestamp `json:"added_at"`
	Track   Track     `json:"track"`
}

type SearchOptions

type SearchOptions struct {
	// Required.
	// Search query keywords and optional field filters and operators.
	// Example: "One Dance Drake"
	Query string `url:"q"`

	// Required.
	// A comma-separated list of item types to search across.
	// Valid types are: album, artist, playlist, and track.
	// Example: "album,track" returns both albums and tracks.
	Type string `url:"type"`

	// Optional.
	// An ISO 3166-1 alpha-2 country code or the string from_token.
	// If a country code is specified, only artists, albums, and tracks with content that is playable in that market is returned.
	Market string `url:"market,omitempty"`

	// Optional.
	// Maximum number of results to return.
	// Default: 20
	// Minimum: 1
	// Maximum: 50
	// The limit is applied within each type, not on the total response.
	// For example, if the limit value is 3 and the type is artist, album, the response contains 3 artists and 3 albums.
	Limit int `url:"limit,omitempty"`

	// Optional.
	// The index of the first result to return.
	// Default: 0 (the first result).
	// Maximum offset (including limit): 10,000.
	// Use with limit to get the next page of search results.
	Offset int `url:"offset,omitempty"`

	// Optional.
	// Possible values: audio
	// If include_external=audio is specified the response will include any relevant audio content that is hosted externally.
	// By default external content is filtered out from responses.
	IncludeExternal string `url:"include_external,omitempty"`
}

type SearchResult

type SearchResult struct {
	Tracks    TrackPaging    `json:"tracks"`
	Albums    AlbumPaging    `json:"albums"`
	Artists   ArtistPaging   `json:"artists"`
	Playlists PlaylistPaging `json:"playlists"`
}

Data structure for a response from a search

type State

type State struct {
	CurrentTrack Track `json:"currentTrack"`
	Position     int   `json:"position"`
	Duration     int   `json:"duration"`
	Paused       bool  `json:"paused"`
}

type Timestamp

type Timestamp string

type Track

type Track struct {
	Album            AlbumSimple       `json:"album"`
	Artists          []ArtistSimple    `json:"artists"`
	AvailableMarkets []string          `json:"available_markets"`
	DiscNumber       int               `json:"disc_number"`
	DurationMs       int               `json:"duration_ms"`
	Explicit         bool              `json:"explicit"`
	ExternalIds      map[string]string `json:"external_ids"`
	Href             string            `json:"href"`
	Id               string            `json:"id"`
	IsPlayable       bool              `json:"is_playable"`
	LinkedFrom       TrackLink         `json:"linked_from"`
	Restrictions     Restrictions      `json:"restrictions"`
	Name             string            `json:"name"`
	Popularity       int               `json:"popularity"`
	PreviewUrl       string            `json:"preview_url"`
	TrackNumber      int               `json:"track_number"`
	Type             string            `json:"type"`
	Uri              string            `json:"uri"`
	IsLocal          bool              `json:"is_local"`
}
type TrackLink struct {
	ExternalUrls map[string]string `json:"external_urls"`
	Href         string            `json:"href"`
	Id           string            `json:"id"`
	Type         string            `json:"type"`
	Uri          string            `json:"uri"`
}

type TrackPaging

type TrackPaging struct {
	Href     string  `json:"href"`
	Items    []Track `json:"items"`
	Limit    int     `json:"limit"`
	Next     string  `json:"next"`
	Offset   int     `json:"offset"`
	Previous string  `json:"previous"`
	Total    int     `json:"total"`
}

type TrackSimple

type TrackSimple struct {
	Artists          []ArtistSimple    `json:"artists"`
	AvailableMarkets []string          `json:"available_markets"`
	DiscNumber       int               `json:"disc_number"`
	DurationMs       int               `json:"duration_ms"`
	Explicit         bool              `json:"explicit"`
	ExternalIds      map[string]string `json:"external_ids"`
	Href             string            `json:"href"`
	Id               string            `json:"id"`
	IsPlayable       bool              `json:"is_playable"`
	LinkedFrom       TrackLink         `json:"linked_from"`
	Restrictions     Restrictions      `json:"restrictions"`
	Name             string            `json:"name"`
	PreviewUrl       string            `json:"preview_url"`
	TrackNumber      int               `json:"track_number"`
	Type             string            `json:"type"`
	Uri              string            `json:"uri"`
	IsLocal          bool              `json:"is_local"`
}

type User

type User struct {
	DisplayName  string            `json:"display_name"`
	ExternalUrls map[string]string `json:"external_urls"`
	Followers    Followers         `json:"followers"`
	Href         string            `json:"href"`
	Id           string            `json:"id"`
	Images       []Image           `json:"images"`
	Type         string            `json:"type"`
	Uri          string            `json:"uri"`
}

type UserPrivate

type UserPrivate struct {
	Country      string            `json:"country"`
	DisplayName  string            `json:"display_name"`
	Email        string            `json:"email"`
	ExternalUrls map[string]string `json:"external_urls"`
	Followers    Followers         `json:"followers"`
	Href         string            `json:"href"`
	Id           string            `json:"id"`
	Images       []Image           `json:"images"`
	Product      string            `json:"product"`
	Type         string            `json:"type"`
	Uri          string            `json:"uri"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL