1
0
Fork 0
oldhaven/app/controllers/import_controller.rb

82 lines
3.7 KiB
Ruby

class ImportController < ApplicationController
def self.importArtist name
# Get artist from MB
mb_artist = MusicBrainzArtist.getByName(name)
# # Save artist
artist_xml = open(
'http://ws.audioscrobbler.com/2.0/'+
'?method=artist.getinfo&api_key=04fda005dbf61a50af5abc3e90f111f2'+
'&artist='+ URI.escape(name)).read
artist = Artist.new
artist.name = mb_artist.name
artist.desc = artist_xml.scan(/<summary><\!\[CDATA\[?(.*)\]\]><\/summary>/m)[0][0]
artist.pic_url = artist_xml.scan(/<image\ssize=\"extralarge\">?(.*)<\/image>/)[0][0]
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|
# Get album releases from MB
mb_releases = MusicBrainzRelease.where('album_id = ? AND release_type = 1', mb_album.id).order('date ASC, id ASC')
unless mb_releases.empty?
# 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')
unless mb_main_tracks.empty?
# Saving album
album_xml = open(
'http://ws.audioscrobbler.com/2.0/'+
'?method=album.getinfo&api_key=04fda005dbf61a50af5abc3e90f111f2'+
'&artist='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F')+
'&album='+ URI.escape(mb_album.name).gsub(/\&/, '%26').gsub(/\?/, '%3F')).read
album = Album.new
album.name = mb_album.name
album.year = mb_album.year
album.artist_id = artist.id
album.status = 1
album.pic_url = album_xml.scan(/<image\ssize=\"extralarge\">?(.*)<\/image>/)[0][0]
album.save
# 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 # mb_other_tracks.each do |mb_track|
# 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_main_tracks.each do |mb_track|
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 # mb_bonus_tracks.each do |mb_track|
end # unless mb_main_tracks.empty?
end # unless mb_releases.empty?
end # mb_albums.each do |mb_album|
end # def self.importArtist name
end