2011-06-24 09:36:44 +00:00
|
|
|
require 'musicbrainz'
|
2011-09-26 05:10:23 +00:00
|
|
|
require 'discogs'
|
2011-09-16 00:21:07 +00:00
|
|
|
require 'nokogiri'
|
2011-09-26 05:10:23 +00:00
|
|
|
require 'zlib'
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-06-14 21:29:36 +00:00
|
|
|
class ImportController < ApplicationController
|
2011-06-28 20:54:46 +00:00
|
|
|
def self.importArtist name, dry_run = false
|
2011-09-16 00:21:07 +00:00
|
|
|
artist = Artist.find_or_create_by_name(name)
|
|
|
|
return 3 if artist.status == 1
|
2011-06-24 09:36:44 +00:00
|
|
|
|
2011-09-09 22:13:02 +00:00
|
|
|
begin
|
2011-09-17 19:00:08 +00:00
|
|
|
lastfm_artist = LastFM::Artist.get_info( :artist => name )
|
2011-09-16 02:02:39 +00:00
|
|
|
unless artist.mbid.nil? or artist.mbid.empty?
|
2011-09-16 00:21:07 +00:00
|
|
|
brainz_artist = MusicBrainz::Artist.find(artist.mbid)
|
|
|
|
else
|
|
|
|
brainz_artist = MusicBrainz::Artist.find_by_name(name)
|
|
|
|
end
|
2011-09-14 14:20:25 +00:00
|
|
|
rescue => e
|
2011-09-17 19:00:08 +00:00
|
|
|
lastfm_artist = { 'artist' => {
|
2011-09-14 16:03:21 +00:00
|
|
|
'bio' => { 'summary' => '' },
|
2011-09-18 14:25:47 +00:00
|
|
|
'image' => [ nil, nil, nil, { '#text' => '' } ],
|
2011-09-16 00:21:07 +00:00
|
|
|
'stats' => { 'listeners' => 0 }
|
2011-09-17 19:00:08 +00:00
|
|
|
} }
|
2011-09-14 14:20:25 +00:00
|
|
|
ap e.message
|
|
|
|
ap e.backtrace
|
|
|
|
end
|
2011-09-19 04:07:45 +00:00
|
|
|
|
|
|
|
if brainz_artist.nil?
|
|
|
|
artist.mbid = nil
|
|
|
|
artist.status = 2
|
|
|
|
artist.save
|
|
|
|
return 2
|
|
|
|
end
|
2011-09-14 14:20:25 +00:00
|
|
|
|
2011-09-09 22:13:02 +00:00
|
|
|
begin
|
2011-09-14 16:03:21 +00:00
|
|
|
# Save artist
|
2011-09-17 19:00:08 +00:00
|
|
|
artist.desc = lastfm_artist['artist']['bio']['summary']
|
|
|
|
artist.pic_url = lastfm_artist['artist']['image'][3]['#text']
|
|
|
|
artist.listeners = lastfm_artist['artist']['stats']['listeners']
|
2011-09-14 16:03:21 +00:00
|
|
|
artist.artist_type = brainz_artist.type
|
|
|
|
artist.mbid = brainz_artist.id
|
2011-09-20 20:30:42 +00:00
|
|
|
artist.save
|
|
|
|
|
|
|
|
brainz_artist.urls.each do |service, url|
|
|
|
|
ArtistLink.new(
|
|
|
|
:artist_id => artist.id,
|
|
|
|
:service => service.to_s,
|
|
|
|
:url => url
|
|
|
|
).save
|
|
|
|
end
|
2011-09-26 05:10:23 +00:00
|
|
|
|
|
|
|
unless brainz_artist.urls[:discogs].nil?
|
|
|
|
Discogs.get_master_albums(brainz_artist.urls[:discogs]).each do |info|
|
|
|
|
begin
|
|
|
|
puts " * "+info[:title]+" ("+info[:year]+") -- http://api.discogs.com/"+info[:uri]+")"
|
|
|
|
stream = open("http://api.discogs.com/"+info[:uri]+"?f=xml",
|
|
|
|
'User-Agent' => 'Haven Import Bot',
|
|
|
|
'Accept-Encoding' => 'gzip, deflate'
|
2011-09-20 20:30:42 +00:00
|
|
|
)
|
2011-09-26 05:10:23 +00:00
|
|
|
if (stream.content_encoding.empty?)
|
|
|
|
body = stream.read
|
|
|
|
else
|
|
|
|
body = Zlib::GzipReader.new(stream).read
|
2011-06-25 15:23:46 +00:00
|
|
|
end
|
2011-09-26 05:10:23 +00:00
|
|
|
# Creating album
|
|
|
|
album = Album.new(
|
|
|
|
:name => info[:title],
|
|
|
|
:artist_id => artist.id,
|
|
|
|
:year => info[:year],
|
|
|
|
:status => 1,
|
|
|
|
:master => true
|
|
|
|
)
|
|
|
|
begin
|
|
|
|
album_lastfm = LastFM::Album.get_info( :artist => lastfm_artist['artist']['name'], :album => info[:title] )
|
|
|
|
album_image = album_lastfm['album']['image'][3]['#text']
|
|
|
|
rescue
|
|
|
|
album_image = ''
|
|
|
|
end
|
|
|
|
album.pic_url = album_image
|
|
|
|
album.has_pic = (album_image != '' and not album_image.nil?)
|
|
|
|
album.save
|
|
|
|
Discogs.save_album(artist, album, Nokogiri::HTML(body))
|
2011-09-14 16:03:21 +00:00
|
|
|
end
|
2011-09-26 05:10:23 +00:00
|
|
|
sleep 1
|
2011-09-14 16:03:21 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-26 05:10:23 +00:00
|
|
|
|
|
|
|
# brainz_artist.release_groups.each do |brainz_release_group|
|
|
|
|
# # Saving album
|
|
|
|
# begin
|
|
|
|
# album_lastfm = LastFM::Album.get_info( :artist => lastfm_artist['artist']['name'], :album => '' )
|
|
|
|
# album_image = album_lastfm['album']['image'][3]['#text']
|
|
|
|
# rescue
|
|
|
|
# album_image = ''
|
|
|
|
# end
|
|
|
|
# album = Album.new
|
|
|
|
# album.name = brainz_release_group.title
|
|
|
|
# album.year = brainz_release_group.first_release_date.year
|
|
|
|
# album.artist_id = artist.id
|
|
|
|
# album.mbid = brainz_release_group.id
|
|
|
|
# album.album_type = brainz_release_group.type
|
|
|
|
# album.pic_url = album_image
|
|
|
|
# album.has_pic = (album_image != '' and not album_image.nil?)
|
|
|
|
# album.status = 0
|
|
|
|
# album.save
|
|
|
|
# # Tracks from the first release
|
|
|
|
# tracks_hashed = []
|
|
|
|
# brainz_release_group.releases.each_with_index do |brainz_release, i|
|
|
|
|
# local_brainz_release = LocalBrainz::Release.new(
|
|
|
|
# :mbid => brainz_release.id,
|
|
|
|
# :title => brainz_release.title,
|
|
|
|
# :status => brainz_release.status,
|
|
|
|
# :date => brainz_release.date,
|
|
|
|
# :country => brainz_release.country,
|
|
|
|
# :format => brainz_release.format,
|
|
|
|
# :album_id => album.id
|
|
|
|
# )
|
|
|
|
# local_brainz_release.save
|
|
|
|
# # Processing tracks
|
|
|
|
# brainz_release.tracks.each do |brainz_track|
|
|
|
|
# local_brainz_track = LocalBrainz::Track.new(
|
|
|
|
# :position => brainz_track.position,
|
|
|
|
# :recording_id => brainz_track.recording_id,
|
|
|
|
# :title => brainz_track.title,
|
|
|
|
# :length => brainz_track.length,
|
|
|
|
# :release_id => local_brainz_release.id
|
|
|
|
# )
|
|
|
|
# local_brainz_track.save
|
|
|
|
# track_title = brainz_track.title.gsub(/\s\/\s\[.*?\]/, '')
|
|
|
|
# if tracks_hashed.include? track_title.downcase.scan(/[a-z0-9]*/).join
|
|
|
|
# next
|
|
|
|
# end
|
|
|
|
# tracks_hashed << track_title.downcase.scan(/[a-z0-9]*/).join
|
|
|
|
# track = Track.new
|
|
|
|
# track.name = track_title
|
|
|
|
# track.album_id = album.id
|
|
|
|
# track.position = brainz_track.position
|
|
|
|
# track.length = brainz_track.length
|
|
|
|
# track.country = brainz_release.country
|
|
|
|
# track.bonus = (i == 0 ? 0 : 1)
|
|
|
|
# track.live = track_title.downcase.include? 'live'
|
|
|
|
# track.acoustic = track_title.downcase.include? 'acoustic'
|
|
|
|
# track.mbid = brainz_track.recording_id
|
|
|
|
# track.save
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# album.status = 1
|
|
|
|
# album.save unless dry_run
|
|
|
|
# end
|
|
|
|
|
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
|
2011-09-16 00:21:07 +00:00
|
|
|
artist.status
|
2011-09-14 16:03:21 +00:00
|
|
|
end
|
2011-09-16 00:21:07 +00:00
|
|
|
|
|
|
|
def self.parseLastfmXML path
|
|
|
|
Nokogiri::XML(open(path)).css('lfm > artists > artist').map do |node|
|
|
|
|
artist = {}
|
|
|
|
artist[:name] = node.css('name').text
|
|
|
|
artist[:mbid] = nil
|
|
|
|
artist[:mbid] = node.css('mbid').text unless node.css('mbid').empty?
|
|
|
|
artist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-15 02:24:01 +00:00
|
|
|
end
|