From 20c191c1bf5925f90ebbe9a42726f743bd9bf606 Mon Sep 17 00:00:00 2001 From: Raine Virta Date: Wed, 25 Feb 2009 00:46:48 +0200 Subject: lastfm: group same tracks in recent tracks --- data/rbot/plugins/lastfm.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'data') 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 } -- cgit v1.2.3