Skip to content

Instantly share code, notes, and snippets.

@rhnvrm
Created August 9, 2018 07:08
Show Gist options
  • Save rhnvrm/85ec6b6e3437006a5d07fee75feb6b80 to your computer and use it in GitHub Desktop.
Save rhnvrm/85ec6b6e3437006a5d07fee75feb6b80 to your computer and use it in GitHub Desktop.
Tag Spotify Top Tracks with Lyrics
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/rhnvrm/lyric-api-go"
"github.com/buger/jsonparser"
)
var (
token = ""
limit = 50
offset = 50
results []result
)
type result struct {
Artist string `json:"artist"`
Song string `json:"song"`
Lyrics string `json:"lyrics"`
}
func main() {
for i := 0; i < 10; i++ {
url := "https://api.spotify.com/v1/me/top/tracks?limit=" + strconv.Itoa(limit) + "&offset=" + strconv.Itoa(offset*i)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
for i := 0; i < limit; i++ {
song, err := jsonparser.GetString(bodyBytes, "items", "["+strconv.Itoa(i)+"]", "name")
artist, err := jsonparser.GetString(bodyBytes, "items", "["+strconv.Itoa(i)+"]", "artists", "[0]", "name")
if err == nil {
l := lyrics.New()
lyrics, err := l.Search(artist, song)
if err == nil {
results = append(results, result{
Song: song,
Artist: artist,
Lyrics: lyrics,
})
}
}
}
}
}
dataset, _ := json.Marshal(results)
fmt.Println(string(dataset))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment