module Firstfm class Artist attr_accessor :name, :mbid, :url, :listeners, :streamable, :images, :playcount include HTTParty base_uri 'ws.audioscrobbler.com' format :xml parser( Proc.new do |body, format| Crack::XML.parse(body) end ) def initialize(params = {}) @name = params[:name] end def get_tags name_params = self.mbid.nil? ? {:artist => self.name} : {:mbid => self.mbid} response = self.class.get("/2.0/", {:query => {:method => 'artist.getInfo', :api_key => Firstfm.config.api_key}.merge(name_params)}) tags_array = (response["lfm"] and response["lfm"]["artist"] and response["lfm"]["artist"]["tags"] and response["lfm"]["artist"]["tags"]["tag"]) || [] tags_array.is_a?(Array) ? tags_array.map {|t| t["name"] } : [] end def self.get_tags(artist) self.new(name: artist).get_tags end def get_images(page = 1, limit = 50) name_params = self.mbid.nil? ? {:artist => self.name} : {:mbid => self.mbid} response = self.class.get("/2.0/", {:query => {:method => 'artist.getImages', :page => page, :limit => limit, :api_key => Firstfm.config.api_key}.merge(name_params)}) images_array = (response["lfm"] and response["lfm"]["images"] and response["lfm"]["images"]["image"]) || [] images = Image.init_from_array(images_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace images pager.total_entries = response["lfm"]["images"]["total"].to_i rescue 0 end end def self.search(artist, page = 1, limit = 50) response = get("/2.0/", {:query => {:method => 'artist.search', :artist => artist, :page => page, :limit => limit, :api_key => Firstfm.config.api_key}}) artists_array = (response and response["lfm"] and response["lfm"]["results"] and response["lfm"]["results"]["artistmatches"] and response["lfm"]["results"]["artistmatches"]["artist"]) || [] artists = Artist.init_from_array(artists_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace artists pager.total_entries = response['lfm']['results']['opensearch:totalResults'].to_i rescue 0 end end def self.get_top_tracks(artist, page = 1, limit = 50) response = get("/2.0/", {:query => {:method => 'artist.getTopTracks', :artist => artist, :page => page, :limit => limit, :api_key => Firstfm.config.api_key}}) tracks_array = (response and response["lfm"] and response["lfm"]["toptracks"] and response["lfm"]["toptracks"]["track"]) || [] tracks = Track.init_from_array(tracks_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace tracks pager.total_entries = response["lfm"]["toptracks"]["total"].to_i rescue 0 end end def self.get_correction(artist) response = get("/2.0/", {:query => {:method => 'artist.getcorrection', :artist => artist, :api_key => Firstfm.config.api_key}}) if response && response["lfm"] && response["lfm"] && response["lfm"]["status"] == "ok" if response["lfm"]["corrections"] && response["lfm"]["corrections"]["correction"] init_from_hash(response["lfm"]["corrections"]["correction"]["artist"]) rescue nil elsif response["lfm"].key?("corrections") init_from_hash({"name" => artist}) end end end def self.init_from_array(array) return [] unless array.is_a?(Array) array.inject([]) do |arr, artist| arr << init_from_hash(artist) arr end end def self.init_from_hash(hash) return nil unless hash.is_a?(Hash) Artist.new.tap do |artist| artist.name = hash["name"] artist.mbid = hash["mbid"] artist.url = hash["url"] artist.listeners = hash["listeners"].to_i artist.streamable = hash["streamable"] == "1" artist.images = hash["image"] artist.playcount = hash["playcount"].to_i end end end end