1
0
Fork 0

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

View File

@ -21,7 +21,7 @@ class BeatHaven.Models.Player extends Backbone.Model
this.play_something() this.play_something()
else else
if @current_track? and @current_track == track if @current_track? and @current_track == track
@current_track.get("sm_obj").resume() @current_track.get("sm_obj").play()
else else
if @current_track? if @current_track?
@current_track.get("sm_obj").stop() @current_track.get("sm_obj").stop()

View File

@ -1,6 +1,7 @@
class Album < ActiveRecord::Base class Album < ActiveRecord::Base
belongs_to :artist belongs_to :artist
has_many :tracks has_many :tracks
belongs_to :image # sick!
scope :shown, lambda { scope :shown, lambda {
self self
@ -12,7 +13,7 @@ class Album < ActiveRecord::Base
# .order('"albums"."year" ASC') # .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" VA = "Various Artists"
def pic_safe def pic_safe
@ -27,20 +28,11 @@ class Album < ActiveRecord::Base
pic_safe pic_safe
end end
def load_pic def update_image
info = begin self.image ||= Image.create
response = LastFM::Album.get_info(artist: (artist.nil? ? VA : artist.name), album: title) update_attributes(image: self.image.load_album_pic(artist.nil? ? VA : artist.name, title))
{ pic: response["album"]["image"][3]["#text"] }
rescue => e
{ pic: nil }
end
unless info[:pic].nil? image
update_attributes(pic: info[:pic])
info[:pic]
else
"/assets/images/album-dummy.png"
end
end end
def url def url

View File

@ -3,16 +3,35 @@ class Artist < ActiveRecord::Base
has_many :performers has_many :performers
has_many :tracks, through: :performers has_many :tracks, through: :performers
has_many :artist_genres has_many :artist_genres
has_many :artist_infos
has_many :genres, through: :artist_genres 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 { scope :discography, lambda {
includes(:albums).includes(:tracks) includes(:albums).includes(:tracks)
} }
def loaded? 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 end
def url def url
@ -41,6 +60,8 @@ class Artist < ActiveRecord::Base
genre genre
} }
) )
update_info
update_image
self self
end end

14
app/models/artist_info.rb Normal file
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
app/models/image.rb Normal file
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

View File

@ -0,0 +1,11 @@
class CreateArtistInfos < ActiveRecord::Migration
def change
create_table :artist_infos do |t|
t.integer :artist_id
t.string :lang
t.text :bio
t.timestamps
end
end
end

View File

@ -0,0 +1,9 @@
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.text :sizes
t.timestamps
end
end
end

View File

@ -0,0 +1,19 @@
class RemoveArtistBioAndImageFields < ActiveRecord::Migration
def up
remove_column :artists, :bio
remove_column :artists, :pic
remove_column :albums, :pic
add_column :artists, :image_id, :integer, default: nil
add_column :albums, :image_id, :integer, default: nil
end
def down
add_column :artists, :bio, :text, default: nil
add_column :artists, :pic, :string, default: nil
add_column :albums, :pic, :string, default: nil
remove_column :artists, :image_id
remove_column :albums, :image_id
end
end

View File

@ -11,17 +11,17 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120901191655) do ActiveRecord::Schema.define(:version => 20120917185417) do
create_table "albums", :force => true do |t| create_table "albums", :force => true do |t|
t.integer "artist_id" t.integer "artist_id"
t.string "rovi_id" t.string "rovi_id"
t.string "title" t.string "title"
t.integer "year" t.integer "year"
t.string "pic"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.boolean "is_hidden", :default => false t.boolean "is_hidden", :default => false
t.integer "image_id"
end end
create_table "artist_genres", :force => true do |t| create_table "artist_genres", :force => true do |t|
@ -31,14 +31,21 @@ ActiveRecord::Schema.define(:version => 20120901191655) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
create_table "artist_infos", :force => true do |t|
t.integer "artist_id"
t.string "lang"
t.text "bio"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "artists", :force => true do |t| create_table "artists", :force => true do |t|
t.string "rovi_id" t.string "rovi_id"
t.string "name" t.string "name"
t.boolean "is_group" t.boolean "is_group"
t.text "bio"
t.string "pic"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.integer "image_id"
end end
create_table "genres", :force => true do |t| create_table "genres", :force => true do |t|
@ -48,6 +55,12 @@ ActiveRecord::Schema.define(:version => 20120901191655) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
end end
create_table "images", :force => true do |t|
t.text "sizes"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "performers", :force => true do |t| create_table "performers", :force => true do |t|
t.integer "track_id" t.integer "track_id"
t.integer "artist_id" t.integer "artist_id"

View File

@ -14,13 +14,14 @@ class window.VkMusic
query = this.prepareQuery artist, track query = this.prepareQuery artist, track
if @query_results[query]? and not return_all if @query_results[query]? and not return_all
callback @query_results[query] callback @query_results[query]
that = this self = this
VK.Api.call 'audio.search', q: query, (r) -> VK.Api.call 'audio.search', q: query, auto_complete: 1, sort: 2, (r) ->
results = that.range r.response, artist, track, duration results = r.response #self.range r.response, artist, track, duration
results.splice(0, 1)
top_result = null top_result = null
if results.length > 0 if results.length > 0
top_result = results[0].url top_result = results[0].url
that.query_results[query] = results self.query_results[query] = results
callback if return_all then results else top_result callback if return_all then results else top_result
range: (data, artist, track, duration) -> range: (data, artist, track, duration) ->