Class: Scrobbler::Album

Inherits:
Base
  • Object
show all
Defined in:
lib/scrobbler/album.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key=, connection, get, maybe_streamable_attribute, maybe_streamable_node, post_request, request, sanitize, secret=

Constructor Details

#initialize(artist, name, data = {}) ⇒ Album

TODO:

Albums should be able to be created via a MusicBrainz id too

If the additional parameter :include_info is set to true, additional information is loaded

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/scrobbler/album.rb', line 111

def initialize(artist, name, data={})    
  super()
  raise ArgumentError, "Artist or MBID is required" if artist.blank?
  
  #check for old style parameter arguments, infer MBID if only an artist is given
  if artist.class == String && name.blank? && data == {}
    raise ArgumentError, "MBID is required for an MBID query" if input.blank?
    @mbid = input
    load_album_info() # data must be fetched since all we have is an mbid, nothing else useful
  else
    raise ArgumentError, "Artist is required" if artist.blank?
    raise ArgumentError, "Album Name is required" if name.blank?
    @artist = Artist.new(artist)
    @name = name
    load_album_info() if data[:include_info] || data[:include_all_info]
    load_track_info() if data[:include_all_info]
  end
end

Instance Attribute Details

#album_idObject (readonly)

Returns the value of attribute album_id.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def album_id
  @album_id
end

#artistObject (readonly)

Returns the value of attribute artist.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def artist
  @artist
end

#artist_mbidObject (readonly)

Returns the value of attribute artist_mbid.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def artist_mbid
  @artist_mbid
end

#chartpositionObject (readonly)

needed for weekly album charts



58
59
60
# File 'lib/scrobbler/album.rb', line 58

def chartposition
  @chartposition
end

#countObject (readonly)

needed on top albums for tag



55
56
57
# File 'lib/scrobbler/album.rb', line 55

def count
  @count
end

#image_largeObject (readonly)

Returns the value of attribute image_large.



52
53
54
# File 'lib/scrobbler/album.rb', line 52

def image_large
  @image_large
end

#image_mediumObject (readonly)

Returns the value of attribute image_medium.



52
53
54
# File 'lib/scrobbler/album.rb', line 52

def image_medium
  @image_medium
end

#image_smallObject (readonly)

Returns the value of attribute image_small.



52
53
54
# File 'lib/scrobbler/album.rb', line 52

def image_small
  @image_small
end

#listenersObject (readonly)

Returns the value of attribute listeners.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def listeners
  @listeners
end

#mbidObject (readonly)

Returns the value of attribute mbid.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def mbid
  @mbid
end

#nameObject (readonly)

Returns the value of attribute name.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def name
  @name
end

#playcountObject (readonly)

Returns the value of attribute playcount.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def playcount
  @playcount
end

#playlistObject (readonly)

needed on top albums for tag



55
56
57
# File 'lib/scrobbler/album.rb', line 55

def playlist
  @playlist
end

#positionObject (readonly)

needed for weekly album charts



58
59
60
# File 'lib/scrobbler/album.rb', line 58

def position
  @position
end

#rankObject (readonly)

Returns the value of attribute rank.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def rank
  @rank
end

#reachObject (readonly)

Returns the value of attribute reach.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def reach
  @reach
end

#release_dateObject (readonly)

Returns the value of attribute release_date.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def release_date
  @release_date
end

#tagcountObject (readonly)

Returns the value of attribute tagcount.



52
53
54
# File 'lib/scrobbler/album.rb', line 52

def tagcount
  @tagcount
end

#top_tagsObject (readonly)

Returns the value of attribute top_tags.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def top_tags
  @top_tags
end

#urlObject (readonly)

Returns the value of attribute url.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def url
  @url
end

Class Method Details

.data_from_xml(xml, o = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/scrobbler/album.rb', line 68

def data_from_xml(xml, o = {})
  data = {}
  o = {:include_artist_info => true}.merge(o) 
  xml.children.each do |child|
    data[:name] = child.content if ['name', 'title'].include?(child.name)
    data[:album_id] = child.content.to_i if child.name == 'id'
    data[:playcount] = child.content.to_i if child.name == 'playcount'
    data[:tagcount] = child.content.to_i if child.name == 'tagcount'
    data[:release_date] = Time.parse(child.content.strip) if child.name == 'releasedate'
    data[:listeners] = child.content.to_i if child.name == 'listeners'
    data[:mbid] = child.content if child.name == 'mbid'
    data[:url] = child.content if child.name == 'url'
    data[:artist] = Artist.new_from_xml(child) if (child.name == 'artist' || child.name == 'creator') && o[:include_artist_info]
    maybe_image_node(data, child)
    if child.name == 'toptags'
      data[:top_tags] = []
      child.children.each do |grandchild|
        next unless grandchild.name == 'tag'
        data[:top_tags] << Tag.new_from_xml(grandchild)
      end
    end  
  end
          
  # If we have not found anything in the content of this node yet then
  # this must be a simple artist node which has the name of the artist
  # as its content
  data[:name] = xml.content if data == {}
  
  # Get all information from the root's attributes
  data[:mbid] = xml['mbid'] if xml['mbid']
  data[:rank] = xml['rank'].to_i if xml['rank']
  data[:position] = xml['position'].to_i if xml['position']
  
  # If there is no name defined, than this was an empty album tag
  return nil if data[:name].empty?
  data
end

.new_from_xml(xml, o = {}) ⇒ Object



62
63
64
65
66
# File 'lib/scrobbler/album.rb', line 62

def new_from_xml(xml, o = {})
  data = self.data_from_xml(xml)
  return nil if data[:name].empty?
  Album.new(data[:artist], data[:name], data)
end

Instance Method Details

#add_tags(tags) ⇒ Object

Tag an album using a list of user supplied tags.

Raises:

  • (NotImplementedError)


164
165
166
167
# File 'lib/scrobbler/album.rb', line 164

def add_tags(tags)
    # This function require authentication, but SimpleAuth is not yet 2.0
    raise NotImplementedError
end

#load_album_infoObject

TODO:

Parse wiki content

TODO:

Add language code for wiki translation

Load additional information about this album

Calls “album.getinfo” REST method



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/scrobbler/album.rb', line 139

def load_album_info
  return nil if @album_info_loaded
  params = @mbid ? {'mbid' => @mbid} : {'artist' => @artist.name, 'album' => @name}
  xml = Base.request('album.getinfo', params)
  unless xml.root['status'] == 'failed'
    xml.root.children.each do |child|
      next unless child.name == 'album'
      data = self.class.data_from_xml(child)
      populate_data(data)
      @album_info_loaded = true
      break
    end # xml.children.each do |child|
  end
end

#load_track_infoObject



156
157
158
159
160
161
# File 'lib/scrobbler/album.rb', line 156

def load_track_info
   return nil if @track_info_loaded
   load_album_info() if !@album_info_loaded
   @playlist = Playlist.new_from_album(self)
   @tracks = @playlist.tracks
end

#remove_tagObject

Remove a user’s tag from an album.

Raises:

  • (NotImplementedError)


176
177
178
179
# File 'lib/scrobbler/album.rb', line 176

def remove_tag()
    # This function require authentication, but SimpleAuth is not yet 2.0
    raise NotImplementedError
end

#tagsObject

Get the tags applied by an individual user to an album on Last.fm.

Raises:

  • (NotImplementedError)


170
171
172
173
# File 'lib/scrobbler/album.rb', line 170

def tags()
    # This function require authentication, but SimpleAuth is not yet 2.0
    raise NotImplementedError
end