1
0
Fork 0
oldhaven/lib/tasks/db_import.rake

72 lines
2.7 KiB
Ruby
Raw Normal View History

2011-06-14 16:05:12 +00:00
namespace :db do
desc 'Imports data from MusicBrainz database'
task :import => :environment do
2011-06-14 18:06:46 +00:00
ati = ['Kasabian']
ati.each do |name|
# Get artist from MB
mb_artist = MusicBrainzArtist.getByName(name)
# Save artist
artist = Artist.new
artist.name = mb_artist.name
artist.desc = 'test import desc'
artist.pic_url = '/images/dummy.png'
artist.save
# Get albums from MB
mb_albums = MusicBrainzAlbum.where('artist_id = ? AND release_type = 1', mb_artist.id).order('year ASC, id ASC')
mb_albums.each do |mb_album|
# Saving album
album = Album.new
album.name = mb_album.name
album.artist_id = artist.id
album.status = 1
album.pic_url = '/images/dummy.png'
album.save
# Get album releases from MB
mb_releases = MusicBrainzRelease.where('album_id = ? AND release_type = 1', mb_album.id).order('date ASC, id ASC')
# Get tracks from the first release and then exclude release
mb_main_tracks = MusicBrainzTrack.where('release_id IN (?)', mb_releases.shift.id).order('position ASC')
# Creating hashed array for matching
mb_main_tracks_hashed = []
mb_main_tracks.each do |mb_track|
mb_main_tracks_hashed << mb_track.name.downcase.scan(/[a-z0-9]*/).join
end
# Getting other releases ids
release_ids = []
mb_releases.each do |mb_release|
release_ids << mb_release.id
end
# Searching for tracks in other releases
mb_other_tracks = MusicBrainzTrack.where('release_id IN (?)', release_ids).order('position ASC')
mb_bonus_tracks = []
mb_other_tracks.each do |mb_track|
unless mb_main_tracks_hashed.include? mb_track.name.downcase.scan(/[a-z0-9]*/).join
mb_bonus_tracks << mb_track
end
end
# Saving tracks
mb_main_tracks.each do |mb_track|
track = Track.new
track.name = mb_track.name
track.album_id = album.id
track.position = mb_track.position
track.length = mb_track.length
track.bonus = 0
track.live = mb_track.name.downcase.include? 'live'
track.acoustic = mb_track.name.downcase.include? 'acoustic'
track.save
end
mb_bonus_tracks.each do |mb_track|
track = Track.new
track.name = mb_track.name
track.album_id = album.id
track.position = mb_track.position
track.length = mb_track.length
track.bonus = 1
track.live = mb_track.name.downcase.include? 'live'
track.acoustic = mb_track.name.downcase.include? 'acoustic'
track.save
end
end
end
2011-06-14 16:05:12 +00:00
end
end