Sha256: bfbd69e6bc6ca0dc0721d4cd6d2014d32998788c885ccf22cb1dba48c1f343db

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

module Notu

  class MostPlayedTracks

    include Listing

    PERIODS = {
      '7 days' => 'LAST_7_DAYS',
      '30 days' => 'LAST_30_DAYS',
      '90 days' => 'LAST_90_DAYS',
      '365 days' => 'LAST_365_DAYS',
      'Overall' => '',
    }

    attr_reader :period

    def initialize(library, options = {})
      super(library)
      options = options.stringify_keys.reverse_merge('period' => PERIODS.keys.first)
      self.period = options['period']
    end

    def each(&block)
      return unless block_given?
      page_urls.each do |url|
        document = HtmlDocument.get(url)
        (document/'table.chartlist tbody tr').each do |element|
          artist = (element/'td.chartlist-name .chartlist-artists').first.try(:text) || next
          title = (element/'td.chartlist-name .link-block-target').first.try(:text) || next
          plays_count = (element/'td.chartlist-countbar .countbar-bar-value').text.gsub(/[^\d]/, '').presence || next
          yield(Track.new(artist: artist, plays_count: plays_count, title: title))
        end
      end
      nil
    end

    def params
      { 'date_preset' => PERIODS[period] }
    end

    def path
      'library/tracks'
    end

    private

    def period=(value)
      raise ArgumentError.new("Notu::MostPlayedTracks#period is invalid: #{value.inspect}") unless PERIODS.key?(value.to_s)
      @period = value.to_s
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
notu-2.0.1 lib/notu/most_played_tracks.rb
notu-2.0.0 lib/notu/most_played_tracks.rb
notu-1.0.6 lib/notu/most_played_tracks.rb
notu-1.0.5 lib/notu/most_played_tracks.rb
notu-1.0.4 lib/notu/most_played_tracks.rb
notu-1.0.3 lib/notu/most_played_tracks.rb
notu-1.0.2 lib/notu/most_played_tracks.rb
notu-1.0.1 lib/notu/most_played_tracks.rb
notu-1.0.0 lib/notu/most_played_tracks.rb
notu-0.3.1 lib/notu/most_played_tracks.rb
notu-0.3.0 lib/notu/most_played_tracks.rb