2012-08-27 03:53:30 +04:00
|
|
|
class Album < ActiveRecord::Base
|
|
|
|
belongs_to :artist
|
|
|
|
has_many :tracks
|
|
|
|
|
2012-09-01 21:55:01 +04:00
|
|
|
scope :shown, lambda {
|
|
|
|
self
|
|
|
|
.where('"albums"."year" > ?', 0)
|
|
|
|
.where(is_hidden: false)
|
2012-09-09 17:48:24 +04:00
|
|
|
.includes(:tracks)
|
|
|
|
# .group('"albums"."id"')
|
|
|
|
# .having('count("tracks"."id") > ?', 0)
|
|
|
|
# .order('"albums"."year" ASC')
|
2012-09-01 21:55:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
attr_accessible :artist_id, :pic, :rovi_id, :title, :year, :is_hidden
|
|
|
|
VA = "Various Artists"
|
2012-08-27 03:53:30 +04:00
|
|
|
|
|
|
|
def pic_safe
|
|
|
|
unless pic.nil?
|
|
|
|
pic
|
|
|
|
else
|
|
|
|
"/api/albums/#{id}/picture"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_pic
|
2012-09-09 17:48:24 +04:00
|
|
|
info = begin
|
|
|
|
response = LastFM::Album.get_info(artist: (artist.nil? ? VA : artist.name), album: title)
|
|
|
|
{ pic: response["album"]["image"][3]["#text"] }
|
|
|
|
rescue => e
|
|
|
|
{ pic: nil }
|
|
|
|
end
|
|
|
|
|
2012-08-27 03:53:30 +04:00
|
|
|
unless info[:pic].nil?
|
|
|
|
update_attributes(pic: info[:pic])
|
|
|
|
info[:pic]
|
|
|
|
else
|
|
|
|
"/assets/images/album-dummy.png"
|
|
|
|
end
|
|
|
|
end
|
2012-09-01 21:55:01 +04:00
|
|
|
|
2012-09-02 03:38:14 +04:00
|
|
|
def url
|
|
|
|
"/album/#{id}"
|
|
|
|
end
|
|
|
|
|
2012-09-01 21:55:01 +04:00
|
|
|
def import
|
|
|
|
return unless rovi_id?
|
|
|
|
|
2012-09-09 17:48:24 +04:00
|
|
|
robbie_album = Robbie::Album.find(rovi_id)
|
|
|
|
return if robbie_album.nil?
|
2012-09-01 21:55:01 +04:00
|
|
|
|
2012-09-09 17:48:24 +04:00
|
|
|
update_attributes(
|
|
|
|
title: robbie_album.title,
|
|
|
|
year: robbie_album.year,
|
|
|
|
tracks: robbie_album.tracks.each { |robbie_track|
|
|
|
|
track = Track.find_or_create_by_rovi_id(robbie_track.id)
|
2012-09-01 21:55:01 +04:00
|
|
|
track.update_attributes(
|
2012-09-09 17:48:24 +04:00
|
|
|
disc_id: robbie_track.disc_id,
|
|
|
|
position: robbie_track.position,
|
|
|
|
title: robbie_track.title,
|
|
|
|
duration: robbie_track.duration,
|
|
|
|
artists: robbie_track.artists.map { |robbie_artist|
|
|
|
|
track_artist = Artist.find_or_create_by_rovi_id(robbie_artist.id)
|
|
|
|
track_artist.update_attributes(
|
|
|
|
name: robbie_artist.name
|
|
|
|
)
|
|
|
|
track_artist
|
|
|
|
}
|
2012-09-01 21:55:01 +04:00
|
|
|
)
|
2012-09-09 17:48:24 +04:00
|
|
|
track
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self
|
2012-09-01 21:55:01 +04:00
|
|
|
end
|
2012-09-09 17:48:24 +04:00
|
|
|
|
2012-08-27 03:53:30 +04:00
|
|
|
end
|