+ 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