Skip to content

Commit

Permalink
Seed script will read through db/seed/songs and create a favorite mix…
Browse files Browse the repository at this point in the history
…tape with all valid songs. Songs are validated by presence and content type of audio
  • Loading branch information
aviflombaum committed Nov 14, 2011
1 parent 0b7fac9 commit e5cde26
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@ db/*.sqlite3
log/*.log log/*.log
tmp/ tmp/
.sass-cache/ .sass-cache/
public/system public/system
db/seed
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -17,6 +17,10 @@ group :assets do
end end


gem 'paperclip' gem 'paperclip'

gem 'ruby-mp3info', :require => 'mp3info'
# gem 'taglib-ruby', :require => 'taglib'

gem 'jquery-rails' gem 'jquery-rails'


group :development, :test do group :development, :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -113,6 +113,7 @@ GEM
columnize (>= 0.3.1) columnize (>= 0.3.1)
linecache19 (>= 0.5.11) linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19) ruby-debug-base19 (>= 0.11.19)
ruby-mp3info (0.6.16)
ruby_core_source (0.1.5) ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2) archive-tar-minitar (>= 0.5.2)
sass (3.1.10) sass (3.1.10)
Expand Down Expand Up @@ -147,6 +148,7 @@ DEPENDENCIES
rails (= 3.1.1) rails (= 3.1.1)
rspec-rails rspec-rails
ruby-debug19 ruby-debug19
ruby-mp3info
sass-rails (~> 3.1.4) sass-rails (~> 3.1.4)
sqlite3 sqlite3
uglifier (>= 1.0.3) uglifier (>= 1.0.3)
5 changes: 5 additions & 0 deletions app/models/song.rb
Expand Up @@ -5,6 +5,11 @@ class Song < ActiveRecord::Base


has_attached_file :audio 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 def artist_name
self.artist.name if self.artist.present? self.artist.name if self.artist.present?
end end
Expand Down
55 changes: 55 additions & 0 deletions db/seeds.rb
Expand Up @@ -5,3 +5,58 @@
# #
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first) # 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.