class RJL::Itunes

RJL_itunes::Itunes contains the public api for Itunes. @example

require 'rjl/itunes'

itunes = RJL::Itunes.new
albums = itunes.albums

@api public

Attributes

albums[R]
version[R]

Public Class Methods

new( playlist = nil ) click to toggle source

Create a new RJL_itunes::Itunes. Can be called with a Playlist name to only get the tracks in that Playlist - useful for testing, and may be deprecated @param [String] playlist optional name of a Playlist to open

# File lib/rjl/itunes.rb, line 22
def initialize( playlist = nil )
  @tracks = get_tracks( playlist )
  @albums = get_albums( @tracks )
  @playlists = fetch_playlists( kind: :none )
  @playlist_folders = fetch_playlists( kind: :none )
end

Public Instance Methods

create_playlist( playlist_name: "Playlist", track_list: nil ) click to toggle source
# File lib/rjl/itunes.rb, line 79
def create_playlist( playlist_name: "Playlist", track_list: nil )
  playlist_obj = app("iTunes").make(
    :new => :playlist,
    :with_properties => {:name => playlist_name}
    )
  playlist = Playlist.new( playlist_obj )
  playlist.tracks = track_list unless track_list.nil?
  @playlists << playlist
  return playlist
end
create_playlist_folder( folder_name: "untitled folder", playlists: nil ) click to toggle source
# File lib/rjl/itunes.rb, line 95
def create_playlist_folder( folder_name: "untitled folder", playlists: nil )
  playlist_obj = app("iTunes").make(
    :new => :folder_playlist,
    :with_properties => {:name => folder_name}
    )
  # TODO add playlists
  # TODO make and add to @playlist_folders
end
destroy_playlist( playlist_name ) click to toggle source
# File lib/rjl/itunes.rb, line 90
def destroy_playlist( playlist_name )
  app("iTunes").sources["Library"].user_playlists[playlist_name].delete
  # TODO remove from @playlists
end
destroy_playlist_folder( folder_name: nil ) click to toggle source
# File lib/rjl/itunes.rb, line 104
def destroy_playlist_folder( folder_name: nil )
  app("iTunes").sources["Library"].folder_playlists[folder_name].delete
end
playlist( playlist_name ) click to toggle source

Get a playlist of the specified name. Raises error if more than one. @param playlist_name [String] The name of the playlist to get @return [Playlist] the named playlist

# File lib/rjl/itunes.rb, line 70
def playlist( playlist_name )
  play_list = @playlists.select { |playlist| playlist.name == playlist_name }
  if play_list.count == 1
    return play_list[0]
  else
    raise "ERROR: #{play_list.count} playlists with name #{playlist_name}"
  end
end
playlists() click to toggle source

Return a list of the playlists. @return [List of Playlist] the playlists

# File lib/rjl/itunes.rb, line 63
def playlists
  return @playlists
end

Private Instance Methods

fetch_playlists( kind: :none ) click to toggle source
# File lib/rjl/itunes.rb, line 168
def fetch_playlists( kind: :none )
  playlist_list = []
  reject_list = ["Music Videos", "Home Videos", "Books", "PDFs", "Audiobooks"]
  playlists = app("iTunes").user_playlists[its.special_kind.eq(:none)].get
  playlists.each do |playlist_obj|
    playlist_list << Playlist.new( playlist_obj ) unless reject_list.include? (playlist_obj.name.get)
  end
  return playlist_list
end
get_albums( tracks ) click to toggle source

Get the albums Three possibilities:

1. one album, one artist e.g. "Spiceworld, Spice Girls"
2. one album, multiple artists, not compilation  e.g. "Greatest Hits"
3. one album, multiple artists, compilation e.g. "The Firm"

@param [List of Track] the tracks @return [List of Album] the albums

# File lib/rjl/itunes.rb, line 138
def get_albums( tracks )

  album_list = []

  # build hash of tracks with album name as key
  album_hash = Hash.new { |h,k| h[k] = [] }
  tracks.each do |track|
    album_hash[track.album] << track
  end

  # split the album tracks by track artist
  album_hash.each do |album_name, album_tracks|
    artist_hash = Hash.new { |h,k| h[k] = [] }
    album_tracks.each do |album_track|
      artist_hash[album_track.artist] << album_track
    end

    if album_tracks[0].compilation?
      album_list << Album.new( tracks: album_tracks)
    else
      artist_hash.each do |artist, tracks|
        album_list << Album.new( tracks: tracks)
      end
    end

  end

  return album_list
end
get_tracks( playlist = nil) click to toggle source

Get the tracks @param [String] playlist the name of a playlist to get tracks from @return [List of Track] the tracks

# File lib/rjl/itunes.rb, line 113
def get_tracks( playlist = nil)

  track_list = []

  if playlist.nil?
    tracks = app("iTunes").playlists[1].tracks[its.video_kind.eq(:none)].get
  else
    tracks = app("iTunes").playlists[playlist].tracks[its.video_kind.eq(:none)].get
  end


  tracks.each do |track_obj|
    # puts ">>> #{track_obj.class_.get}"
    track_list << Track.new(track_obj)
  end
  return track_list
end
make_key( artist, album ) click to toggle source
# File lib/rjl/itunes.rb, line 178
def make_key( artist, album )
  return "#{artist}__#{album}"
end