From e188fce6cd7d2f7236ee2b5fbd2b2bc53cb9136b Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Wed, 4 Apr 2007 19:54:24 +0000 Subject: imdb plugin: use constants for common strings and regexps --- data/rbot/plugins/imdb.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/data/rbot/plugins/imdb.rb b/data/rbot/plugins/imdb.rb index 5615ac00..699e54dd 100644 --- a/data/rbot/plugins/imdb.rb +++ b/data/rbot/plugins/imdb.rb @@ -13,6 +13,11 @@ require 'uri/common' class Imdb + IMDB = "http://us.imdb.com" + TITLE_OR_NAME_MATCH = /]*)>([^<]*)<\/a>/ + TITLE_MATCH = /]*)>([^<]*)<\/a>/ + NAME_MATCH = /]*)>([^<]*)<\/a>/ + def initialize(bot) @bot = bot end @@ -25,7 +30,7 @@ class Imdb def do_search(str) resp = nil begin - resp = @bot.httputil.get_response("http://us.imdb.com/find?q=#{str}", + resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}", :max_redir => -1) rescue Exception => e error e.message @@ -34,13 +39,13 @@ class Imdb end if resp.code == "200" - m = /]*)>(?:[^<]*)<\/a>/.match(resp.body) + m = TITLE_OR_NAME_MATCH.match(resp.body) if m url = m[1] return url end elsif resp.code == "302" - new_loc = resp['location'].gsub(/http:\/\/us.imdb.com/, "") + new_loc = resp['location'].gsub(IMDB, "") if new_loc.match(/\/find\?q=(.*)/) return do_search($1) else @@ -74,8 +79,7 @@ class Imdb def info_title(sr) resp = nil begin - resp = @bot.httputil.get_response('http://us.imdb.com' + sr, - :max_redir => -1) + resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1) rescue Exception => e error e.message warning e.backtrace.join("\n") @@ -113,8 +117,7 @@ class Imdb def info_name(sr) resp = nil begin - resp = @bot.httputil.get_response('http://us.imdb.com' + sr, - :max_redir => -1) + resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1) rescue Exception => e error e.message warning e.backtrace.join("\n") @@ -142,7 +145,7 @@ class Imdb filmorate = nil begin - filmorate = @bot.httputil.get("http://us.imdb.com" + sr + "filmorate") + filmorate = @bot.httputil.get(IMDB + sr + "filmorate") rescue Exception end @@ -151,7 +154,7 @@ class Imdb what = str.match(/([^<]+)<\/a>/)[1] rescue nil # next unless what next unless ['Actor', 'Director'].include?(what) - movies[what] = str.scan(/([^<]+)<\/a>/)[0..2].map { |tit| + movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit| Utils.decode_html_entities(tit) } } -- cgit v1.2.3