diff options
author | Raine Virta <rane@kapsi.fi> | 2009-02-25 00:46:48 +0200 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2009-02-26 00:39:48 +0100 |
commit | 20c191c1bf5925f90ebbe9a42726f743bd9bf606 (patch) | |
tree | 1d4ebe664a5d13ba9ab938158fbb52355898985c /data/rbot | |
parent | 91020ebc25356118a88c4cb1a3484183b752d17b (diff) |
lastfm: group same tracks in recent tracks
Diffstat (limited to 'data/rbot')
-rw-r--r-- | data/rbot/plugins/lastfm.rb | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/data/rbot/plugins/lastfm.rb b/data/rbot/plugins/lastfm.rb index 96c2d23b..e13b6786 100644 --- a/data/rbot/plugins/lastfm.rb +++ b/data/rbot/plugins/lastfm.rb @@ -553,7 +553,24 @@ class LastFmPlugin < Plugin tracks = doc.root.get_elements("recenttracks/track").map do |track| [track.elements["artist"].text, track.elements["name"].text].join(" - ") end - tracks_prep = tracks[0, num].to_enum(:each_with_index).collect { |e,i| (i % 2).zero? ? Underline+e+Underline : e } + + counts = [] + tracks.each do |track| + if t = counts.assoc(track) + counts[counts.rindex(t)] = [track, t[-1] += 1] + else + counts << [track, 1] + end + end + + tracks_prep = counts[0, num].to_enum(:each_with_index).map do |e,i| + str = (i % 2).zero? ? Underline+e[0]+Underline : e[0] + str << " (%{i} times%{m})" % { + :i => e.last, + :m => counts.size == 1 ? _(" or more") : nil + } if e.last > 1 + str + end if tracks.empty? m.reply _("%{user} hasn't played anything recently") % { :user => user } |