diff --git a/Gemfile b/Gemfile index bd5370c..9a2b18d 100644 --- a/Gemfile +++ b/Gemfile @@ -8,8 +8,10 @@ gem 'haml-rails' # for using last.fm api gem 'httparty' +gem 'typhoeus' # for converting json/xml to object +gem 'json' gem 'hashie' # authentication, authorization, roles diff --git a/Gemfile.lock b/Gemfile.lock index cae9efb..13f436d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,7 +28,7 @@ GEM activemodel (= 3.0.5) activesupport (= 3.0.5) activesupport (3.0.5) - arel (2.0.9) + arel (2.0.10) builder (2.1.2) crack (0.1.8) erubis (2.6.6) @@ -43,9 +43,10 @@ GEM httparty (0.7.7) crack (= 0.1.8) i18n (0.5.0) - jquery-rails (1.0) - rails (~> 3.0) + jquery-rails (1.0.5) + railties (~> 3.0) thor (~> 0.14) + json (1.5.1) mail (2.2.19) activesupport (>= 2.3.6) i18n (>= 0.4.0) @@ -83,6 +84,9 @@ GEM thor (0.14.6) treetop (1.4.9) polyglot (>= 0.3.1) + typhoeus (0.2.4) + mime-types + mime-types tzinfo (0.3.27) youtube_it (1.4.2) builder @@ -97,8 +101,10 @@ DEPENDENCIES hashie httparty jquery-rails (>= 0.2.6) + json rails (= 3.0.5) ruby_parser sqlite3 sqlite3-ruby + typhoeus youtube_it diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index d4948a0..008d143 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -2,6 +2,8 @@ class HomeController < ApplicationController def index @tracks = LastFM::Chart.getTopTracks 25 @artists = LastFM::Chart.getTopArtists 10 + + LastFM::LastFMRequest.run end end diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index c42cf94..613858e 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -12,9 +12,9 @@ %a.right %ul.tracks.short{:count=>"5"} - @tracks.track.each_with_index do |track, i| - - id = @youtube_client.videos_by(:query => "#{track.name} #{track.artist.name} official", :page => 1, :per_page => 1, :order_by => "relevance") + -# id = @youtube_client.videos_by(:query => "#{track.name} #{track.artist.name} official", :page => 1, :per_page => 1, :order_by => "relevance") %li - = image_tag "ico/play.png", :class=>"play", :videoid=>id.videos.first.unique_id + = image_tag "ico/play.png", :class=>"play"#, :videoid=>id.videos.first.unique_id = image_tag "ico/add.png", :class=>"add" %span= track.name = link_to trimString(track.artist.name, 50), artist_path(CGI.escape(track.artist.name)) diff --git a/lib/last_fm/last_fm_request.rb b/lib/last_fm/last_fm_request.rb index 5507f7f..d6c5838 100644 --- a/lib/last_fm/last_fm_request.rb +++ b/lib/last_fm/last_fm_request.rb @@ -1,37 +1,28 @@ require 'httparty' -require 'active_support' +require 'json' require 'hashie' module LastFM class LastFMRequest include HTTParty - - base_uri "ws.audioscrobbler.com/2.0/" - parser Proc.new { |data| Hashie::Mash.new(ActiveSupport::JSON.decode(data)) } + + base_uri 'http://ws.audioscrobbler.com/2.0//' + parser Proc.new { |data| Hashie::Mash.new(JSON.parse(data)) } + @@hydra = Typhoeus::Hydra.new default_params :format => 'json', :autocorrect => '1' #debug_output $> format :json - + def self.api_key=(key) @@api_key = key default_params :api_key => key end - - def self.limit=(limit) - @limit = limit - end - - def self.limit - @limit - end - - def self.page=(page) - @page = page - end - - def self.page - @page + + def self.run + puts "<< ----------- hydra run ----------" + @@hydra.run + puts "<< ----------- hydra finished ----------" end def self.artist_request method, node, q, limit = nil, page = nil @@ -47,16 +38,54 @@ module LastFM end def self.chart_request method, node, limit = nil, page = nil - prepare_result method, node, get('/', :query => { :method => "chart.#{method.to_s}", :limit => limit, :page => page }) + # hydra = Typhoeus::Hydra.new + request = Typhoeus::Request.new(base_uri, :params => default_params.merge(:method => "chart.#{method.to_s}", :limit => limit, :page => page)) + + # handle_response method, node, request + puts '----------------------' + puts request.url + puts '----------------------' + + request.on_complete do |response| + if response.success? + puts "---- '#{method.to_s}' -- TIME: #{response.time.to_s} ----" + + hash = Hashie::Mash.new(JSON.parse(response.body)) + hash = method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) + elsif response.timed_out? + puts ">> -- '#{method.to_s}' -- TIMED OUT -- <<" + end + end + + request.on_complete + puts "---- '#{method.to_s}' request is queued ----" if @@hydra.queue request + # hydra.queue request + + # hydra.run + # prepare_result method, node, get('/', :query => { :method => "chart.#{method.to_s}", :limit => limit, :page => page }) end private - - def self.prepare_result method, node, response_hash - if method.to_s == "search" - response_hash = response_hash.results + + def self.handle_response method, node, request + puts '----------------------' + puts request.url + puts '----------------------' + request.on_complete do |response| + if response.success? + puts "---- '#{method.to_s}' -- TIME: #{response.time.to_s} ----" + + hash = Hashie::Mash.new(JSON.parse(response.body)) + method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) + puts hash + elsif response.timed_out? + puts ">> -- '#{method.to_s}' -- TIMED OUT -- <<" + end end - response_hash.send(node.to_sym) + end + + def self.prepare_result method, node, response_hash + method.to_s == "search" ? hash.results.send(node.to_sym) : hash.send(node.to_sym) end end diff --git a/public/javascripts/application.js b/public/javascripts/application.js index e9133dd..df4a44c 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -20,17 +20,17 @@ $(document).ready(function(){ initPlayer(); playTrack(); - - fitImages(".artists.big img"); // resize and resposition images (artists, album) - fitImages(".artists.small img"); - fitImages(".artists.ultrasmall img"); - fitImages("#artist .side .img img"); + }); $(window).load(function() { initSearchDropdown(); // dropdown for the search input - + + fitImages(".artists.big img"); // resize and resposition images (artists, album) + fitImages(".artists.small img"); + fitImages(".artists.ultrasmall img"); + fitImages("#artist .side .img img"); });