Permalink
Browse files

Seed script will read through db/seed/songs and create a favorite mix…

…tape with all valid songs. Songs are validated by presence and content type of audio
  • Loading branch information...
1 parent 0b7fac9 commit e5cde26e96725efcffcdb4e3b0ac79a618d52ac9 @aviflombaum committed Nov 14, 2011
Showing with 68 additions and 1 deletion.
  1. +2 −1 .gitignore
  2. +4 −0 Gemfile
  3. +2 −0 Gemfile.lock
  4. +5 −0 app/models/song.rb
  5. +55 −0 db/seeds.rb
View
@@ -3,4 +3,5 @@ db/*.sqlite3
log/*.log
tmp/
.sass-cache/
-public/system
+public/system
+db/seed
View
@@ -17,6 +17,10 @@ group :assets do
end
gem 'paperclip'
+
+gem 'ruby-mp3info', :require => 'mp3info'
+# gem 'taglib-ruby', :require => 'taglib'
+
gem 'jquery-rails'
group :development, :test do
View
@@ -113,6 +113,7 @@ GEM
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
+ ruby-mp3info (0.6.16)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
sass (3.1.10)
@@ -147,6 +148,7 @@ DEPENDENCIES
rails (= 3.1.1)
rspec-rails
ruby-debug19
+ ruby-mp3info
sass-rails (~> 3.1.4)
sqlite3
uglifier (>= 1.0.3)
View
@@ -5,6 +5,11 @@ class Song < ActiveRecord::Base
has_attached_file :audio
+ # http://rubydoc.info/github/thoughtbot/paperclip/master/Paperclip/ClassMethods:validates_attachment_content_type
+ # https://netfiles.uiuc.edu/xythoswfs/static/en/content_type.htm
+ validates_attachment_presence :audio
+ validates_attachment_content_type :audio, :content_type => /^audio/
+
def artist_name
self.artist.name if self.artist.present?
end
View
@@ -5,3 +5,58 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
+
+# Create a mixtape
+# Loop through the contents of a directory
+ # for each file, check if it's an mp3
+ # if it's an mp3 file, create a song
+ # parse the file for an artist name
+ # read the file data
+ # create a song with the file data and the artist
+# Add all the songs to the mixtape
+ # require "mp3info"
+
+@mixtape = Mixtape.new(:name => "My Favorite Songs")
+puts "Initialized Mixtape - #{@mixtape.name}..."
+
+@songs = []
+
+# File API - http://www.ruby-doc.org/core-1.9.2/File.html
+# Dir API - http://www.ruby-doc.org/core-1.9.2/Dir.html
+# Dir.entries vs Dir.foreach
+
+# Dir.entries("#{Rails.root}/db/seed/songs").each do |file
+
+puts "Opening #{Rails.root}/db/seed/songs..."
+
+Dir.foreach("#{Rails.root}/db/seed/songs") do |file_name|
+ next if file_name == "." || file_name == ".."
+ puts "...processing #{file_name}"
+
+ # How to check if it's an MP3
+
+ # if file_name.split(".").last != "mp3"
+ # puts "......skipping #{file_name} because it isn't an mp3"
+ # next
+ # end
+
+ file = File.new("#{Rails.root}/db/seed/songs/#{file_name}", 'r')
+
+ if File.extname(file.path) != ".mp3"
+ puts "......skipping #{file_name} because it isn't an mp3"
+ next
+ end
+
+ # https://www.ruby-toolbox.com/gems/ruby-mp3info
+ # https://github.com/moumar/ruby-mp3info
+ # http://rubydoc.info/gems/ruby-mp3info/frames
+
+ mp3 = Mp3Info.open(file.path)
+
+ song = Song.new :name => mp3.tag.title, :artist_name => mp3.tag.artist, :audio => file
+
+ @songs << song if song.save
+end
+
+@mixtape.songs << @songs
+@mixtape.save

0 comments on commit e5cde26

Please sign in to comment.