Moving a Playlist into a Folder?

4 views
Skip to first unread message

lawrencealan

unread,
Oct 2, 2012, 1:09:29 AM10/2/12
to ruby-...@googlegroups.com
I'm successfully importing my MUSE favorites (see http://muse.lawrencealan.com/favorites/lawrencealan
They have been associated with Spotify URIs into playlists, I just need to figure out how to move the playlists into a Folder (as to not dirty up the rest of my library) using Hallon...

Here's my code (folders is a big object -- { { '2012': { 'September': { '2012-09-24': [ {etc}, {etc} ] } } } )

    puts "Creating MUSE folder..."
    muselists
= container.add_folder "__MUSEPLAYLISTS"
   folders.each do |year_label, year|
      year
.each do |month_label, month|
            month
.each do |day_label, day|
              day_list
= container.add day_label, force: true
              htracks = day.map { |x| Hallon::Track.new(x['spotify_uri'])
               day_list
.insert(0,htracks)
               muselists
.contents.push day_list
               day_list
.upload
         
end
       end
   end


As you can see I'm attempting to push each day_list (Playlist) into muselists.contents (Folder)

Kim Burgestrand

unread,
Oct 2, 2012, 2:39:02 AM10/2/12
to ruby-...@googlegroups.com
It’s a little weird, and will be changed after I’ve made some non-significant changes to the Spotify gem I’ve been working on the past few weeks. However, here’s the deal.

Folders consist of a starting marker, placed at index Folder#begin, and an ending marker, placed at index Folder#end. Anything between these two indices are considered to be contained within the folder. You can move any of these markers independently, which would resize the folder accordingly.

To illustrate what happens, consider an initial playlist container containing playlists and folders already:
  1. Great playlist for whatever
  2. Playlists shared with me
    1.   Discofever
    2.   Punk master
  3. Songs I used to like
Now, you add your folder to contain your playlists.
  1. Great playlist for whatever
  2. (start) Playlists shared with me
  3. Discofever
  4. Punk master
  5. (stop) Playlists shared with me
  6. Songs I used to like
  7. (start) __MUSEPLAYLISTS
  8. (stop) __MUSEPLAYLISTS
As you add new playlists, they are added to the end of the container. Say that we add two dates.
  1. Great playlist for whatever
  2. (start) Playlists shared with me
  3. Discofever
  4. Punk master
  5. (stop) Playlists shared with me
  6. Songs I used to like
  7. (start) __MUSEPLAYLISTS
  8. (stop) __MUSEPLAYLISTS
  9. 2012-09-24
  10. 2012-09-29
Now, we want to move these two playlists into the folder. Now, given that the new folder is added to the end of the container, and playlists are added after that, all we need to do to move the playlists into that folder is to move the ending marker of the folder to the end of the container: container.move(muselists.end, container.size - 1).
  1. Great playlist for whatever
  2. (start) Playlists shared with me
  3. Discofever
  4. Punk master
  5. (stop) Playlists shared with me
  6. Songs I used to like
  7. (start) __MUSEPLAYLISTS
  8. 2012-09-24
  9. 2012-09-29
  10. (stop) __MUSEPLAYLISTS
Naturally, you could also move each playlist individually. Unfortunately the folders do not have many convenience methods for writing (which would be useful to have). You could do this by moving a playlist to inside a folder, but it’d be a bit trickier. Moving the playlist from after the folder, to inside it, would mean moving the playlist to the index of the folder’s beginning index. However, if the folder is placed before the folder would mean moving the playlist to the index of the folder’s ending index.

lawrencealan

unread,
Oct 2, 2012, 2:43:33 AM10/2/12
to ruby-...@googlegroups.com

I almost figured it out since posting but was grasping for clues on where those indexes were being stored. I'm sure your explanation will be helpful to others as well as myself :)

lawrencealan

unread,
Oct 2, 2012, 10:36:11 PM10/2/12
to ruby-...@googlegroups.com
I've posted a generic playlist, folder creation and placement Ruby script for others to look at:

#!/usr/bin/env ruby
# encoding: utf-8

require 'hallon'

## CONFIG
@spotify_user = "facebo...@gmail.com"
@spotify_password = "PASSWORD_HERE"

# PREDEFINED PLAYLIST OBJECT
@playlists_object = {
    "Playlist 1" => [
    ],
    "Playlist 2" => [
    ]
}

## END CONFIG


## Load and wait looper
def loadandwait(obj)
  obj.load(5)
rescue Interrupt
  puts "Interrupted!"
  exit
rescue Exception => e
  puts e.message
  retry
end


begin
## Initialize Spotify Session
  session = Hallon::Session.initialize IO.read('./spotify_appkey.key'), {:load_playlists => true }
  session.login! @spotify_user, @spotify_password

  ## Get User Container
  print "Getting Spotify container for #{@spotify_user}..."
  container = session.container
  loadandwait(container)
  puts "Done."

  ## Create Folder at position / index zero
  playlist_folder = container.insert_folder 0, "Hallon Playlists"

  ## Create Playlists
  puts "Creating 'Hallon Playlists' folder in Spotify..."
  @playlists_object.reverse_each do |playlist_label, tracks|

    ## Create Playlist
    playlist = container.add "#{playlist_label}", true

    ## Playlist will be at the last index of the container, move to index 1 -- this pushes playlist_folder.end down
    container.move(container.size-1, 1)

    ## Create Hallon::Track for each spotify URL, insert into playlist
    playlist.insert(0, tracks.map { |x| Hallon::Track.new(x) })
    if playlist.size > 0 then
      ## Upload playlist
      print "\tUploading playlist: #{playlist_label}..."
      begin
        print '.'
        playlist.upload(30)
        STDOUT.flush
      rescue Exception => e
        retry
      end
      puts "Done!"
    else
      ## Remove playlist from container
      container.remove(1)
    end
  end
rescue Exception => e
  puts e.message
  puts e.backtrace
rescue Interrupt
  puts "Interrupted!"
  exit
end


Kim Burgestrand

unread,
Oct 5, 2012, 3:10:37 AM10/5/12
to ruby-...@googlegroups.com
Sweet! Nice of you to publish it.
Reply all
Reply to author
Forward
0 new messages