Skip to content

Commit

Permalink
Increase playlists to be loaded from 20 to 50
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiofalci committed Jan 15, 2017
1 parent f40e582 commit 978c274
Showing 1 changed file with 74 additions and 58 deletions.
132 changes: 74 additions & 58 deletions spotify/spotify_playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,18 @@ import (
"fmt"
)


func (spotify *Spotify) initPlaylist() error {
playlists := sconsify.InitPlaylists()

if spotify.client != nil {
if privateUser, err := spotify.client.CurrentUser(); err == nil {
offset := 0
total := 1
for offset <= total {
offset, total = spotify.loadPlaylists(offset, privateUser, playlists)
if offset > total {
fmt.Printf("Loaded %v from %v playlists\n", total, total)
} else {
fmt.Printf("Loaded %v from %v playlists\n", offset, total)
}
}
} else {
panic(err)
if err := spotify.initWebApiPlaylist(playlists); err != nil {
return err
}
} else {
fmt.Print("Warning: not using -web-api flag. Sconsify will load playlists using deprecated libspotify API. If not working try -web-api flag.\n")
allPlaylists, err := spotify.session.Playlists()
if err != nil {
if err := spotify.initLibspotifyPlaylist(playlists); err != nil {
return err
}
allPlaylists.Wait()
var folderPlaylists []*sconsify.Playlist
var folder *sp.PlaylistFolder
infrastructure.Debugf("# of playlists %v", allPlaylists.Playlists())
for i := 0; i < allPlaylists.Playlists(); i++ {
if allPlaylists.PlaylistType(i) == sp.PlaylistTypeStartFolder {
folder, _ = allPlaylists.Folder(i)
folderPlaylists = make([]*sconsify.Playlist, 0)
infrastructure.Debugf("Opening folder '%v' (%v)", folder.Id(), folder.Name())
} else if allPlaylists.PlaylistType(i) == sp.PlaylistTypeEndFolder {
if folder != nil {
playlists.AddPlaylist(sconsify.InitFolder(strconv.Itoa(int(folder.Id())), folder.Name(), folderPlaylists))
infrastructure.Debugf("Closing folder '%v' (%v)", folder.Id(), folder.Name())
} else {
infrastructure.Debug("Closing a null folder, this doesn't look right ")
}
folderPlaylists = nil
folder = nil
}

if allPlaylists.PlaylistType(i) != sp.PlaylistTypePlaylist {
continue
}

playlist := allPlaylists.Playlist(i)
playlist.Wait()
if spotify.canAddPlaylist(playlist, allPlaylists.PlaylistType(i)) {
id := playlist.Link().String()
infrastructure.Debugf("Playlist '%v' (%v)", id, playlist.Name())
tracks := make([]*sconsify.Track, playlist.Tracks())
infrastructure.Debugf("\t# of tracks %v", playlist.Tracks())
for i := 0; i < playlist.Tracks(); i++ {
tracks[i] = spotify.initTrack(playlist.Track(i))
}
if folderPlaylists == nil {
playlists.AddPlaylist(sconsify.InitPlaylist(id, playlist.Name(), tracks))
} else {
folderPlaylists = append(folderPlaylists, sconsify.InitSubPlaylist(id, playlist.Name(), tracks))
}
}
}
}

webApiCache := spotify.loadWebApiCacheIfNecessary()
Expand Down Expand Up @@ -120,8 +67,77 @@ func (spotify *Spotify) initPlaylist() error {
return nil
}

func (spotify *Spotify) initWebApiPlaylist(playlists *sconsify.Playlists) error {
if privateUser, err := spotify.client.CurrentUser(); err == nil {
offset := 0
total := 1
for offset <= total {
offset, total = spotify.loadPlaylists(offset, privateUser, playlists)
if offset > total {
fmt.Printf("Loaded %v from %v playlists\n", total, total)
} else {
fmt.Printf("Loaded %v from %v playlists\n", offset, total)
}
}
} else {
return err
}

return nil
}

func (spotify *Spotify) initLibspotifyPlaylist(playlists *sconsify.Playlists) error {
fmt.Print("Warning: not using -web-api flag. Sconsify will load playlists using deprecated libspotify API. If not working try -web-api flag.\n")
allPlaylists, err := spotify.session.Playlists()
if err != nil {
return err
}
allPlaylists.Wait()
var folderPlaylists []*sconsify.Playlist
var folder *sp.PlaylistFolder
infrastructure.Debugf("# of playlists %v", allPlaylists.Playlists())
for i := 0; i < allPlaylists.Playlists(); i++ {
if allPlaylists.PlaylistType(i) == sp.PlaylistTypeStartFolder {
folder, _ = allPlaylists.Folder(i)
folderPlaylists = make([]*sconsify.Playlist, 0)
infrastructure.Debugf("Opening folder '%v' (%v)", folder.Id(), folder.Name())
} else if allPlaylists.PlaylistType(i) == sp.PlaylistTypeEndFolder {
if folder != nil {
playlists.AddPlaylist(sconsify.InitFolder(strconv.Itoa(int(folder.Id())), folder.Name(), folderPlaylists))
infrastructure.Debugf("Closing folder '%v' (%v)", folder.Id(), folder.Name())
} else {
infrastructure.Debug("Closing a null folder, this doesn't look right ")
}
folderPlaylists = nil
folder = nil
}

if allPlaylists.PlaylistType(i) != sp.PlaylistTypePlaylist {
continue
}

playlist := allPlaylists.Playlist(i)
playlist.Wait()
if spotify.canAddPlaylist(playlist, allPlaylists.PlaylistType(i)) {
id := playlist.Link().String()
infrastructure.Debugf("Playlist '%v' (%v)", id, playlist.Name())
tracks := make([]*sconsify.Track, playlist.Tracks())
infrastructure.Debugf("\t# of tracks %v", playlist.Tracks())
for i := 0; i < playlist.Tracks(); i++ {
tracks[i] = spotify.initTrack(playlist.Track(i))
}
if folderPlaylists == nil {
playlists.AddPlaylist(sconsify.InitPlaylist(id, playlist.Name(), tracks))
} else {
folderPlaylists = append(folderPlaylists, sconsify.InitSubPlaylist(id, playlist.Name(), tracks))
}
}
}
return nil
}

func (spotify *Spotify) loadPlaylists(offset int, privateUser *webspotify.PrivateUser, playlists *sconsify.Playlists) (int, int) {
limit := 20
limit := 50
options := &webspotify.Options{Limit: &limit, Offset: &offset}
if simplePlaylistPage, err := spotify.client.GetPlaylistsForUserOpt(privateUser.ID, options); err == nil {
for _, webPlaylist := range simplePlaylistPage.Playlists {
Expand Down

0 comments on commit 978c274

Please sign in to comment.