From 7cef7bfc9c380e9fc49219d17d5637654eaf6fb8 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Tue, 18 Sep 2012 00:03:57 +0400 Subject: [PATCH] Loading ru/en artist bio, all image sizes --- .../backbone/models/player.js.coffee | 2 +- app/models/album.rb | 20 ++++-------- app/models/artist.rb | 25 +++++++++++++-- app/models/artist_info.rb | 14 ++++++++ app/models/image.rb | 32 +++++++++++++++++++ .../20120917185226_create_artist_infos.rb | 11 +++++++ db/migrate/20120917185345_create_images.rb | 9 ++++++ ...5417_remove_artist_bio_and_image_fields.rb | 19 +++++++++++ db/schema.rb | 21 +++++++++--- vendor/assets/javascripts/vk_music.js.coffee | 9 +++--- 10 files changed, 137 insertions(+), 25 deletions(-) create mode 100644 app/models/artist_info.rb create mode 100644 app/models/image.rb create mode 100644 db/migrate/20120917185226_create_artist_infos.rb create mode 100644 db/migrate/20120917185345_create_images.rb create mode 100644 db/migrate/20120917185417_remove_artist_bio_and_image_fields.rb diff --git a/app/assets/javascripts/backbone/models/player.js.coffee b/app/assets/javascripts/backbone/models/player.js.coffee index 5538ce4..aa686e3 100644 --- a/app/assets/javascripts/backbone/models/player.js.coffee +++ b/app/assets/javascripts/backbone/models/player.js.coffee @@ -21,7 +21,7 @@ class BeatHaven.Models.Player extends Backbone.Model this.play_something() else if @current_track? and @current_track == track - @current_track.get("sm_obj").resume() + @current_track.get("sm_obj").play() else if @current_track? @current_track.get("sm_obj").stop() diff --git a/app/models/album.rb b/app/models/album.rb index babcbef..c922d3f 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -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 diff --git a/app/models/artist.rb b/app/models/artist.rb index 87ab79b..f8e59e8 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -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 diff --git a/app/models/artist_info.rb b/app/models/artist_info.rb new file mode 100644 index 0000000..c27f88f --- /dev/null +++ b/app/models/artist_info.rb @@ -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 diff --git a/app/models/image.rb b/app/models/image.rb new file mode 100644 index 0000000..c55babb --- /dev/null +++ b/app/models/image.rb @@ -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 diff --git a/db/migrate/20120917185226_create_artist_infos.rb b/db/migrate/20120917185226_create_artist_infos.rb new file mode 100644 index 0000000..957b3ce --- /dev/null +++ b/db/migrate/20120917185226_create_artist_infos.rb @@ -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 diff --git a/db/migrate/20120917185345_create_images.rb b/db/migrate/20120917185345_create_images.rb new file mode 100644 index 0000000..ce27692 --- /dev/null +++ b/db/migrate/20120917185345_create_images.rb @@ -0,0 +1,9 @@ +class CreateImages < ActiveRecord::Migration + def change + create_table :images do |t| + t.text :sizes + + t.timestamps + end + end +end diff --git a/db/migrate/20120917185417_remove_artist_bio_and_image_fields.rb b/db/migrate/20120917185417_remove_artist_bio_and_image_fields.rb new file mode 100644 index 0000000..cc9a1a1 --- /dev/null +++ b/db/migrate/20120917185417_remove_artist_bio_and_image_fields.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 6b9fb00..3ecf376 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,17 +11,17 @@ # # 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| t.integer "artist_id" t.string "rovi_id" t.string "title" t.integer "year" - t.string "pic" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.boolean "is_hidden", :default => false + t.integer "image_id" end create_table "artist_genres", :force => true do |t| @@ -31,14 +31,21 @@ ActiveRecord::Schema.define(:version => 20120901191655) do t.datetime "updated_at", :null => false 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| t.string "rovi_id" t.string "name" t.boolean "is_group" - t.text "bio" - t.string "pic" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.integer "image_id" end create_table "genres", :force => true do |t| @@ -48,6 +55,12 @@ ActiveRecord::Schema.define(:version => 20120901191655) do t.datetime "updated_at", :null => false 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| t.integer "track_id" t.integer "artist_id" diff --git a/vendor/assets/javascripts/vk_music.js.coffee b/vendor/assets/javascripts/vk_music.js.coffee index 90a5f9e..47079fb 100644 --- a/vendor/assets/javascripts/vk_music.js.coffee +++ b/vendor/assets/javascripts/vk_music.js.coffee @@ -14,13 +14,14 @@ class window.VkMusic query = this.prepareQuery artist, track if @query_results[query]? and not return_all callback @query_results[query] - that = this - VK.Api.call 'audio.search', q: query, (r) -> - results = that.range r.response, artist, track, duration + self = this + VK.Api.call 'audio.search', q: query, auto_complete: 1, sort: 2, (r) -> + results = r.response #self.range r.response, artist, track, duration + results.splice(0, 1) top_result = null if results.length > 0 top_result = results[0].url - that.query_results[query] = results + self.query_results[query] = results callback if return_all then results else top_result range: (data, artist, track, duration) ->