<commit_before># Below are examples of how to find an artists top tracks and similar artists.
# 
#   artist = Rockstar::Artist.new('<NAME>')
# 
#   puts 'Top Tracks'
#   puts "=" * 10
#   artist.top_tracks.each { |t| puts "#{t.name}" }
# 
#   puts
# 
#   puts 'Similar Artists'
#   puts "=" * 15
#   artist.similar.each { |a| puts "(#{a.match}%) #{a.name}" }
# 
# Would output something similar to:
# 
#   Top Tracks
#   ==========
#   (8797) Before He Cheats
#   (3574) Don't Forget to Remember Me
#   (3569) Wasted
#   (3246) Some Hearts
#   (3142) Jesus, Take the Wheel
#   (2600) Starts With Goodbye
#   (2511) Jesus Take The Wheel
#   (2423) Inside Your Heaven
#   (2328) Lessons Learned
#   (2040) I Just Can't Live a Lie
#   (1899) Whenever You Remember
#   (1882) We're Young and Beautiful
#   (1854) That's Where It Is
#   (1786) I Ain't in Checotah Anymore
#   (1596) The Night Before (Life Goes On)
#   
#   Similar Artists
#   ===============
#   (100%) <NAME>
#   (84.985%) <NAME>an
#   (84.007%) <NAME>
#   (82.694%) <NAME>
#   (81.213%) <NAME>
#   (79.397%) <NAME>
#   (77.121%) <NAME>
#   (75.191%) <NAME>
#   (75.182%) <NAME>
#   (75.144%) <NAME>
#   (73.034%) <NAME>
#   (71.765%) <NAME>
#   (71.084%) <NAME>
#   (69.535%) <NAME>
#   (66.952%) <NAME>
#   (66.398%) <NAME>
#   (65.817%) <NAME>
#   (65.279%) <NAME>
#   (65.115%) <NAME>
#   (62.982%) <NAME>
#   (62.436%) <NAME>
#   (62.160%) <NAME>
module Rockstar
  class Artist < Base
    attr_accessor :name, :mbid, :listenercount, :playcount, :rank, :url, :thumbnail
    attr_accessor :summary, :content, :images, :count, :streamable
    attr_accessor :chartposition
    
    # used for similar artists
    attr_accessor :match
    
    class << self
      def new_from_xml(xml, doc=nil)
        # occasionally name can be found in root of artist element (<artist name="">) rather than as an element (<name>)
        name             = (xml).at(:name).inner_html           if (xml).at(:name)
        name             = xml['name']                          if name.nil? && xml['name']

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

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

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

      return self if xml.nil?

      self.mbid           = (xml).at(:mbid).inner_html              if (xml).at(:mbid)
      self.listenercount  = (xml).at(:listeners).inner_html         if (xml).at(:listeners)
      self.playcount      = (xml).at(:playcount).inner_html         if (xml).at(:playcount)
      self.chartposition  = self.rank = xml['rank']                 if xml['rank']
      self.chartposition  = self.rank = (xml).at(:rank).inner_html  if (xml).at(:rank) if self.rank.nil?
      self.url            = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)<commit_msg>Allowing artist to be looked up by mbid<commit_after># Below are examples of how to find an artists top tracks and similar artists.
# 
#   artist = Rockstar::Artist.new('<NAME>')
# 
#   puts 'Top Tracks'
#   puts "=" * 10
#   artist.top_tracks.each { |t| puts "#{t.name}" }
# 
#   puts
# 
#   puts 'Similar Artists'
#   puts "=" * 15
#   artist.similar.each { |a| puts "(#{a.match}%) #{a.name}" }
# 
# Would output something similar to:
# 
#   Top Tracks
#   ==========
#   (8797) Before He Cheats
#   (3574) Don't Forget to Remember Me
#   (3569) Wasted
#   (3246) Some Hearts
#   (3142) Jesus, Take the Wheel
#   (2600) Starts With Goodbye
#   (2511) Jesus Take The Wheel
#   (2423) Inside Your Heaven
#   (2328) Lessons Learned
#   (2040) I Just Can't Live a Lie
#   (1899) Whenever You Remember
#   (1882) We're Young and Beautiful
#   (1854) That's Where It Is
#   (1786) I Ain't in Checotah Anymore
#   (1596) The Night Before (Life Goes On)
#   
#   Similar Artists
#   ===============
#   (100%) <NAME>
#   (84.985%) <NAME>
#   (84.007%) <NAME>
#   (82.694%) <NAME>
#   (81.213%) <NAME>
#   (79.397%) <NAME>
#   (77.121%) <NAME>
#   (75.191%) <NAME>
#   (75.182%) <NAME>
#   (75.144%) The Wreckers
#   (73.034%) <NAME>
#   (71.765%) <NAME>
#   (71.084%) <NAME>
#   (69.535%) <NAME>
#   (66.952%) <NAME>
#   (66.398%) <NAME>
#   (65.817%) <NAME>
#   (65.279%) <NAME>
#   (65.115%) <NAME>
#   (62.982%) <NAME>
#   (62.436%) <NAME>
#   (62.160%) <NAME>
module Rockstar
  class Artist < Base
    attr_accessor :name, :mbid, :listenercount, :playcount, :rank, :url, :thumbnail
    attr_accessor :summary, :content, :images, :count, :streamable
    attr_accessor :chartposition
    
    # used for similar artists
    attr_accessor :match
    
    class << self
      def new_from_xml(xml, doc=nil)
        # occasionally name can be found in root of artist element (<artist name="">) rather than as an element (<name>)
        name             = (xml).at(:name).inner_html           if (xml).at(:name)
        name             = xml['name']                          if name.nil? && xml['name']

        artist = Artist.new(name)
        artist.load_info(xml)
        artist
      end
    end
    
    def initialize(o={})
      raise ArgumentError, "Name or mbid is required" if o[:name].nil? && o[:mbid].nil?
      
      @name = o[:name] unless o[:name].nil?
      @mbid = o[:mbid] unless o[:mbid].nil?

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

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

      return self if xml.nil?

      self.mbid           = (xml).at(:mbid).inner_html              if (xml).at(:mbid)
      self.listenercount  = (xml).at(:listeners).inner_html         if (xml).at(:listeners)
      self.playcount      = (xml).at(:playcount).inner_html         if (xml).at(:playcount)
      self.chartposition  = self.rank = xml['rank']                 if xml['rank']
      self.chartposition  = self.rank = (xml).at(:rank).inner_html  if (xml).at(:rank) if self.rank.nil?
      self.url            = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)
{'fraction_non_alphanumeric': 0.17137096774193547, 'fraction_numerical': 0.05334987593052109, 'mean_word_length': 2.4449786324786325, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 46}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1513028', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2334, 'n_words': 596}