Work on import refactor
This commit is contained in:
parent
6c8831618e
commit
8f56a84a32
2
Gemfile
2
Gemfile
|
@ -14,7 +14,7 @@ gem 'barista'
|
|||
gem 'awesome_print', :require => 'ap'
|
||||
gem 'delayed_job'
|
||||
gem 'lastfm', :git => 'git://github.com/magnolia-fan/ruby-lastfm.git'
|
||||
gem 'musicbrainz-ruby', :git => 'git://github.com/magnolia-fan/musicbrainz-ruby.git'
|
||||
gem 'musicbrainz', '~> 0.4.3'
|
||||
|
||||
group :development do
|
||||
gem 'sqlite3'
|
||||
|
|
14
Gemfile.lock
14
Gemfile.lock
|
@ -1,11 +1,3 @@
|
|||
GIT
|
||||
remote: git://github.com/magnolia-fan/musicbrainz-ruby.git
|
||||
revision: 17d743a270b02b77251e10357af3d12cdab5f3a9
|
||||
specs:
|
||||
musicbrainz-ruby (0.4.0)
|
||||
hashie (~> 1.0)
|
||||
httparty (>= 0.7.3)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/magnolia-fan/ruby-lastfm.git
|
||||
revision: 024064d52c9fb3996664436a339a4b2ec7541d9d
|
||||
|
@ -66,7 +58,6 @@ GEM
|
|||
execjs (1.2.4)
|
||||
multi_json (~> 1.0)
|
||||
haml (3.1.3)
|
||||
hashie (1.1.0)
|
||||
hike (1.2.1)
|
||||
httparty (0.7.8)
|
||||
crack (= 0.1.8)
|
||||
|
@ -79,6 +70,9 @@ GEM
|
|||
treetop (~> 1.4.8)
|
||||
mime-types (1.16)
|
||||
multi_json (1.0.3)
|
||||
musicbrainz (0.4.1)
|
||||
nokogiri
|
||||
nokogiri (1.5.0)
|
||||
pg (0.11.0)
|
||||
polyglot (0.3.2)
|
||||
rack (1.3.2)
|
||||
|
@ -137,7 +131,7 @@ DEPENDENCIES
|
|||
haml
|
||||
json
|
||||
lastfm!
|
||||
musicbrainz-ruby!
|
||||
musicbrainz (~> 0.4.1)
|
||||
pg
|
||||
rails (= 3.1.0)
|
||||
sass
|
||||
|
|
|
@ -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
|
||||
begin
|
||||
artist_lastfm = lastfm.artist.get_info(name)
|
||||
artist_mb = brainz.artist(artist_mb_data[:mbid])
|
||||
rescue => e
|
||||
ap e.message
|
||||
ap e.backtrace
|
||||
end
|
||||
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 = 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
|
||||
# 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
|
||||
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
|
||||
end
|
||||
|
||||
begin
|
||||
|
|
Loading…
Reference in New Issue