4 # :title: spotify library used at least in spotify and lastfm plugins
6 # Author:: Raine Virta <raine.virta@gmail.com>
8 # Copyright:: (C) 2009 Raine Virta
12 require 'rexml/document'
18 @spotify_id = xml.attributes["href"]
22 id = @spotify_id[@spotify_id.rindex(':')+1..-1]
23 method = self.class.to_s.split('::').last.downcase
24 return "http://open.spotify.com/#{method}/#{id}"
28 class Album < SpotifyObject
29 attr_reader :name, :released, :artist
33 @name = xml.elements["name"].text
34 if e = xml.elements["artist"]
35 @artist = Artist.new(xml.elements["artist"])
37 if e = xml.elements["released"]
38 @released = e.text.to_i
43 class Artist < SpotifyObject
48 @name = xml.elements["name"].text
52 class Track < SpotifyObject
53 attr_reader :name, :artist, :album, :track_number
57 @name = xml.elements["name"].text
58 @artist = Artist.new(xml.elements["artist"])
59 @album = Album.new(xml.elements["album"])
60 @track_number = xml.elements["track-number"].text.to_i
61 @length = xml.elements["length"].text.to_f
65 str = "#{artist.name} – #{name} [#{album.name}"
66 str << ", #{album.released}" if album.released
71 def self.get(service, method, query, page=1)
73 url = "http://ws.spotify.com/#{service}/1/#{method}?q=#{CGI.escape(query)}&page=#{page}"
74 xml = Irc::Utils.bot.httputil.get(url)
76 return REXML::Document.new(xml).root
79 def self.search(method, query, page=1)
80 doc = get(:search, method, query, page)
81 return nil if doc.elements["opensearch:totalResults"].text.to_i.zero?
82 return Spotify.const_get(method.to_s.capitalize).new(doc.elements[method.to_s])