86 lines
2.9 KiB
Ruby
86 lines
2.9 KiB
Ruby
require 'lastfm'
|
|
require 'musicbrainz'
|
|
|
|
class ImportController < ApplicationController
|
|
@@lastfm_api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
|
@@lastfm_secret = '19e70e98b291e9f15d0516925945eb1b'
|
|
|
|
def self.importArtist name, dry_run = false
|
|
|
|
# Initializing gems
|
|
lastfm = Lastfm.new(@@lastfm_api_key, @@lastfm_secret)
|
|
|
|
artist = Artist.find_by_name(name)
|
|
|
|
begin
|
|
lastfm_artist = lastfm.artist.get_info(name)
|
|
brainz_artist = MusicBrainz::Artist.find_by_name(name)
|
|
rescue => e
|
|
lastfm_artist = {
|
|
'bio' => { 'summary' => '' },
|
|
'image' => [ nil, nil, nil, { 'content' => '' } ]
|
|
}
|
|
ap e.message
|
|
ap e.backtrace
|
|
end
|
|
|
|
begin
|
|
# 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
|
|
dry_run ? ap(artist) : artist.save
|
|
|
|
brainz_artist.release_groups.each do |brainz_release_group|
|
|
# Saving album
|
|
begin
|
|
album_lastfm = lastfm.album.get_info(lastfm_artist['name'], brainz_release_group.title)
|
|
album_image = album_lastfm['image'][3]['content']
|
|
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?)
|
|
dry_run ? ap(album) : album.save
|
|
# Tracks from the first release
|
|
tracks_hashed = []
|
|
brainz_release_group.releases.each_with_index do |brainz_release, i|
|
|
# Processing tracks
|
|
brainz_release.tracks.each do |brainz_track|
|
|
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
|
|
dry_run ? ap(track) : track.save
|
|
end
|
|
end
|
|
end
|
|
|
|
artist.status = 1
|
|
rescue => e
|
|
ap e.message
|
|
ap e.backtrace
|
|
artist.status = 2
|
|
end
|
|
|
|
artist.save unless dry_run
|
|
end
|
|
end |