This commit is contained in:
Tobias Klika
2011-05-04 16:09:37 +02:00
12 changed files with 178 additions and 142 deletions
+9 -9
View File
@@ -1,14 +1,14 @@
class SearchController < ApplicationController
def index
if !params[:search_term].nil?
puts "TRACK SEARCH"
@results = LastFM::Artist.search(params[:search_term].to_s.lstrip)
@resultsArtists = @results["results"]
@results = LastFM::Track.search(params[:search_term].to_s.lstrip)
@resultsTracks = @results["results"]
@results = LastFM::Album.search(params[:search_term].to_s.lstrip)
@resultsAlbums = @results["results"]
def index
if params[:q].nil?
params[:q] = "foobar"
end
if !params[:q].nil?
@artists = LastFM::Artist.search(params[:q].to_s.lstrip, 18)
@tracks = LastFM::Track.search(params[:q].to_s.lstrip, 24)
@albums = LastFM::Album.search(params[:q].to_s.lstrip, 16)
end
end
+6 -1
View File
@@ -1,3 +1,4 @@
# encoding: UTF-8
module ApplicationHelper
def validate_img_url image, size
@@ -16,5 +17,9 @@ module ApplicationHelper
@result = LastFM::Album.getInfo(album, artist)
@result.album
end
def trimString str, length
truncate str, :length => length.to_i, :omission => ""
end
end
end
+1 -1
View File
@@ -10,7 +10,7 @@
#head
%h1
= image_tag "9minutes.png"
- form_tag search_index_path, :method => 'get', :id => "search_form", :class => "right" do
= form_tag search_index_path, :method => 'get', :id => "search_form", :class => "right" do
.wrap
= text_field_tag 'search_term', params[:search_term], :class => "input", :placeholder => "Search for"
-# TODO: image_submit_tag
+17 -15
View File
@@ -1,5 +1,5 @@
%h2.headline
=raw "Search for &#132;#{params[:search_term]}&#147;"
=raw "Search for &#132;#{params[:q]}&#147;"
%ul.navigation{:steering => "search"}
%li.active ALL
%li ARTISTS
@@ -11,7 +11,7 @@
#search_all_artists
%ul.artists
- count = 0
- @resultsArtists.artistmatches.artist.each_with_index do |artist, i|
- @artists.artist.each_with_index do |artist, i|
- break if (i >= 6 && count >= 6)
- next unless artist.listeners.to_i > 15
- count += 1
@@ -19,37 +19,38 @@
= link_to "LINK" do
= image_tag validate_img_url(artist.image, :large)
%p
= artist.name
= trimString(artist.name, 22)
%br.clean
#search_all_songs
%h2.headline.small Songs
%ul.songs
- @resultsTracks.trackmatches.track.each_with_index do |track, i|
- @tracks.track.each_with_index do |track, i|
- break if i == 10
%li
= image_tag "ico/play.png", :class=>"play"
= image_tag "ico/add.png", :class=>"add"
%span
= track.name
=raw "&nbsp;by #{track.artist}"
=raw "&nbsp;"
= "by #{trimString(track.artist, 50)}"
#search_all_albums
%h2.headline.small Albums
%ul.albums
- @resultsAlbums.albummatches.album.each_with_index do |album, i|
- @albums.album.each_with_index do |album, i|
- break if i == 8
%li
.img
= image_tag validate_img_url(album.image, :medium)
%p
%span
= album.name
= trimString(album.name, 38)
%br
= album.artist
= trimString(album.artist, 22)
%sub
#search_artists
%ul.artists
- count = 0
- @resultsArtists.artistmatches.artist.each_with_index do |artist, i|
- @artists.artist.each_with_index do |artist, i|
- break if (i >= 18 && count >= 18)
- next unless artist.listeners.to_i > 15
- count += 1
@@ -58,36 +59,37 @@
= link_to "LINK" do
= image_tag validate_img_url(artist.image, :large)
%p
= artist.name
= trimString(artist.name, 22)
%br.clean
= link_to "NACHLADEN", :class => "more" do
= image_tag "ico/more.png"
#search_albums
%ul.albums
- @resultsAlbums.albummatches.album.each_with_index do |album, i|
- @albums.album.each_with_index do |album, i|
- break if i == 16
%li
.img
= image_tag validate_img_url(album.image, :medium)
%p
%span
= album.name
= trimString(album.name, 38)
%br
= album.artist
= trimString(album.artist, 22)
%sub
%br.clean
= link_to "NACHLADEN", :class => "more" do
= image_tag "ico/more.png"
#search_songs
%ul.songs.tab
- @resultsTracks.trackmatches.track.each_with_index do |track, i|
- @tracks.track.each_with_index do |track, i|
- break if i == 24
%li
= image_tag "ico/play.png", :class=>"play"
= image_tag "ico/add.png", :class=>"add"
%span
= track.name
=raw "&nbsp;by #{track.artist}"
=raw "&nbsp;"
= "by #{trimString(track.artist, 50)}"
%br.clean
%br
= link_to "NACHLADEN", :class => "more" do
+2 -2
View File
@@ -13,8 +13,8 @@ module Nineminutes
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/lib/LastFM)
# config.autoload_paths += Dir["#{config.root}/lib/LastFM"]
# config.autoload_paths += %W(#{config.root}/lib)
# config.autoload_paths += Dir["#{config.root}/lib/LastFM/"]
# config.autoload_paths << config.root.join('lib')
# Only load the plugins named here, in the order given (default is alphabetical).
+6 -1
View File
@@ -1,6 +1,11 @@
Nineminutes::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Reloads modules and files in lib directory
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
+5 -3
View File
@@ -1,4 +1,6 @@
require "#{Rails.root.to_s}/lib/LastFM/LastFMRequest.rb"
require "#{Rails.root.to_s}/lib/LastFM/LastFM.rb"
require 'LastFM/LastFMRequest'
require 'LastFM/Artist'
require 'LastFM/Album'
require 'LastFM/Track'
LastFMRequest::api_key = '4c32e360f68553ec8fdca3711456b4f9'
LastFM::LastFMRequest::api_key = '4c32e360f68553ec8fdca3711456b4f9'
+20
View File
@@ -0,0 +1,20 @@
module LastFM
class Album < LastFMRequest
def self.search query, limit = nil, page = nil
album_request "search", "albummatches", query, nil, limit, page
end
# return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes )
def self.getInfo album, artist
album_request "getInfo", nil, album, artist
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags album, artist
album_request "getTopTags", "toptags", album, artist
end
end
end
+35
View File
@@ -0,0 +1,35 @@
module LastFM
class Artist < LastFMRequest
def self.search query, limit = nil, page = nil
artist_request "search", "artistmatches", query, limit, page
end
# return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes )
def self.getInfo artist
artist_request "getInfo", nil, artist
end
# return: artists ( name, mbid, image urls ( different sizes ) )
def self.getSimilar artist, limit = nil
artist_request "getSimilar", "similarartists", artist, limit
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags artist
artist_request "getTopTags", "toptags", artist
end
# return: tracks ( name, image urls ( different sizes ) ) ordered by popularity
def self.getTopTracks artist, limit = nil, page = nil
artist_request "getTopTracks", "toptracks", artist, limit, page
end
# 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
end
end
end
-76
View File
@@ -1,76 +0,0 @@
module LastFM
class Artist < LastFMRequest
def self.search artist
get('/', :query => {:method => 'artist.search', :artist => artist})
end
# return: artist name, tags, similar artists, artist bio (summary / long), mbid, image urls ( different sizes )
def self.getInfo artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'artist.getInfo', :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
# return: artists ( name, mbid, image urls ( different sizes ) )
def self.getSimilar artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'artist.getSimilar', :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'artist.getTopTags', :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
# return: tracks ( name, image urls ( different sizes ) ) ordered by popularity
def self.getTopTracks artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'artist.getTopTracks', :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
# return: albums ( name, mbid, album url, image urls ( different sizes ) ) ordered by popularity
def self.getTopAlbums artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'artist.getTopAlbums', :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
end
class Album < LastFMRequest
def self.search album
get('/', :query => {:method => 'album.search', :album => album})
end
# return: album name, artist, (top)tags, tracks ( name, duration (in sec) ) mbid (album), release date, image urls ( different sizes )
def self.getInfo album = nil, artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'album.getInfo', :album =>album, :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags album = nil, artist = nil, mbid = nil, autocorrect = 1
get('/', :query => {:method => 'album.getTopTags', :album =>album, :artist => artist, :mbid => mbid, :autocorrect => autocorrect})
end
end
class Track < LastFMRequest
def self.search track
get('/', :query => {:method => 'track.search', :track => track})
end
# return: track name, id, (top)tags, artist, duration (in msec), album ( artist, title, image urls ( different sizes) ), mbid
def self.getInfo track = nil, artist = nil, autocorrect = 1
get('/', :query => {:method => 'track.getInfo', :track => track, :artist => artist, :autocorrect => autocorrect})
end
# return: tracks ( name, artist (name, mbid), duration (in msec), image urls (sometimes) )
def self.getSimilar track = nil, artist = nil, autocorrect = 1
get('/', :query => {:method => 'track.getSimilar', :track => track, :artist => artist, :autocorrect => autocorrect})
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags track = nil, artist = nil, autocorrect = 1
get('/', :query => {:method => 'track.getTopTags', :track => track, :artist => artist, :autocorrect => autocorrect})
end
end
end
+53 -34
View File
@@ -2,40 +2,59 @@ require 'httparty'
require 'active_support'
require 'hashie'
class LastFMRequest
include HTTParty
base_uri "ws.audioscrobbler.com/2.0/"
parser Proc.new { |data| Hashie::Mash.new(ActiveSupport::JSON.decode(data)) }
default_params :format => 'json'
debug_output $>
format :json
module LastFM
def self.api_key=(key)
@@api_key = key
default_params :api_key => key
end
class LastFMRequest
include HTTParty
def self.api_key
@@api_key
base_uri "ws.audioscrobbler.com/2.0/"
parser Proc.new { |data| Hashie::Mash.new(ActiveSupport::JSON.decode(data)) }
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
end
def self.artist_request method, node, q, limit = nil, page = nil
prepare_result method, node, get('/', :query => { :method => "artist.#{method.to_s}", :artist => q, :limit => limit, :page => page })
end
def self.album_request method, node, q, artist = nil, limit = nil, page = nil
prepare_result method, node, get('/', :query => { :method => "album.#{method.to_s}", :album => q, :artist => artist, :limit => limit, :page => page })
end
def self.track_request method, node, q, artist = nil, limit = nil, page = nil
prepare_result method, node, get('/', :query => { :method => "track.#{method.to_s}", :track => q, :artist => artist, :limit => limit, :page => page })
end
private
def self.prepare_result method, node, response_hash
if method.to_s == "search"
response_hash = response_hash.results
end
response_hash.send(node.to_sym)
end
end
def self.limit=(limit)
@limit = limit
default_params :limit => limit
end
def self.limit
@limit
end
def self.page=(page)
@page = page
default_params :page => page
end
def self.page
@page
end
end
end
+24
View File
@@ -0,0 +1,24 @@
module LastFM
class Track < LastFMRequest
def self.search query, limit = nil, page = nil
track_request "search", "trackmatches", query, nil, limit, page
end
# return: track name, id, (top)tags, artist, duration (in msec), album ( artist, title, image urls ( different sizes) ), mbid
def self.getInfo track, artist
track_request "getInfo", nil, track, artist
end
# return: tracks ( name, artist (name, mbid), duration (in msec), image urls (sometimes) )
def self.getSimilar track, artist, limit = nil
track_request "getSimilar", "similartracks", track, artist, limit
end
# return: tags ( name, tag´s last.fm-url ) ordered by popularity
def self.getTopTags track, artist
track_request "getTopTags", "toptags", track, artist
end
end
end