require 'find' module MP3Manager VERSION = '1.0.0' SIXTY_DAYS_IN_SECONDS = 5184000 def MP3Manager.create_m3u_playlist(m3u_file, folder = '.', new_files_only = false, relative = true, verbose = true) if m3u_file.kind_of?(String) if File::exists?(m3u_file) puts "#{m3u_file} exists.. overwriting" #prompt = STDIN::gets #if not(prompt.index(%r{^y}i)) : puts "Exiting.."; return; end end # open a new file object as m3u_file - we will pass this down the recursion chain, allowing us to write to this file from any folder if verbose : puts "Using playlist file #{m3u_file}"; end m3u_file = File::open(m3u_file, 'w+') end folder = File::join(folder) m3u_filename = m3u_file.path # normalize path with to unix style m3u_filename.gsub!('\\', '/') m3u_file_contents = Array.new # iterate through all files and subfolders Find.find(folder) do |file| # normalize path to unix style file.gsub!('\\', '/') case when File::file?(file) # if we are only interested in new files, then skip this file if it's datetime is older than 60 days if new_files_only if File::mtime(file) < (Time::now - SIXTY_DAYS_IN_SECONDS) : next; end end # make path relative for current file, if appropriate if relative if File::dirname(m3u_filename) != '.' file.gsub!(%r{^#{File::dirname(m3u_filename)}}i, "") end file.gsub!(%r{^/}, "") end # only process mp3 files if File::extname(file) == '.mp3' m3u_file_contents << file end when File::directory?(file) if verbose : puts "Processing folder #{file}"; end end end #sort using case independent method sorted_contents = m3u_file_contents.sort_by {|w| w.downcase} m3u_file.puts(sorted_contents.uniq.join("\n")) end end if !ARGV[0] || !ARGV[1] || ARGV[0].index(%r{help$|\?$}) puts "Usage: m3umaker.rb (m3u file to create) (folder to process) /new" else if ARGV[2] && ARGV[2].downcase =~ /new$/i : new_files_only = true; end MP3Manager::create_m3u_playlist(ARGV[0], ARGV[1], new_files_only) end