+ 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