Active Topics

 


Reply
Thread Tools
brontide's Avatar
Posts: 868 | Thanked: 474 times | Joined on Oct 2007 @ Capital District, NY, USA
#1
This is one of the items that I hacked together and though would be useful to the community. This works under Leopard or above ( needs the scripting hooks ). It will create a directory with .m3u's for playlists, 2 level deep directories with symbolic links for files that can be rsync-ed to the nokia. Canola recognized the m3u's and seems to work well, except random non-DRM'ed aac files do not work.

Code:
#
# Export playlist folder for a non iPod music player
#

require 'osx/cocoa'
require 'FileUtils'

include OSX
OSX.require_framework 'ScriptingBridge'

 
def directoryFor(file)
	file.split('/')[1..-2].join('/')	
end

def processTrack(source_path, rel_path)
	dir = directoryFor('%s/%s' % [ $DEST, rel_path])
	FileUtils.makedirs dir if ! File.exists?(dir)
	FileUtils.ln_s source_path, '%s/%s' % [ $DEST, rel_path]
	art = directoryFor(source_path) + '/folder.jpg'
	if !$artwork[art] then
		$artwork[art] = ''
		artd = dir + '/folder.jpg'
		FileUtils.ln_s art, artd if File.exists?(art)
	end
end

def processPlaylist(playlist, prefix)
	mypls = $iTunes.sources[0].playlists.select do
		|p|
		# ScriptingBridge does not give us a clean way to prevent barfing on this next
		# statement so I had to wrap it to prevent the exception from halting the app
		begin
			p.get.parent.persistentID == playlist.persistentID
		rescue
		end
	end
	
	mypls.each do |pl|
		prefix_new = prefix.dup.push(pl.name)
		puts ' Playlist: %s' % [ prefix_new.join('-') ]
		fout = File.open('%s%s%s' % [ $DEST, prefix_new.join('-'), ".m3u" ], "w")
		pl.tracks.each do |track|
			path = $track_paths[track.databaseID]
			fout.puts '%s' % [path] if path
		end
		$pl_count += 1
		fout.close
		processPlaylist(pl,prefix_new)
	end
end

$iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")

$track_count = 0
$drm_count = 0
$pl_count = 0
$track_paths = Hash.new
$artwork = Hash.new

PLAYLIST = "Eric's Nokia"
SOURCE = '/Users/ericwarnke/Music/iTunes/iTunes Music/'
DEST = '/Users/ericwarnke/Music/MusicSync'

mylist = $iTunes.sources[0].playlists.filteredArrayUsingPredicate(NSPredicate.predicateWithFormat('name MATCHES %@', PLAYLIST))[0]

mylist.tracks.each do |track| 
  track = track.get
  source_path = track.location.path
  rel_path = 'Music/'+ source_path.dup.gsub!(SOURCE,'')
  $track_count+=1
  if (track.kind == 'Protected AAC audio file') then
    cache_path = '%d.mp3' % track.databaseID
    rel_path = rel_path.sub( /^(.*)\.([^.]+)$/ , "\\1.mp3")
    puts "%d **** DRM NEEDS CONVERSION ****\n %s\n %s" % [ track.databaseID, cache_path, rel_path ]
    $drm_count+=1
  else 
    puts "%d\n %s\n %s" % [ track.databaseID, source_path, rel_path ]
    #processTrack(source_path,rel_path)  
    $track_paths[track.databaseID] = rel_path
  end
end

processPlaylist(mylist,[])

puts 'Tracks: %d DRM: %d Playlists: %d' % [ $track_count, $drm_count, $pl_count ]
I didn't get around to doing the DRM fixup conversion ( can be done through scripting bridge, asking itunes to convert the song and then caching the stripped version ). You will need to fixup the paths for your own files. I made an attempt to add cover art, but Canola didn't recognize it properly.

Maybe someone can finish this up and turn it into a real application or extrapolate for some other use.
 

The Following 2 Users Say Thank You to brontide For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 02:28.