Class: Rockstar::Track

Inherits:
Base
  • Object
show all
Defined in:
lib/rockstar/track.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connection, fetch_and_parse, get_instance

Constructor Details

#initialize(artist, name, o = {}) ⇒ Track

Returns a new instance of Track.

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
173
# File 'lib/rockstar/track.rb', line 165

def initialize(artist, name, o={})
  raise ArgumentError, "Artist is required" if artist.blank?
  raise ArgumentError, "Name is required" if name.blank?
  @artist = artist
  @name = name

  options = {:include_info => false}.merge(o)
  load_info if options[:include_info]
end

Instance Attribute Details

#albumObject

Returns the value of attribute album.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def album
  @album
end

#album_mbidObject

Returns the value of attribute album_mbid.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def album_mbid
  @album_mbid
end

#artistObject

Returns the value of attribute artist.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def artist
  @artist
end

#artist_mbidObject

Returns the value of attribute artist_mbid.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def artist_mbid
  @artist_mbid
end

#chartpositionObject

for weekly top tracks



52
53
54
# File 'lib/rockstar/track.rb', line 52

def chartposition
  @chartposition
end

#contentObject

Returns the value of attribute content.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def content
  @content
end

#countObject

only seems to be used on top tracks for tag



49
50
51
# File 'lib/rockstar/track.rb', line 49

def count
  @count
end

#dateObject

Returns the value of attribute date.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def date
  @date
end

#date_utsObject

Returns the value of attribute date_uts.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def date_uts
  @date_uts
end

#durationObject

Returns the value of attribute duration.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def duration
  @duration
end

#imageObject

only seems to be used on top tracks for tag



49
50
51
# File 'lib/rockstar/track.rb', line 49

def image
  @image
end

#imagesObject

only seems to be used on top tracks for tag



49
50
51
# File 'lib/rockstar/track.rb', line 49

def images
  @images
end

#mbidObject

Returns the value of attribute mbid.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def name
  @name
end

#now_playingObject Also known as: now_playing?

Returns the value of attribute now_playing.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def now_playing
  @now_playing
end

#playcountObject

Returns the value of attribute playcount.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def rank
  @rank
end

#streamableObject

Returns the value of attribute streamable.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def streamable
  @streamable
end

#summaryObject

Returns the value of attribute summary.



45
46
47
# File 'lib/rockstar/track.rb', line 45

def summary
  @summary
end

#thumbnailObject

only seems to be used on top tracks for tag



49
50
51
# File 'lib/rockstar/track.rb', line 49

def thumbnail
  @thumbnail
end

#urlObject

Returns the value of attribute url.



44
45
46
# File 'lib/rockstar/track.rb', line 44

def url
  @url
end

Class Method Details

.love(artist, track, session_key) ⇒ Object



68
69
70
71
# File 'lib/rockstar/track.rb', line 68

def love(artist, track, session_key)
  doc = Hpricot::XML(Track.connection.post("track.love", true, {:track => track, :artist => artist, :sk => session_key}))
  doc.at("lfm")["status"]
end

.new_from_xml(xml, doc = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rockstar/track.rb', line 55

def new_from_xml(xml, doc=nil)
  artist          = (xml).at(:artist)['name']               if (xml).at(:artist) && !(xml).at(:artist)['name'].nil?
  artist          = (xml).at(:artist).at(:name).inner_html  if artist.nil? && (xml).at(:artist) && (xml).at(:artist).at(:name)
  artist          = (xml).at(:artist).inner_html            if artist.nil? && (xml).at(:artist)
  artist          = doc.root['artist']                      if artist.nil? && doc.root['artist']
  name            = (xml).at(:name).inner_html              if (xml).at(:name)
  name            = xml['name']                             if name.nil? && xml['name']

  track = Track.new(artist, name)
  track.load_info(xml)
  track
end

.scrobble(params = {}) ⇒ Object

Scrobble a song

Possible parameters:

session_key (required) : the session key you got during authentification
track       (required) : name of the track
artist      (required) : name of the artist of the track
time        (required) : a time object set to the time the track started playing
album                  : Name of the album
albumArtist            : Name of the album artist if artist differs
trackNumber            : Number of the track
mbid                   : MusicBrainz ID of the track
duration               : track length


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rockstar/track.rb', line 85

def scrobble(params = {})
 if params[:session_key].blank? || params[:track].blank? || params[:time].nil? || params[:artist].blank?
   raise ArgumentError, "Missing required argument"
 end

 query = {
   :sk           => params[:session_key],
   "track[0]"    => params[:track],
   "timestamp[0]"=> params[:time].utc.to_i,
   "artist[0]"   => params[:artist]
 }

 query["album[0]"]       = params[:album] if !params[:album].blank?
 query["albumArtist[0]"] = params[:albumArtist] if !params[:albumArtist].blank?
 query["trackNumber[0]"] = params[:trackNumber] if !params[:trackNumber].blank?
 query["mbid[0]"]        = params[:mbid] if !params[:mbid].blank?
 query["duration[0]"]    = params[:duration] if !params[:duration].blank?

 doc = Hpricot::XML(Track.connection.post("track.scrobble", true, query))

 if doc.at("lfm")["status"] == "failed"
   case doc.at("lfm").at("error")["code"].to_i
     when 9
       raise BadSessionError, doc.at("lfm").at("error").inner_html
     when 11, 16
       raise UnavailableError, doc.at("lfm").at("error").inner_html
    else
       raise RequestFailedError, doc.at("lfm").at("error").inner_html
    end
 end

 doc.at("lfm")["status"]
end

.updateNowPlaying(params = {}) ⇒ Object

Update the current playing song

Possible parameters:

session_key (required) : the session key you got during authentification
track       (required) : name of the track
artist      (required) : name of the artist of the track
album                  : Name of the album
albumArtist            : Name of the album artist if artist differs
trackNumber            : Number of the track
mbid                   : MusicBrainz ID of the track
duration               : track length


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rockstar/track.rb', line 130

def updateNowPlaying(params = {})
  if params[:session_key].blank? || params[:track].blank? || params[:artist].blank?
    raise ArgumentError, "Missing required argument"
  end

  query = {
    :sk        => params[:session_key],
    "track"    => params[:track],
    "artist"   => params[:artist]
  }

  query["album"]       = params[:album] if !params[:album].blank?
  query["albumArtist"] = params[:albumArtist] if !params[:albumArtist].blank?
  query["trackNumber"] = params[:trackNumber] if !params[:trackNumber].blank?
  query["mbid"]        = params[:mbid] if !params[:mbid].blank?
  query["duration"]    = params[:duration] if !params[:duration].blank?

  doc = Hpricot::XML(Track.connection.post("track.updateNowPlaying", true, query))

  if doc.at("lfm")["status"] == "failed"
    case doc.at("lfm").at("error")["code"].to_i
      when 9
        raise BadSessionError, doc.at("lfm").at("error").inner_html
      when 11, 16
        raise UnavailableError, doc.at("lfm").at("error").inner_html
     else
        raise RequestFailedError, doc.at("lfm").at("error").inner_html
     end
  end

  doc.at("lfm")["status"]
end

Instance Method Details

#albums(force = false) ⇒ Object



216
217
218
# File 'lib/rockstar/track.rb', line 216

def albums(force=false)
  get_instance("track.getInfo", :albums, :album, {:track => @name, :artist => @artist}, force)
end

#fans(force = false) ⇒ Object



220
221
222
# File 'lib/rockstar/track.rb', line 220

def fans(force=false)
  get_instance("track.getTopFans", :fans, :user, {:track => @name, :artist => @artist}, force)
end

#load_info(xml = nil) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rockstar/track.rb', line 175

def load_info(xml=nil)
  unless xml
    doc = self.class.fetch_and_parse("track.getInfo", {:artist => @artist, :track => @name})
    xml = (doc / :track).first
  end

  return self if xml.nil?

  self.now_playing	 = xml['nowplaying'] == 'true' ? true : false
  self.artist_mbid   = (xml).at(:artist)['mbid']               if (xml).at(:artist) && (xml).at(:artist)['mbid']
  self.artist_mbid   = (xml).at(:artist).at(:mbid).inner_html  if artist_mbid.nil? && (xml).at(:artist) && (xml).at(:artist).at(:mbid)
  self.mbid          = (xml).at(:mbid).inner_html              if (xml).at(:mbid)
  self.playcount     = (xml).at(:playcount).inner_html         if (xml).at(:playcount)
  self.chartposition = self.rank = xml['rank']                 if xml['rank']
  self.url           = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)
  self.streamable    = (xml).at(:track)['streamable']          if (xml).at(:track) && (xml).at(:track)['streamable']
  self.streamable    = (xml).at(:streamable).inner_html == '1' ? 'yes' : 'no' if streamable.nil? && (xml).at(:streamable)
  self.duration      = (xml).at(:duration).inner_html.to_i     if (xml).at(:duration)

  self.count         = xml['count']                            if xml['count']
  self.album         = (xml).at(:album).inner_html             if (xml).at(:album)
  self.album_mbid    = (xml).at(:album)['mbid']                if (xml).at(:album) && (xml).at(:album)['mbid']
  self.date          = Base.parse_time((xml).at(:date).inner_html)  if (xml).at(:date)
  self.date_uts      = (xml).at(:date)['uts']                  if (xml).at(:date) && (xml).at(:date)['uts']

  if wiki_xml = xml.at(:wiki)
    self.summary     = wiki_xml.at(:summary).to_plain_text     if wiki_xml.at(:summary)
    self.content     = wiki_xml.at(:content).to_plain_text     if wiki_xml.at(:content)
  end

  self.images = {}
  (xml/'image').each {|image|
    self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
  }

  self.thumbnail = images['small']
  self.image     = images['medium']

  self
end

#love(session_key) ⇒ Object

The session_key is returned by auth.session.key



233
234
235
# File 'lib/rockstar/track.rb', line 233

def love(session_key)
  Track.love(@artist, @name, session_key)
end

#scrobble(time, session_key) ⇒ Object

scrobble this track

time :       a time object set to the time the track started playing
session_key: the session key you got during authentification


240
241
242
243
244
245
246
247
248
249
# File 'lib/rockstar/track.rb', line 240

def scrobble(time, session_key)
  Track.scrobble({
    :session_key => session_key,
    :time        => time,
    :track       => @name,
    :artist      => @artist,
    :album       => @album,
    :mbid        => @mbid
  })
end

#similar(limit = 10, force = false) ⇒ Object



228
229
230
# File 'lib/rockstar/track.rb', line 228

def similar(limit = 10, force = false)
  get_instance('track.getSimilar', :similar, :track, {:track => @name, :artist => @artist, :limit => limit}, force)
end

#tags(force = false) ⇒ Object



224
225
226
# File 'lib/rockstar/track.rb', line 224

def tags(force=false)
  get_instance("track.getTopTags", :tags, :tag, {:track => @name, :artist => @artist}, force)
end

#updateNowPlaying(time, session_key) ⇒ Object

inform last.fm that this track is currently playing

time :       a time object set to the time the track started playing
session_key: the session key you got during authentification


254
255
256
257
258
259
260
261
262
263
# File 'lib/rockstar/track.rb', line 254

def updateNowPlaying(time, session_key)
  Track.updateNowPlaying({
    :session_key => session_key,
    :time        => time,
    :track       => @name,
    :artist      => @artist,
    :album       => @album,
    :mbid        => @mbid
  })
end