Class: MineTunes

Inherits:
Object
  • Object
show all
Defined in:
lib/mine_tunes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ MineTunes

Returns a new instance of MineTunes.



15
16
17
18
# File 'lib/mine_tunes.rb', line 15

def initialize file
  @tracks = []
  @tracks = get_tracks file
end

Instance Attribute Details

#tracksObject

Returns the value of attribute tracks.



13
14
15
# File 'lib/mine_tunes.rb', line 13

def tracks
  @tracks
end

Class Method Details

.run(file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mine_tunes.rb', line 20

def self.run file
  mine = MineTunes.new file
  
  most_played_tracks = mine.most_played_tracks 10
  most_recently_played_tracks = mine.most_recently_played_tracks 10
  
  puts "STATS"
  puts "#{mine.number_of_tracks} tracks"
  puts "#{mine.number_of_artists} artists"
  puts "#{mine.number_of_albums} albums"
  puts "#{mine.total_play_time}ms of play time"
  puts
  puts "MOST PLAYED TRACKS"
  puts most_played_tracks
  puts
  puts "MOST RECENTLY PLAYED TRACKS"
  puts most_recently_played_tracks
end

Instance Method Details

#get_tracks(file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mine_tunes.rb', line 39

def get_tracks file
  doc = Nokogiri::XML(open("#{file}"))
  
  doc.xpath('//dict/dict/dict').each do |track|
    meta = {}
    track.xpath('key').each do |item|
      key = item.inner_text.downcase.gsub(" ", "_").to_sym
      if item.next.name == "date"
        value = Time.parse(item.next.inner_text)
      else
        value = item.next.inner_text
      end
      meta[key] = value
    end
    @tracks << Track.new(meta)
  end
  return @tracks
end

#most_played_tracks(number) ⇒ Object



78
79
80
# File 'lib/mine_tunes.rb', line 78

def most_played_tracks(number)
  @tracks.sort_by{ |track| -track.play_count.to_i }.first(number).map{ |track| "[#{track.play_count}] #{track.artist} - #{track.name}" }
end

#most_recently_played_tracks(number) ⇒ Object



82
83
84
# File 'lib/mine_tunes.rb', line 82

def most_recently_played_tracks(number)
  @tracks.sort_by{ |track| -track.play_date_utc.to_i }.first(number).map{ |track| "[#{track.play_date_utc}] #{track.artist} - #{track.name}" }
end

#names_of_artistsObject



62
63
64
# File 'lib/mine_tunes.rb', line 62

def names_of_artists
  @tracks.map{ |track| track.artist }.uniq
end

#number_of_albumsObject



70
71
72
# File 'lib/mine_tunes.rb', line 70

def number_of_albums
  @tracks.map{ |track| track.album }.uniq.size
end

#number_of_artistsObject



66
67
68
# File 'lib/mine_tunes.rb', line 66

def number_of_artists
  @tracks.map{ |track| track.artist }.uniq.size
end

#number_of_tracksObject



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

def number_of_tracks
  @tracks.size
end

#total_play_timeObject



74
75
76
# File 'lib/mine_tunes.rb', line 74

def total_play_time
  @tracks.map{ |track| track.total_time.to_i }.inject(0){ |sum, time| sum + time }
end