- def lastfm(m, params)
- action = params[:action].intern
- action = :neighbours if action == :neighbors
- what = params[:what]
- case action
- when :events, :event
- page = nil
- begin
- location = what.to_s.sub(/^in\s+/,'')
- raise "wrong location #{location}" if location.empty?
- esc = URI.escape(location)
- page = @bot.httputil.get "#{LASTFM}/events/?findloc=#{esc}"
- if page
- events = Array.new
- disp_events = Array.new
-
- # matches are:
- # 1. day 2. moth 3. year 4. url_who 5. who 6. url_where 7. where 8. how_many
- pre_events = page.scan(/<tr class="vevent\s+\w+\s+\S+?-(\d\d)-(\d\d)-(\d\d\d\d)\s*">.*?<a class="url summary" href="(\/event\/\d+)">(.*?)<\/a>.*?<a href="(\/venue\/\d+)">(.*?)<\/a>.*?<td class="attendance">(.*?)<\/td>\s+<\/tr>/m)
- # debug pre_events.inspect
+ 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