Loading ru/en artist bio, all image sizes

This commit is contained in:
Gregory Eremin
2012-09-18 00:03:57 +04:00
parent e8fa9e4cda
commit 7cef7bfc9c
10 changed files with 137 additions and 25 deletions
+6 -14
View File
@@ -1,6 +1,7 @@
class Album < ActiveRecord::Base
belongs_to :artist
has_many :tracks
belongs_to :image # sick!
scope :shown, lambda {
self
@@ -12,7 +13,7 @@ class Album < ActiveRecord::Base
# .order('"albums"."year" ASC')
}
attr_accessible :artist_id, :pic, :rovi_id, :title, :year, :is_hidden, :tracks
attr_accessible :artist_id, :image, :rovi_id, :title, :year, :is_hidden, :tracks
VA = "Various Artists"
def pic_safe
@@ -27,20 +28,11 @@ class Album < ActiveRecord::Base
pic_safe
end
def load_pic
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
def update_image
self.image ||= Image.create
update_attributes(image: self.image.load_album_pic(artist.nil? ? VA : artist.name, title))
unless info[:pic].nil?
update_attributes(pic: info[:pic])
info[:pic]
else
"/assets/images/album-dummy.png"
end
image
end
def url
+23 -2
View File
@@ -3,16 +3,35 @@ class Artist < ActiveRecord::Base
has_many :performers
has_many :tracks, through: :performers
has_many :artist_genres
has_many :artist_infos
has_many :genres, through: :artist_genres
belongs_to :image # sick!
attr_accessible :bio, :is_group, :name, :pic, :rovi_id, :albums, :genres
attr_accessible :is_group, :name, :rovi_id, :albums, :genres, :image, :artist_infos
scope :discography, lambda {
includes(:albums).includes(:tracks)
}
def loaded?
pic? && bio?
image? && bio?
end
def update_image
self.image ||= Image.create
update_attributes(image: self.image.load_artist_pics(name))
image
end
def update_info
%w[ ru en ].each do |lang|
ArtistInfo.find_or_create_by_artist_id_and_lang(id, lang).import
end
end
def bio(lang = "en")
artist_infos.where(lang: lang.to_s).first.bio rescue ""
end
def url
@@ -41,6 +60,8 @@ class Artist < ActiveRecord::Base
genre
}
)
update_info
update_image
self
end
+14
View File
@@ -0,0 +1,14 @@
class ArtistInfo < ActiveRecord::Base
belongs_to :artist
attr_accessible :artist_id, :bio, :lang
def import
begin
info = LastFM::Artist.get_info(artist: artist.name, lang: lang)
update_attributes(bio: info["artist"]["bio"]["summary"])
true
rescue => e
false
end
end
end
+32
View File
@@ -0,0 +1,32 @@
class Image < ActiveRecord::Base
attr_accessible :sizes
serialize :sizes
def sized(kind)
sizes[kind]
end
def load_artist_pics(artist_name)
sizes = begin
response = LastFM::Artist.get_info(artist: artist_name)
Hash[response["artist"]["image"].map{ |img| [img["size"], img["#text"]] }].symbolize_keys
rescue => e
{}
end
update_attributes(sizes: sizes)
self
end
def load_album_pic(artist_name, album_name)
sizes = begin
response = LastFM::Album.get_info(artist: artist_name, album: album_name)
Hash[response["album"]["image"].map{ |img| [img["size"], img["#text"]] }].symbolize_keys
rescue => e
{}
end
update_attributes(sizes: sizes)
self
end
end