Esempio n. 1
0
// ReadSpotifyPlaylist reads spotify playlist into list of tracks
func ReadSpotifyPlaylist(spotifyClient *spotify.Client, uri string) (tracks []Track, playlistName string, err error) {

	// Fetch playlist
	playlist, err := spotifyClient.GetPlaylist(ParseSpotifyUri(uri))
	if err != nil {
		return
	}
	playlistName = playlist.Name

	// Convert to tracks
	tracks = make([]Track, len(playlist.Tracks.Tracks))
	for i, x := range playlist.Tracks.Tracks {

		// Artists
		artistStrings := make([]string, len(x.Track.Artists))
		for k, x := range x.Track.Artists {
			artistStrings[k] = x.Name
		}
		if artistStrings == nil {
			err = errors.New("empty artist in track")
			return
		}

		// Store in slice
		tracks[i] = Track{
			x.Track.Name,
			x.Track.Album.Name,
			artistStrings[0], // All tracks has at least one artist
			artistStrings,
		}
	}
	return
}
Esempio n. 2
0
File: main.go Progetto: brandur/wgt2
func addTracksToPlaylist(client *spotify.Client, user *spotify.PrivateUser, playlist *spotify.SimplePlaylist, trackIDs []spotify.ID) error {
	allTracks := make(map[spotify.ID]spotify.FullTrack)
	limit := 100
	offset := 0

	var options spotify.Options
	options.Limit = &limit
	options.Offset = &offset

	for {
		tracksPage, err := client.GetPlaylistTracksOpt(user.ID, playlist.ID, &options, "")
		if err != nil {
			return err
		}

		log.Printf("Playlist %v: fetched page of %v track(s)", playlist.Name, len(tracksPage.Tracks))

		for _, playlistTrack := range tracksPage.Tracks {
			track := playlistTrack.Track
			allTracks[track.ID] = track
		}

		// The Spotify API always returns full pages unless it has none left to
		// return.
		if len(tracksPage.Tracks) < 100 {
			break
		}

		offset = offset + len(tracksPage.Tracks)
	}

	log.Printf("Playlist %v: %v track(s)", playlist.Name, len(allTracks))

	var trackIDsToAdd []spotify.ID
	for _, id := range trackIDs {
		if _, ok := allTracks[id]; !ok {
			trackIDsToAdd = append(trackIDs, id)
		}
	}

	log.Printf("Playlist %v: Need to add %v track(s)", playlist.Name,
		len(trackIDsToAdd))

	for i := 0; i < len(trackIDsToAdd); i += 100 {
		bound := i + 100
		if bound > len(trackIDsToAdd) {
			bound = len(trackIDsToAdd)
		}

		_, err := client.AddTracksToPlaylist(user.ID, playlist.ID, trackIDsToAdd[i:bound]...)
		if err != nil {
			return err
		}
		log.Printf("Playlist %v: added page of %v track(s)", playlist.Name,
			len(trackIDsToAdd[i:bound]))
	}

	return nil
}
Esempio n. 3
0
func GetTrax(c *spotify.Client, id spotify.ID) ([]spotify.ID, error) {
	discTrax, err := c.GetPlaylistTracksOpt("spotifydiscover", id, nil, "items(track(id, name))")
	if err != nil {
		return nil, errors.New("getplay err: " + err.Error())
	}

	ids := make([]spotify.ID, 0, len(discTrax.Tracks))
	for _, t := range discTrax.Tracks {
		fmt.Printf("ID: %v, Name: %v\n", t.Track.ID, t.Track.Name)
		ids = append(ids, spotify.ID(t.Track.ID))
	}

	return ids, nil
}
Esempio n. 4
0
File: main.go Progetto: brandur/wgt2
func getPlaylist(client *spotify.Client, user *spotify.PrivateUser, playlistName string) (*spotify.SimplePlaylist, error) {
	page, err := client.CurrentUsersPlaylists()
	if err != nil {
		return nil, err
	}

	for _, playlist := range page.Playlists {
		if playlist.Name == playlistName {
			log.Printf("Found playlist: %v", playlistName)
			return &playlist, nil
		}
	}

	// otherwise create a new playlist for the user
	log.Printf("Creating playlist: %v", playlistName)

	playlist, err := client.CreatePlaylistForUser(user.ID, playlistName, true)
	return &playlist.SimplePlaylist, err
}
Esempio n. 5
0
func MakePlist(c *spotify.Client, id spotify.ID) error {
	u, err := c.CurrentUser()
	if err != nil {
		return errors.New("curuser err: " + err.Error())
	}

	uid := u.ID

	pl, err := c.CreatePlaylistForUser(uid, NewPlistName(), false)
	if err != nil {
		return errors.New("create error: " + err.Error())
	}

	ids, err := GetTrax(c, id)
	if err != nil {
		return errors.New("gettrax err: " + err.Error())
	}

	snap, err := c.AddTracksToPlaylist(u.ID, pl.ID, ids...)
	if err != nil {
		return errors.New("addtrax err: " + err.Error())
	}
	fmt.Println(snap)

	return nil
}