Last.fm mega import tasks
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
require 'lastfm'
|
||||
require 'musicbrainz'
|
||||
require 'nokogiri'
|
||||
|
||||
class ImportController < ApplicationController
|
||||
@@lastfm_api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
||||
@@ -10,15 +11,21 @@ class ImportController < ApplicationController
|
||||
# Initializing gems
|
||||
lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret)
|
||||
|
||||
artist = Artist.find_by_name(name)
|
||||
artist = Artist.find_or_create_by_name(name)
|
||||
return 3 if artist.status == 1
|
||||
|
||||
begin
|
||||
lastfm_artist = lastfm.artist.get_info(name)
|
||||
brainz_artist = MusicBrainz::Artist.find_by_name(name)
|
||||
unless artist.mbid.nil?
|
||||
brainz_artist = MusicBrainz::Artist.find(artist.mbid)
|
||||
else
|
||||
brainz_artist = MusicBrainz::Artist.find_by_name(name)
|
||||
end
|
||||
rescue => e
|
||||
lastfm_artist = {
|
||||
'bio' => { 'summary' => '' },
|
||||
'image' => [ nil, nil, nil, { 'content' => '' } ]
|
||||
'image' => [ nil, nil, nil, { 'content' => '' } ],
|
||||
'stats' => { 'listeners' => 0 }
|
||||
}
|
||||
ap e.message
|
||||
ap e.backtrace
|
||||
@@ -28,6 +35,7 @@ class ImportController < ApplicationController
|
||||
# Save artist
|
||||
artist.desc = lastfm_artist['bio']['summary']
|
||||
artist.pic_url = lastfm_artist['image'][3]['content']
|
||||
artist.listeners = lastfm_artist['stats']['listeners']
|
||||
artist.artist_type = brainz_artist.type
|
||||
artist.mbid = brainz_artist.id
|
||||
dry_run ? ap(artist) : artist.save
|
||||
@@ -85,5 +93,17 @@ class ImportController < ApplicationController
|
||||
end
|
||||
|
||||
artist.save unless dry_run
|
||||
artist.status
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user