Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 8036b0d5a6b6d06067e765333cb586e17b6b5a2b 0 parents
Blixt authored
Showing with 156 additions and 0 deletions.
  1. +40 −0 README.md
  2. +116 −0 spotify/spotify.go
40 README.md
@@ -0,0 +1,40 @@
+# Go Spotify
+
+This is a simple module to communicate with the Spotify API.
+
+**Note:** This is library is currently very simple and only supports a few
+requests.
+
+## Installation
+
+ go get github.com/blixt/go-spotify/spotify
+
+## Examples
+
+### Perform parallel search queries
+
+ package main
+
+ import (
+ "fmt"
+ "github.com/blixt/go-spotify/spotify"
+ )
+
+ func main() {
+ api := spotify.GetApi()
+
+ queries := make(chan *spotify.SearchTrackQuery)
+
+ // Make two search queries in parallel.
+ go api.SearchTrack("never gonna give you up", queries)
+ go api.SearchTrack("trololo", queries)
+
+ // Print the first track of each query result.
+ for i := 0; i < 2; i++ {
+ query := <-queries
+ fmt.Printf("Got %d results for \"%s\". First result:\n", query.Info.NumResults, query.Query)
+
+ track := query.Tracks[0]
+ fmt.Println(track.Href, track.Name, "by", track.Artists[0].Name)
+ }
+ }
116 spotify/spotify.go
@@ -0,0 +1,116 @@
+package spotify
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+)
+
+type Album struct {
+ Name string
+ Href string
+ Released string
+ Availability interface{}
+}
+
+type Api struct {
+}
+
+type Artist struct {
+ Name string
+ Href string
+}
+
+type ExternalId struct {
+ Type string
+ Id string
+}
+
+type Info struct {
+ NumResults int `json:"num_results"`
+ Limit int
+ Offset int
+ Query string
+ Type string
+ Page int
+}
+
+type Track struct {
+ Name string
+ Href string
+ Artists []Artist
+ Album Album
+ TrackNumber string `json:"track-number"`
+ Length float32
+ Popularity string
+ ExternalIds []ExternalId `json:"external-ids"`
+}
+
+type query interface {
+ GetCallInfo() (path string, params map[string][]string)
+ setError(error)
+}
+
+type queryBase struct {
+ Error error
+}
+
+func (query *queryBase) setError(err error) {
+ query.Error = err
+}
+
+type SearchTrackQuery struct {
+ queryBase
+ Query string
+ Info Info
+ Tracks []Track
+}
+
+func (query *SearchTrackQuery) GetCallInfo() (path string, params map[string][]string) {
+ path = "/search/1/track.json"
+ params = map[string][]string{
+ "q": {query.Query}}
+ return
+}
+
+const (
+ BASE_URL = "http://ws.spotify.com"
+)
+
+func GetApi() *Api {
+ return &Api{}
+}
+
+func (api *Api) call(query query) {
+ path, params := query.GetCallInfo()
+
+ v := url.Values{}
+ for key, values := range params {
+ for _, value := range values {
+ v.Add(key, value)
+ }
+ }
+
+ resp, err := http.Get(fmt.Sprintf("%s%s?%s", BASE_URL, path, v.Encode()))
+ if err != nil {
+ query.setError(err)
+ return
+ }
+ defer resp.Body.Close()
+
+ if data, err := ioutil.ReadAll(resp.Body); err != nil {
+ query.setError(err)
+ } else {
+ json.Unmarshal(data, &query)
+ }
+}
+
+func (api *Api) SearchTrack(q string, out chan *SearchTrackQuery) {
+ query := &SearchTrackQuery{
+ Query: q}
+
+ api.call(query)
+ out <- query
+}
Please sign in to comment.
Something went wrong with that request. Please try again.