diff --git a/Gemfile b/Gemfile index 9a2b18d..4d4eda2 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ source 'http://rubygems.org' +gem 'rake', '!= 0.9.0' + gem 'rails', '3.0.5' gem 'jquery-rails', '>= 0.2.6' diff --git a/Gemfile.lock b/Gemfile.lock index e90fdd3..4dd3051 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,7 +43,7 @@ GEM httparty (0.7.7) crack (= 0.1.8) i18n (0.6.0) - jquery-rails (1.0.7) + jquery-rails (1.0.9) railties (~> 3.0) thor (~> 0.14) json (1.5.1) @@ -73,7 +73,7 @@ GEM activesupport (= 3.0.5) rake (>= 0.8.7) thor (~> 0.14.4) - rake (0.9.0) + rake (0.9.1) ruby_parser (2.0.6) sexp_processor (~> 3.0) sexp_processor (3.0.5) @@ -88,7 +88,7 @@ GEM mime-types mime-types tzinfo (0.3.27) - youtube_it (1.4.2) + youtube_it (1.4.3) builder oauth (>= 0.4.4) @@ -103,6 +103,7 @@ DEPENDENCIES jquery-rails (>= 0.2.6) json rails (= 3.0.5) + rake (!= 0.9.0) ruby_parser sqlite3 sqlite3-ruby diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 05f81ed..ce680b8 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -7,7 +7,7 @@ class ArtistsController < ApplicationController @tracks = LastFM::Artist.getTopTracks CGI.unescape(params[:id]), 10 @albums = LastFM::Artist.getTopAlbums CGI.unescape(params[:id]), 8 @images = LastFM::Artist.getImages CGI.unescape(params[:id]), 18 - @related= LastFM::Artist.getSimilar CGI.unescape(params[:id]), 6 + @related = LastFM::Artist.getSimilar CGI.unescape(params[:id]), 6 LastFM::LastFMRequest.run_queue! end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a9d72a3..5ce491a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -5,7 +5,7 @@ module ApplicationHelper @attr = (type=="gallery") ? "name" : "size" image.each do |img| if img[@attr] == size.to_s - if img["#text"].empty? + if img["#text"].blank? return "/images/placeholder/#{type.to_s}.png" end return img["#text"] diff --git a/app/views/artists/show.html.haml b/app/views/artists/show.html.haml index a2d85db..e65b496 100644 --- a/app/views/artists/show.html.haml +++ b/app/views/artists/show.html.haml @@ -28,8 +28,7 @@ %ul.songs.long - @tracks.track.each do |track| %li - - id = @youtube_client.videos_by(:query => "#{track.name} #{track.artist.name} official", :page => 1, :per_page => 1, :order_by => "relevance") - = image_tag "ico/play.png", :class=>"play", :videoid=>id.videos.first.unique_id + = image_tag "ico/play.png", :class=>"play" = image_tag "ico/add.png", :class=>"add" %span= track.name = link_to trimString(track.artist.name, 50), :controller=>"artists",:action=>"show",:id=>CGI.escape(track.artist.name) diff --git a/lib/last_fm/album.rb b/lib/last_fm/album.rb index e52c8c9..85eb966 100644 --- a/lib/last_fm/album.rb +++ b/lib/last_fm/album.rb @@ -2,7 +2,11 @@ module LastFM class Album < LastFMRequest def self.search query, limit = nil, page = nil - album_request "search", "albummatches", query, nil, limit, page + album_request "search", "albummatches", query, nil, limit, page do |result| + result = LastFM::Validator.validate_mash result + result = Hashie::Mash.new({ :album => [] }) if result.blank? + update_results "#{current_class_name}_#{__method__}", result + end end # return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes ) diff --git a/lib/last_fm/artist.rb b/lib/last_fm/artist.rb index cbd0b35..7444868 100644 --- a/lib/last_fm/artist.rb +++ b/lib/last_fm/artist.rb @@ -1,13 +1,20 @@ module LastFM class Artist < LastFMRequest + def self.search query, limit = nil, page = nil - artist_request "search", "artistmatches", query, limit, page + artist_request "search", "artistmatches", query, limit, page do |result| + result = LastFM::Validator.validate_mash result + result = Hashie::Mash.new({ :artist => [] }) if result.blank? + update_results "#{current_class_name}_#{__method__}", result + end end # return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes ) def self.getInfo artist - artist_request "getInfo", "artist", artist + artist_request "getInfo", "artist", artist do |info| + LastFM::Validator.validate_mash info, "tag", "image" + end end # return: artists ( name, mbid, image urls ( different sizes ) ) @@ -28,14 +35,18 @@ module LastFM # return: albums ( name, mbid, album url, image urls ( different sizes ) ) ordered by popularity def self.getTopAlbums artist, limit = nil, page = nil artist_request "getTopAlbums", "topalbums", artist, limit, page do |albums| - if albums["@attr"].total.to_i == 1 - albums.album = [albums.album] # wrap with Array - end + albums = LastFM::Validator.validate_mash albums, "album" + albums = Hashie::Mash.new({ :album => [] }) if albums.total == "0" + update_results "#{current_class_name}_#{__method__}", albums end end def self.getImages artist, limit = nil - artist_request "getImages", "images", artist, limit + artist_request "getImages", "images", artist, limit do |images| + images = LastFM::Validator.validate_mash images, "image" + images = Hashie::Mash.new({ :image => [] }) if images.total == "0" + update_results "#{current_class_name}_#{__method__}", images + end end end diff --git a/lib/last_fm/chart.rb b/lib/last_fm/chart.rb index aaff786..9476c57 100644 --- a/lib/last_fm/chart.rb +++ b/lib/last_fm/chart.rb @@ -1,11 +1,18 @@ module LastFM + class Chart < LastFMRequest def self.getTopArtists limit = nil, page = nil - chart_request "getTopArtists", "artists", limit, page + chart_request "getTopArtists", "artists", limit, page do |artists| + LastFM::Validator.validate_mash artists, "image" + update_results "#{current_class_name}_#{__method__}", artists + end end - + def self.getTopTracks limit = nil, page = nil - chart_request "getTopTracks", "tracks", limit, page + chart_request "getTopTracks", "tracks", limit, page do |tracks| + LastFM::Validator.validate_mash tracks + update_results "#{current_class_name}_#{__method__}", tracks + end end end diff --git a/lib/last_fm/last_fm_request.rb b/lib/last_fm/last_fm_request.rb index 4fee99e..2e0f0fd 100644 --- a/lib/last_fm/last_fm_request.rb +++ b/lib/last_fm/last_fm_request.rb @@ -4,12 +4,12 @@ require 'hashie' module LastFM class LastFMRequest - + @@base_uri = 'http://ws.audioscrobbler.com/2.0/' @@default_params = { :format => 'json', :autocorrect => '1' } @@hydra = Typhoeus::Hydra.new @@results = {} - + def self.api_key=(key) @@api_key = key @@default_params[:api_key] = key @@ -18,15 +18,15 @@ module LastFM def self.base_uri return @@base_uri end - + def self.default_params return @@default_params end - + def self.results return @@results end - + def self.run_queue! puts "<< ----------- started request queue ---------" @@hydra.run @@ -34,52 +34,59 @@ module LastFM end def self.artist_request method, node, q, limit = nil, page = nil, &block - klass_name = self.name.split('::').last.downcase! - request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :artist => q, :limit => limit, :page => page ) - create_and_queue_request klass_name, method, node, block, request_params + request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :artist => q, :limit => limit, :page => page ) + create_and_queue_request method, node, block, request_params end def self.album_request method, node, q, artist = nil, limit = nil, page = nil, &block - klass_name = self.name.split('::').last.downcase! - request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page ) - create_and_queue_request klass_name, method, node, block, request_params + request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page ) + create_and_queue_request method, node, block, request_params end def self.track_request method, node, q, artist = nil, limit = nil, page = nil, &block - klass_name = self.name.split('::').last.downcase! - request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page ) - create_and_queue_request klass_name, method, node, block, request_params + request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page ) + create_and_queue_request method, node, block, request_params end - + def self.chart_request method, node, limit = nil, page = nil, &block - klass_name = self.name.split('::').last.downcase! - request_params = default_params.merge( :method => "#{klass_name}.#{method.to_s}", :limit => limit, :page => page ) - create_and_queue_request klass_name, method, node, block, request_params + request_params = default_params.merge( :method => "#{current_class_name}.#{method.to_s}", :limit => limit, :page => page ) + create_and_queue_request method, node, block, request_params + end + + protected + + def self.update_results key, hash + @@results[(key.to_sym)].update hash + end + + def self.current_class_name + self.name.split('::').last.downcase! end private - - def self.create_and_queue_request klass_name, method, node, block, request_params + + def self.create_and_queue_request method, node, block, request_params request = Typhoeus::Request.new(base_uri, :params => request_params) - handle_response request, klass_name, method, node, block + handle_response request, method, node, block - puts ">> ---- '#{klass_name}.#{method.to_s}' request is queued ----" if @@hydra.queue request - @@results[(klass_name + "_" + method).to_sym] = Hashie::Mash.new + puts ">> ---- '#{current_class_name}.#{method.to_s}' request is queued ----" if @@hydra.queue request + @@results[(current_class_name + "_" + method).to_sym] = Hashie::Mash.new end - - def self.handle_response request, klass_name, method, node, block + + def self.handle_response request, method, node, block request.on_complete do |response| if response.success? - puts "<< ---- '#{klass_name}.#{method.to_s}' -- TIME: #{response.time.to_s} ----" + puts "<< ---- '#{current_class_name}.#{method.to_s}' -- TIME: #{response.time.to_s} ----" - hash = Hashie::Mash.new(JSON.parse(response.body)) + hash = Hashie::Mash.new(JSON.parse(response.body)) + puts hash hash = method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) - @@results[(klass_name + "_" + method).to_sym].update hash + update_results "#{current_class_name}_#{method}", hash unless hash.blank? - block.call(@@results[(klass_name + "_" + method).to_sym]) unless block.nil? + block.call(@@results[("#{current_class_name}_#{method}").to_sym]) unless block.nil? elsif response.timed_out? - puts ">> -- '#{klass_name}.#{method.to_s}' -- TIMED OUT -- <<" + puts ">> -- '#{current_class_name}.#{method.to_s}' -- TIMED OUT -- <<" end end end diff --git a/lib/last_fm/track.rb b/lib/last_fm/track.rb index 430a9b8..4b596e9 100644 --- a/lib/last_fm/track.rb +++ b/lib/last_fm/track.rb @@ -2,7 +2,11 @@ module LastFM class Track < LastFMRequest def self.search query, limit = nil, page = nil - track_request "search", "trackmatches", query, nil, limit, page + track_request "search", "trackmatches", query, nil, limit, page do |result| + result = LastFM::Validator.validate_mash result + result = Hashie::Mash.new({ :track => [] }) if result.blank? + update_results "#{current_class_name}_#{__method__}", result + end end # return: track name, id, (top)tags, artist, duration (in msec), album ( artist, title, image urls ( different sizes) ), mbid diff --git a/lib/last_fm/validator.rb b/lib/last_fm/validator.rb new file mode 100644 index 0000000..be2ea33 --- /dev/null +++ b/lib/last_fm/validator.rb @@ -0,0 +1,28 @@ +module LastFM + + class Validator + + def self.validate_mash mash, *args + args.flatten! + + return [] if mash.blank? + + mash.each do |key, value| + if args.include? key.to_s || value.is_a?(Array) + mash[key] = Array.wrap value + mash[key].map do |mashie| + validate_mash mashie, args + end + elsif value.is_a? Hashie::Mash + mash[key] = validate_mash mash[key], args + end + mash[key] = "" if value.blank? && value.is_a?(String) + mash[key] = [] if value.blank? && value.is_a?(Array) + end + + return mash + end + + end + +end \ No newline at end of file