Class: Itility

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

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(file) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/itility.rb', line 13

def self.run(file)
  show = Itility.new
  raw = show.all_tracks(file)
  album_list = show.album_count(raw)
  artist_count = show.artist_count(raw)
  time_total = show.time_total(raw)
  sorted = show.top_tracks_by_played(raw,5)

  puts "RECOMMENDED ARTISTS:"
  puts show.find_similar_artist(sorted)
  puts "-----"

  puts "RECOMMENDED SONGS:"
  puts show.find_similar_song(sorted)
  puts "-----"
 
  puts "MOST PLAYED SONGS:"
  sorted.each do |item|
    puts "PLAYED: #{item.play_count} times - TITLE: #{item.artist} - SONG: #{item.name} - ALBUM: #{item.album}"
  end 
  puts "-----"
  puts "There are #{album_list} albums."
  puts "There are #{raw.size} tracks."
  puts "There are #{artist_count} artists."
  puts "Total play time is #{Duration.new(time_total / 1000)}."
  
end

Instance Method Details

#album_count(track_list) ⇒ Object



105
106
107
108
# File 'lib/itility.rb', line 105

def album_count(track_list)
  albums = album_list(track_list)
  albums.size
end

#album_list(track_list) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/itility.rb', line 83

def album_list(track_list)
  albums = Hash.new 0
  track_list.each do |item| 
    albums[item.album] += 1
  end
  return albums
end

#all_tracks(file) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/itility.rb', line 68

def all_tracks file
  doc = Nokogiri::XML.parse(File.read("#{file}"))
  track_list = []
  doc.xpath('//dict/dict/dict').each do |tracks|
    song = {}
    tracks.xpath('key').each do |track|
      key = track.inner_text.downcase.gsub(" ", "_").to_sym
      value = track.next.inner_text
      song[key] = value
    end
    track_list << Track.new(song[:track_id],song)
  end
  return track_list
end

#artist_count(track_list) ⇒ Object



119
120
121
122
# File 'lib/itility.rb', line 119

def artist_count(track_list)
  artists = artist_list(track_list)
  artists.size
end

#artist_list(track_list) ⇒ Object



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

def artist_list(track_list)
  artists = Hash.new 0 
  track_list.each do |item|
    song_artist = item.artist || "Unknown Artist"
    artists[song_artist] += 1 
  end
  return artists
end

#find_similar_artist(sorted) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/itility.rb', line 41

def find_similar_artist(sorted)
  new_artists = Array.new
  sorted.each do |item|
    artist = item.artist.to_s.gsub(/ /, '')
    xml_raw = open("http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=#{artist}&limit=5&api_key=b25b959554ed76058ac220b7b2e0a026")
    doc = Nokogiri::XML.parse(xml_raw.read)
    doc.xpath('/lfm/similarartists/artist').each do |new|
      new_artists << new.xpath('name').inner_text
    end
  end
  return new_artists.uniq
end

#find_similar_song(sorted) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/itility.rb', line 54

def find_similar_song(sorted)
  new_artists = Array.new
  sorted.each do |item|
    artist = item.artist.to_s.gsub(/ /, '+')
    song = item.name.to_s.gsub(/ /, '+')
    xml_raw = open("http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=#{artist}&track=#{song}&api_key=b25b959554ed76058ac220b7b2e0a026")
    doc = Nokogiri::XML.parse(xml_raw.read)
    doc.xpath('/lfm/similartracks/track').each do |new|
      new_artists << "#{new.xpath('artist/name').inner_text} - #{new.xpath('name').inner_text}"
    end
  end
  return new_artists.uniq.first(5)
end

#time_total(track_list) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/itility.rb', line 96

def time_total(track_list)
  all_time = 0
  track_list.each do |item| 
    calc = item.total_time.to_i
    all_time = all_time + calc
  end 
  return all_time
end

#top_tracks_by_played(track_list, number) ⇒ Object



91
92
93
94
# File 'lib/itility.rb', line 91

def top_tracks_by_played(track_list,number)
  sorted = track_list.compact.sort_by {|track| -track.play_count.to_i}
  return sorted.first(number)
end