+ case (topic.intern rescue nil)
+ when :event, :events
+ "lastfm [<num>] events in <location> => show information on events in or near <location>. lastfm [<num>] events by <artist/group> => show information on events by <artist/group>. The number of events <num> that can be displayed is optional, defaults to #{@bot.config['lastfm.default_events']} and cannot be higher than #{@bot.config['lastfm.max_events']}"
+ when :artist, :group
+ "lastfm artist <name> => show information on artist/group <name> from last.fm"
+ when :song, :track
+ "lastfm track <name> => show information on track/song <name> from last.fm [not implemented yet]"
+ when :album
+ "lastfm album <name> => show information on album <name> from last.fm [not implemented yet]"
+ else
+ "lastfm <function> <user> => lastfm data for <user> on last.fm where <function> in [recenttracks, topartists, topalbums, toptracks, tags, friends, neighbors]. other topics: events, artist, group, song, track, album"
+ end
+ end
+
+ def find_event(m, params)
+ num = params[:num] || @bot.config['lastfm.default_events']
+ num = num.to_i.clip(1, @bot.config['lastfm.max_events'])
+
+ location = artist = nil
+ location = params[:location].to_s if params[:location]
+ artist = params[:who].to_s if params[:who]
+ page = nil
+ spec = location ? "in #{location}" : "by #{artist}"
+ query = location ? "?findloc=#{CGI.escape(location)}" : "?s=#{CGI.escape(artist)}&findloc="
+ begin
+ page = @bot.httputil.get LASTFM + "/events/" + query
+ if page
+ events = Array.new
+ disp_events = Array.new
+
+ pre_events = page.scan(LastFmEvent::SELECTOR)
+ # debug pre_events.inspect
+ if pre_events.empty?
+ # We may not find any even because the page gives a list
+ # of locations instead. In this case, retry with the first of
+ # these location
+ if page.match(/<a href="(\/events\/\?l=[^"]+)">/)
+ debug "Rechecking with #{$1}"
+ page = @bot.httputil.get(LASTFM+$1)
+ debug page
+ pre_events = page.scan(LastFmEvent::SELECTOR) if page
+ debug pre_events
+ end
+ if pre_events.empty?
+ m.reply "No events found #{spec}, sorry"
+ return
+ end
+ end
+ pre_events.each { |s| s.scan(LastFmEvent::MATCHER) { |day, month, year, url_who, who, url_where, where, how_many|
+ date = Time.utc(year.to_i, month.to_i, day.to_i)
+ url = LASTFM + url_who
+ if who.match(/<strong>(.*?)<\/strong>(.+)?/)
+ artist = Bold + $1.ircify_html + Bold
+ artist << ", " << $2.ircify_html if $2
+ else
+ debug "who: #{who.inspect}"
+ artist = who.ircify_html
+ end
+ if where.match(/<strong>(.*?)<\/strong>(?:<br\s*\/>(.+)?)?/)
+ loc = Bold + $1.ircify_html + Bold
+ loc << ", " << $2.ircify_html if $2
+ else
+ debug where.inspect
+ loc = where.ircify_html
+ end
+ attendance = how_many ? how_many.ircify_html : ''
+ events << LastFmEvent.new(url, date, artist, loc, attendance)
+ } }
+ # debug events.inspect
+
+ events[0...num].each { |event|
+ disp_events << event.to_s
+ }
+ m.reply disp_events.join(' | '), :split_at => /\s+\|\s+/
+ else
+ m.reply "No events found #{spec}"
+ return
+ end
+ rescue Exception => e
+ m.reply "I had problems looking for events #{spec}"
+ error e.inspect
+ debug e.backtrace.join("\n")
+ debug page[0...10*1024] if page
+ return
+ end