Work on import refactor
This commit is contained in:
@@ -2,40 +2,52 @@ require 'lastfm'
|
||||
require 'musicbrainz'
|
||||
|
||||
class ImportController < ApplicationController
|
||||
|
||||
@@user_agent = 'BeatHaven.org'
|
||||
|
||||
@@lastfm_api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
||||
@@lastfm_secret = '19e70e98b291e9f15d0516925945eb1b'
|
||||
|
||||
@@brainz_login = 'magnolia_fan'
|
||||
@@brainz_password = '111111'
|
||||
|
||||
def self.importArtist name, dry_run = false
|
||||
|
||||
# Initializing gems
|
||||
brainz = MusicBrainz::Client.new(@@brainz_login, @@brainz_password, @@user_agent)
|
||||
lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret)
|
||||
|
||||
artist = Artist.find_or_create_by_name(name)
|
||||
|
||||
begin
|
||||
# Get artist info
|
||||
artist_mb_data = ArtistController.musicBrainzExactSearch(name).first
|
||||
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
|
||||
begin
|
||||
artist_lastfm = lastfm.artist.get_info(name)
|
||||
artist_mb = brainz.artist(artist_mb_data[:mbid])
|
||||
rescue => e
|
||||
ap e.message
|
||||
ap e.backtrace
|
||||
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|
|
||||
|
||||
end
|
||||
|
||||
# Save artist
|
||||
artist.desc = artist_lastfm['bio']['summary']
|
||||
artist.pic_url = artist_lastfm['image'][3]['content']
|
||||
artist.artist_type = artist_mb['artist']['type']
|
||||
artist.mbid = artist_mb_data[:mbid]
|
||||
artist.save! unless dry_run
|
||||
end
|
||||
|
||||
begin
|
||||
|
||||
Reference in New Issue
Block a user