2011-06-24 09:36:44 +00:00
|
|
|
require 'lastfm'
|
|
|
|
require 'musicbrainz'
|
|
|
|
|
2011-06-14 21:29:36 +00:00
|
|
|
class ImportController < ApplicationController
|
2011-06-17 21:49:32 +00:00
|
|
|
@@lastfm_api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
2011-06-24 09:36:44 +00:00
|
|
|
@@lastfm_secret = '19e70e98b291e9f15d0516925945eb1b'
|
|
|
|
|
2011-06-28 20:54:46 +00:00
|
|
|
def self.importArtist name, dry_run = false
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
# Initializing gems
|
|
|
|
lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret)
|
|
|
|
|
2011-06-27 11:14:14 +00:00
|
|
|
artist = Artist.find_or_create_by_name(name)
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-09-09 22:13:02 +00:00
|
|
|
begin
|
2011-09-14 14:20:25 +00:00
|
|
|
lastfm_artist = lastfm.artist.get_info(name)
|
|
|
|
brainz_artist = MusicBrainz::Artist.find_by_name(name)
|
|
|
|
rescue => e
|
|
|
|
ap e.message
|
|
|
|
ap e.backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
# Save artist
|
|
|
|
artist.desc = lastfm_artist['bio']['summary']
|
|
|
|
artist.pic_url = lastfm_artist['image'][3]['content']
|
|
|
|
artist.artist_type = brainz_artist.type
|
|
|
|
artist.mbid = brainz_artist.id
|
|
|
|
artist.save! unless dry_run
|
|
|
|
|
|
|
|
brainz_artist.release_groups.each do |release_group|
|
|
|
|
# Saving album
|
2011-06-24 09:36:44 +00:00
|
|
|
begin
|
2011-09-14 14:20:25 +00:00
|
|
|
album_lastfm = lastfm.album.get_info(lastfm_artist['name'], release_group.title)
|
|
|
|
album_image = album_lastfm['image'][3]['content']
|
|
|
|
rescue
|
|
|
|
album_image = ''
|
|
|
|
end
|
|
|
|
album = Album.new
|
|
|
|
album.name = release_group.title
|
|
|
|
album.year = release_group.first_release_date.year
|
|
|
|
album.artist_id = artist.id
|
|
|
|
album.mbid = release_group.id
|
|
|
|
album.album_type = release_group.type
|
|
|
|
album.pic_url = album_image
|
|
|
|
album.has_pic = (album_image != '' and not album_image.nil?)
|
|
|
|
dry_run ? ap(album) : album.save
|
|
|
|
# Preparing releases
|
|
|
|
release_group.releases.each do |release|
|
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
end
|
2011-09-09 22:13:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2011-06-24 09:36:44 +00:00
|
|
|
# Get albums from MB
|
2011-06-27 11:14:14 +00:00
|
|
|
release_groups_mb = brainz.release_group(nil, :artist => artist_mb_data[:mbid], :limit => 500)
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
unless release_groups_mb['release_group_list']['release_group'].nil?
|
2011-06-25 15:23:46 +00:00
|
|
|
|
2011-06-27 11:14:14 +00:00
|
|
|
if release_groups_mb['release_group_list']['release_group'].is_a? Hash
|
|
|
|
release_groups_mb['release_group_list']['release_group'] = [release_groups_mb['release_group_list']['release_group']]
|
|
|
|
end
|
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
release_groups_mb['release_group_list']['release_group'].each do |mb_album|
|
2011-06-25 15:23:46 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
album_name = mb_album['title'] + (mb_album['disambiguation'].nil? ? '' : ' ('+ mb_album['disambiguation'] +')')
|
|
|
|
|
|
|
|
# Checking if there is already an album with the same name
|
2011-06-27 11:14:14 +00:00
|
|
|
albums_db = Album.where(:artist_id => artist.id, :name => album_name, :album_type => mb_album['type'])
|
2011-06-28 20:54:46 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Get album releases from MB
|
|
|
|
releases_mb = brainz.release(nil, {'release-group' => mb_album['id']})
|
|
|
|
|
|
|
|
unless releases_mb['release_list']['release'].nil? or not albums_db.empty?
|
|
|
|
|
|
|
|
releases_mb = releases_mb['release_list']['release']
|
2011-06-25 15:23:46 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
if releases_mb.is_a? Hash
|
|
|
|
releases_mb = [releases_mb]
|
2011-06-25 15:23:46 +00:00
|
|
|
end
|
2011-06-26 05:25:30 +00:00
|
|
|
|
|
|
|
releases_mb.delete_if { |a| a['date'].nil? }
|
|
|
|
|
|
|
|
unless releases_mb.empty?
|
|
|
|
|
|
|
|
releases_mb.each do |release|
|
|
|
|
if release['date'].length == 4
|
|
|
|
release['date'] += '-12-28'
|
|
|
|
elsif release['date'].length == 7
|
|
|
|
release['date'] += '-28'
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Sorting releases by date
|
|
|
|
releases_mb.sort! { |a, b| a['date'].to_date <=> b['date'].to_date }
|
|
|
|
main_release = releases_mb.shift
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Get tracks from the first release and then exclude release
|
|
|
|
tracks_mb = []
|
|
|
|
tracks_mb_r = brainz.release(main_release['id'], {'inc' => 'recordings'})
|
|
|
|
|
|
|
|
unless tracks_mb_r['release']['medium_list']['medium'].nil?
|
|
|
|
|
|
|
|
tracks_mb_r = tracks_mb_r['release']['medium_list']['medium']
|
|
|
|
|
|
|
|
if tracks_mb_r.is_a? Hash
|
|
|
|
if tracks_mb_r['track_list']['track'].is_a? Hash
|
|
|
|
tracks_mb_r['track_list']['track'] = [tracks_mb_r['track_list']['track']]
|
|
|
|
end
|
|
|
|
tracks_mb = tracks_mb_r['track_list']['track']
|
|
|
|
elsif tracks_mb_r.is_a? Array
|
|
|
|
tracks_mb = tracks_mb_r.shift['track_list']['track']
|
|
|
|
tracks_mb_r.each do |cd|
|
|
|
|
if cd['track_list']['track'].is_a? Hash
|
|
|
|
cd['track_list']['track'] = [cd['track_list']['track']]
|
|
|
|
end
|
|
|
|
cd['track_list']['track'].each { |mb_track| mb_track['bonus'] = 1 }
|
|
|
|
tracks_mb |= cd['track_list']['track']
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
unless tracks_mb.empty?
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Saving album
|
|
|
|
begin
|
|
|
|
album_lastfm = lastfm.album.get_info(artist_lastfm['name'], album_name)
|
|
|
|
album_image = album_lastfm['image'][3]['content']
|
|
|
|
rescue
|
|
|
|
album_image = ''
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
album = Album.new
|
|
|
|
album.name = album_name
|
|
|
|
album.year = main_release['date'].to_date.year
|
|
|
|
album.artist_id = artist.id
|
|
|
|
album.mbid = mb_album['id']
|
|
|
|
album.album_type = mb_album['type']
|
|
|
|
album.pic_url = album_image
|
2011-06-30 05:29:24 +00:00
|
|
|
album.has_pic = (album_image != '' and not album_image.nil?)
|
2011-06-28 20:54:46 +00:00
|
|
|
album.save unless dry_run
|
|
|
|
ap album if dry_run
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Creating hashed array for matching
|
|
|
|
mb_main_tracks_hashed = []
|
|
|
|
tracks_mb.each do |mb_track|
|
|
|
|
unless mb_main_tracks_hashed.include? mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join
|
|
|
|
mb_main_tracks_hashed << mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join
|
|
|
|
mb_track['country'] = main_release['country'] unless main_release['country'].nil?
|
|
|
|
mb_track['bonus'] = 0 if mb_track['bonus'].nil?
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Searching for tracks in other releases
|
|
|
|
releases_mb.each do |mb_release|
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
other_tracks_mb = []
|
|
|
|
other_tracks_mb_r = brainz.release(mb_release['id'], {'inc' => 'recordings'})['release']['medium_list']['medium']
|
|
|
|
if other_tracks_mb_r.is_a? Hash
|
|
|
|
if other_tracks_mb_r['track_list']['track'].is_a? Hash
|
|
|
|
other_tracks_mb_r['track_list']['track'] = [other_tracks_mb_r['track_list']['track']]
|
|
|
|
end
|
|
|
|
other_tracks_mb = other_tracks_mb_r['track_list']['track']
|
|
|
|
elsif other_tracks_mb_r.is_a? Array
|
|
|
|
other_tracks_mb_r.each do |cd|
|
|
|
|
if cd['track_list']['track'].is_a? Hash
|
|
|
|
cd['track_list']['track'] = [cd['track_list']['track']]
|
|
|
|
end
|
|
|
|
other_tracks_mb |= cd['track_list']['track']
|
|
|
|
end
|
|
|
|
end
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
other_tracks_mb.each do |mb_track|
|
|
|
|
unless mb_main_tracks_hashed.include? mb_track['recording']['title'].downcase.scan(/[a-z0-9]*/).join
|
|
|
|
mb_track['country'] = mb_release['country'] unless mb_release['country'].nil?
|
|
|
|
mb_track['bonus'] = 1
|
|
|
|
tracks_mb << mb_track
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
# Saving tracks
|
|
|
|
tracks_mb.each do |mb_track|
|
|
|
|
unless ['[silence]', '[untitled]'].include? mb_track['recording']['title']
|
|
|
|
track = Track.new
|
2011-09-09 22:13:02 +00:00
|
|
|
track.name = mb_track['recording']['title'].gsub(/\s\/\s\[.*?\]/, '')
|
2011-06-26 05:25:30 +00:00
|
|
|
track.album_id = album.id
|
|
|
|
track.position = mb_track['position']
|
|
|
|
track.length = mb_track['length'] unless mb_track['length'].nil?
|
|
|
|
track.length = mb_track['recording']['length'] unless mb_track['recording']['length'].nil?
|
|
|
|
track.country = mb_track['country']
|
|
|
|
track.bonus = mb_track['bonus']
|
|
|
|
track.live = mb_track['recording']['title'].downcase.include? 'live'
|
|
|
|
track.acoustic = mb_track['recording']['title'].downcase.include? 'acoustic'
|
|
|
|
track.mbid = mb_track['recording']['id']
|
2011-06-28 20:54:46 +00:00
|
|
|
track.save unless dry_run
|
2011-06-26 05:25:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # unless tracks_mb_r['release']['medium_list']['medium'].nil?
|
|
|
|
|
|
|
|
end # releases_mb.empty?
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
end # unless mb_main_tracks.empty?
|
2011-06-27 11:14:14 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
end # unless releases_mb.nil?
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-26 05:25:30 +00:00
|
|
|
end # release_groups_mb.each do |mb_album|
|
|
|
|
|
|
|
|
end # unless release_groups_mb['release_group_list']['release_group'].nil?
|
2011-06-24 09:36:44 +00:00
|
|
|
|
|
|
|
artist.status = 1
|
|
|
|
|
2011-06-24 11:15:35 +00:00
|
|
|
rescue => e
|
|
|
|
ap e.message
|
|
|
|
ap e.backtrace
|
2011-06-24 09:36:44 +00:00
|
|
|
artist.status = 2
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-28 20:54:46 +00:00
|
|
|
artist.save unless dry_run
|
|
|
|
ap artist if dry_run
|
2011-06-19 21:23:22 +00:00
|
|
|
|
2011-06-15 02:24:01 +00:00
|
|
|
end # def self.importArtist name
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-15 02:24:01 +00:00
|
|
|
end
|