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-24 09:36:44 +00:00
|
|
|
|
2011-06-17 21:49:32 +00:00
|
|
|
@@user_agent = 'BeatHaven.org'
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-06-17 21:49:32 +00:00
|
|
|
@@lastfm_api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
2011-06-24 09:36:44 +00:00
|
|
|
@@lastfm_secret = '19e70e98b291e9f15d0516925945eb1b'
|
|
|
|
|
|
|
|
@@brainz_login = 'magnolia_fan'
|
|
|
|
@@brainz_password = '111111'
|
|
|
|
|
2011-06-14 21:29:36 +00:00
|
|
|
def self.importArtist name
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
# Initializing gems
|
|
|
|
brainz = MusicBrainz::Client.new(@@brainz_login, @@brainz_password, @@user_agent)
|
|
|
|
lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret)
|
|
|
|
|
2011-06-19 21:23:22 +00:00
|
|
|
artist = Artist.find_by_name(name)
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
begin
|
|
|
|
|
|
|
|
# Get artist info
|
|
|
|
artist_mb_data = ArtistController.musicBrainzExactSearch(name).first
|
|
|
|
begin
|
|
|
|
artist_lastfm = lastfm.artist.get_info(name)
|
2011-06-24 11:15:35 +00:00
|
|
|
artist_mb = brainz.artist(artist_mb_data[:mbid])
|
|
|
|
rescue => e
|
|
|
|
ap e.message
|
|
|
|
ap e.backtrace
|
2011-06-24 09:36:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Save artist
|
|
|
|
artist.desc = artist_lastfm['bio']['summary']
|
|
|
|
artist.pic_url = artist_lastfm['image'][3]['content']
|
|
|
|
artist.artist_type = artist_mb['artist']['type']
|
2011-06-24 11:39:19 +00:00
|
|
|
artist.mbid = artist_mb_data[:mbid]
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
# Get albums from MB
|
2011-06-24 11:15:35 +00:00
|
|
|
release_groups_mb = brainz.release_group(nil, :artist => artist_mb_data[:mbid])['release_group_list']['release_group']
|
2011-06-24 09:36:44 +00:00
|
|
|
release_groups_mb.each do |mb_album|
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
# Get album releases from MB
|
|
|
|
releases_mb = brainz.release(nil, {'release-group' => mb_album['id']})['release_list']['release']
|
|
|
|
if releases_mb.is_a? Hash
|
|
|
|
releases_mb = [releases_mb]
|
|
|
|
end
|
|
|
|
releases_mb.delete_if { |a| a['date'].nil? }
|
|
|
|
releases_mb.each do |release|
|
|
|
|
if release['date'].length == 4
|
|
|
|
release['date'] += '-12-28'
|
|
|
|
elsif release['date'].length == 7
|
|
|
|
release['date'] += '-28'
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
end
|
|
|
|
|
2011-06-24 09:59:35 +00:00
|
|
|
unless releases_mb.empty?
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +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-24 09:36:44 +00:00
|
|
|
# Get tracks from the first release and then exclude release
|
|
|
|
tracks_mb = []
|
|
|
|
tracks_mb_r = brainz.release(main_release['id'], {'inc' => 'recordings'})['release']['medium_list']['medium']
|
|
|
|
if tracks_mb_r.is_a? Hash
|
2011-06-24 11:15:35 +00:00
|
|
|
if tracks_mb_r['track_list']['track'].is_a? Hash
|
|
|
|
tracks_mb_r['track_list']['track'] = [tracks_mb_r['track_list']['track']]
|
|
|
|
end
|
2011-06-24 09:36:44 +00:00
|
|
|
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|
|
2011-06-24 11:15:35 +00:00
|
|
|
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 }
|
2011-06-24 09:36:44 +00:00
|
|
|
tracks_mb |= cd['track_list']['track']
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
unless tracks_mb.empty?
|
|
|
|
|
|
|
|
# Saving album
|
|
|
|
begin
|
|
|
|
album_lastfm = lastfm.album.get_info(artist_lastfm['name'], mb_album['title'])
|
|
|
|
album_image = album_lastfm['image'][3]['content']
|
|
|
|
rescue
|
|
|
|
album_image = ''
|
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
album = Album.new
|
|
|
|
album.name = mb_album['title']
|
|
|
|
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
|
|
|
|
album.save
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +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
|
2011-06-20 22:11:44 +00:00
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
# Searching for tracks in other releases
|
|
|
|
releases_mb.each do |mb_release|
|
|
|
|
|
|
|
|
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
|
2011-06-24 11:15:35 +00:00
|
|
|
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
|
2011-06-24 09:36:44 +00:00
|
|
|
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|
|
2011-06-24 11:15:35 +00:00
|
|
|
if cd['track_list']['track'].is_a? Hash
|
|
|
|
cd['track_list']['track'] = [cd['track_list']['track']]
|
|
|
|
end
|
2011-06-24 09:36:44 +00:00
|
|
|
other_tracks_mb |= cd['track_list']['track']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2011-06-17 21:49:32 +00:00
|
|
|
end
|
2011-06-15 02:24:01 +00:00
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
# Saving tracks
|
|
|
|
tracks_mb.each do |mb_track|
|
|
|
|
unless ['[silence]', '[untitled]'].include? mb_track['recording']['title']
|
|
|
|
track = Track.new
|
2011-06-24 09:59:35 +00:00
|
|
|
track.name = mb_track['recording']['title'].gsub(/\s\\\s\[.*?\]/, '')
|
2011-06-24 09:36:44 +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']
|
|
|
|
track.save
|
|
|
|
end
|
2011-06-20 22:11:44 +00:00
|
|
|
end
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
end # unless mb_main_tracks.empty?
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
end # unless mb_releases.empty?
|
2011-06-17 21:49:32 +00:00
|
|
|
|
2011-06-24 09:36:44 +00:00
|
|
|
end # mb_albums.each do |mb_album|
|
|
|
|
|
|
|
|
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-19 21:23:22 +00:00
|
|
|
artist.save
|
|
|
|
|
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
|