class ImportController < ApplicationController @@user_agent = 'BeatHaven.org' @@lastfm_api_key = '04fda005dbf61a50af5abc3e90f111f2' def self.importArtist name # Get artist info artist_mb_data = ArtistController.musicBrainzExactSearch(name).first begin artist_lastfm_xml = open( 'http://ws.audioscrobbler.com/2.0/'+ '?method=artist.getinfo&api_key='+ @@lastfm_api_key + '&artist='+ URI.escape(name).gsub(/amp;/, '').gsub(/\&/, '%26').gsub(/\?/, '%3F') ).read artist_desc = artist_lastfm_xml.scan(/<\!\[CDATA\[?(.*)\]\]><\/summary>/m)[0][0] artist_pic = artist_lastfm_xml.scan(/?(.*)<\/image>/)[0][0] rescue artist_desc = '' artist_pic = '' end # Save artist artist = Artist.find_by_name(name) #artist.name = artist_mb_data[:name] artist.desc = artist_desc artist.pic_url = artist_pic artist.artist_type = artist_mb_data[:type] artist.mbid = artist_mb_data[:mbid] artist.save # Get albums from MB release_groups_mb_xml = open( 'http://musicbrainz.org/ws/2/release-group/?artist='+ artist_mb_data[:mbid], 'User-Agent' => @@user_agent ).read release_groups_mb_data = release_groups_mb_xml.scan(/(.+?)<\/title>/m) release_groups_mb_data.each do |mb_album| # Get album releases from MB releases_mb_xml = open( 'http://musicbrainz.org/ws/2/release/?release-group='+ mb_album[1], 'User-Agent' => @@user_agent ).read releases_mb_data = releases_mb_xml.scan(/<release\sid=\"([a-f0-9-]+?)\">.*?<status>([a-zA-Z]+?)<\/status>.*?<date>([0-9-]+?)<\/date>.*?<country>([A-Z]+?)<\/country>/m) releases_mb_data.each do |item| if item[2].length == 4 item[2] += '-12-28' elsif item[2].length == 7 item[2] += '-28' end end unless releases_mb_data.empty? # Sorting releases by date releases_mb_data.sort! { |a, b| a[2].to_date <=> b[2].to_date } main_release = releases_mb_data.shift # Get tracks from the first release and then exclude release main_tracks_mb_xml = open( 'http://musicbrainz.org/ws/2/release/'+ main_release[0] +'/?inc=recordings', 'User-Agent' => @@user_agent ).read mb_tracks = main_tracks_mb_xml.scan(/<track>.*?<position>(\d+?)<\/position>.*?<recording\sid=\"([a-f0-9-]+?)\"><title>(.*?)<\/title>(<length>(.*?)<\/length>)?/m) mb_tracks.map! do |item| item << main_release[3] end unless mb_tracks.empty? # Saving album begin album_lastm_xml = open( 'http://ws.audioscrobbler.com/2.0/'+ '?method=album.getinfo&api_key='+ @@lastfm_api_key + '&artist='+ URI.escape(name).gsub(/amp;/, '').gsub(/\&/, '%26').gsub(/\?/, '%3F')+ '&album='+ URI.escape(mb_album[2]).gsub(/amp;/, '').gsub(/\&/, '%26').gsub(/\?/, '%3F') ).read album_image = album_lastm_xml.scan(/<image\ssize=\"extralarge\">?(.*)<\/image>/)[0][0] rescue album_image = '' end album = Album.new album.name = mb_album[2] album.year = main_release[2].to_date.year album.artist_id = artist.id album.mbid = mb_album[1] album.album_type = mb_album[0] album.pic_url = album_image album.save # Creating hashed array for matching mb_main_tracks_hashed = [] mb_tracks.each do |mb_track| unless mb_main_tracks_hashed.include? mb_track[2].downcase.scan(/[a-z0-9]*/).join mb_main_tracks_hashed << mb_track[2].downcase.scan(/[a-z0-9]*/).join mb_track << main_release[3] # Release country mb_track << 0 # Bonus flag end end # Searching for tracks in other releases releases_mb_data.each do |mb_release| other_tracks_mb_xml = open( 'http://musicbrainz.org/ws/2/release/'+ mb_release[0] +'/?inc=recordings', 'User-Agent' => @@user_agent ).read mb_other_tracks = other_tracks_mb_xml.scan(/<track>.*?<position>(\d+?)<\/position>.*?<recording\sid=\"([a-f0-9-]+?)\"><title>(.*?)<\/title><length>(.*?)<\/length>/m) mb_other_tracks.each do |item| unless mb_main_tracks_hashed.include? item[2].downcase.scan(/[a-z0-9]*/).join item << mb_release[3] # Release country item << 1 # Bonus flag mb_tracks << item end end end # Saving tracks mb_tracks.each do |mb_track| unless mb_track[2] == '[silence]' or mb_track[2] == '[untitled]' track = Track.new track.name = mb_track[2] track.album_id = album.id track.position = mb_track[0] track.length = mb_track[4] track.country = mb_track[5] track.bonus = mb_track[6] track.live = mb_track[2].downcase.include? 'live' track.acoustic = mb_track[2].downcase.include? 'acoustic' track.save end end end # unless mb_main_tracks.empty? end # unless mb_releases.empty? end # mb_albums.each do |mb_album| artist.status = 1 artist.save end # def self.importArtist name end