Class: ITunesAmazon

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

Constant Summary collapse

VERSION =
'0.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ ITunesAmazon

Returns a new instance of ITunesAmazon.



21
22
23
24
25
26
# File 'lib/i_tunes_amazon.rb', line 21

def initialize(file)
  @tracks = []
  @albums = {}
  parse(file)
  get_amazon_stats
end

Instance Attribute Details

#albumsObject

Returns the value of attribute albums.



19
20
21
# File 'lib/i_tunes_amazon.rb', line 19

def albums
  @albums
end

#total_timeObject

Returns the value of attribute total_time.



19
20
21
# File 'lib/i_tunes_amazon.rb', line 19

def total_time
  @total_time
end

#tracksObject

Returns the value of attribute tracks.



19
20
21
# File 'lib/i_tunes_amazon.rb', line 19

def tracks
  @tracks
end

Class Method Details

.run(args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/i_tunes_amazon.rb', line 127

def self.run(args)
  parser = ITunesAmazon.new(args[0])

  album = parser.top_rated_amazon_albums(1)[0]
  p "Top rated Amazon album: #{album.album} by #{album.artist} rated #{album.amazon_average_rating.to_f}"

  top_track = parser.top_played_songs(1)[0]
  p "Top song: #{top_track.name} by #{top_track.artist}, played #{top_track.play_count} times"
  p "Total playtime: #{parser.total_playtime/1000/60} minutes"
  p "Artist Count: #{parser.artist_count}"
  p "Album Count: #{parser.album_count}"
  p "Track Count: #{parser.track_count}"
  p "Genre Count: #{parser.genre_count}"
  random_song = parser.random_song()
  p "Random song: #{random_song.name} by #{random_song.artist}"
  p "Most playd album: #{parser.most_played_album}"
  p "Most played artist: #{parser.most_played_artist}"
  p "Most played genre: #{parser.most_played_genre}"
end

Instance Method Details

#album_countObject



71
72
73
# File 'lib/i_tunes_amazon.rb', line 71

def album_count
  count_field "album"
end

#artist_countObject



67
68
69
# File 'lib/i_tunes_amazon.rb', line 67

def artist_count
  count_field "artist"
end

#count_field(name) ⇒ Object



63
64
65
# File 'lib/i_tunes_amazon.rb', line 63

def count_field name
  @tracks.map {|track| track.send name}.uniq.size
end

#genre_countObject



95
96
97
# File 'lib/i_tunes_amazon.rb', line 95

def genre_count
  count_field "genre"
end

#get_amazon_statsObject



53
54
55
56
57
# File 'lib/i_tunes_amazon.rb', line 53

def get_amazon_stats
  @albums.each_value do |album|
    album.get_amazon_info
  end
end

#most_played_albumObject



99
100
101
# File 'lib/i_tunes_amazon.rb', line 99

def most_played_album
  most_played_field("album")
end

#most_played_artistObject



103
104
105
# File 'lib/i_tunes_amazon.rb', line 103

def most_played_artist
  most_played_field("artist")
end

#most_played_field(name) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/i_tunes_amazon.rb', line 111

def most_played_field(name)
  fields = {}
  fields.default = 0
  @tracks.each do |track|
    fields[track.send(name)] += track.play_count.to_i
  end
  fields.sort {|a, b| b[1] <=> a[1]}[0][0]
end

#most_played_genreObject



107
108
109
# File 'lib/i_tunes_amazon.rb', line 107

def most_played_genre
  most_played_field("genre")
end

#parse(file) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i_tunes_amazon.rb', line 28

def parse(file)
  xml = Nokogiri::XML.parse(File.read(file))
  xml.xpath("//dict/dict/dict").each do |song|
    track = Track.new
    song.children.each do |attr|
      case attr.text
        when 'Artist' then track.artist = attr.next.text
        when 'Album' then track.album = attr.next.text
        when 'Name' then track.name = attr.next.text
        when 'Genre' then track.genre = attr.next.text
        when 'Size' then track.size = attr.next.text
        when 'Total Time' then track.time = attr.next.text
        when 'Play Count' then track.play_count = attr.next.text
      end
    end
    @tracks << track
    key = "#{track.artist},#{track.album}"
    unless @albums.has_key?(key)
      @albums[key] = Album.new(track.album, track.artist, track)
    else
      @albums[key].add_track(track)
    end
  end
end

#random_song(seed = nil) ⇒ Object



90
91
92
93
# File 'lib/i_tunes_amazon.rb', line 90

def random_song(seed = nil)
  unless seed == nil then srand(seed) end
  @tracks[rand(@tracks.size)]
end

#top_played_songs(n) ⇒ Object



85
86
87
88
# File 'lib/i_tunes_amazon.rb', line 85

def top_played_songs(n)
  top_tracks = @tracks.sort {|a, b| b.play_count.to_i <=> a.play_count.to_i}
  top_tracks[0..(n-1)]
end

#top_rated_amazon_albums(n) ⇒ Object



120
121
122
123
# File 'lib/i_tunes_amazon.rb', line 120

def top_rated_amazon_albums(n)
  top_albums = @albums.values.sort {|a, b| b.amazon_average_rating.to_f <=> a.amazon_average_rating.to_f}
  top_albums[0..(n-1)]
end

#total_playtimeObject



75
76
77
78
79
80
81
82
83
# File 'lib/i_tunes_amazon.rb', line 75

def total_playtime
  total_time = 0
  @tracks.each do |track|
    unless (track.time == nil && track.time.is_a?(Numeric)) then
      total_time += track.time.to_i
    end
  end
  total_time
end

#track_countObject



59
60
61
# File 'lib/i_tunes_amazon.rb', line 59

def track_count
  @tracks.size
end