Index: trunk/amazonbot/amazonbot.py
===================================================================
--- trunk/amazonbot/amazonbot.py	(revision 9)
+++ trunk/amazonbot/amazonbot.py	(revision 10)
@@ -10,6 +10,8 @@
 import re
 import sys
+import time
 import random
 import MeCab
+import nkf
 
 from ircbot import SingleServerIRCBot
@@ -29,7 +31,26 @@
 
 def uniq(sequence):
+	"""ãªã¹ãããéè€ãåãé€ã (é çªãçãã®ã§æ³šæ)
+	"""
 	return list(set(sequence))
 
+def unicoding(text):
+	"""text ãåŒ·å¶çã« unicode ãªããžã§ã¯ãã«å€æ
+	"""
+	if type(text) is unicode:
+		return text
+	return unicode(nkf.nkf('-w', text), 'utf-8')
+
+def ununicoding(text, encoding='iso-2022-jp'):
+	"""text ãæå®ããã encoding ã§ãšã³ã³ãŒããïŒraw str ã«åŒ·å¶å€æ
+	"""
+	if type(text) is not unicode:
+		return unicoding(text).encode(encoding)
+	return text.encode(encoding)
+
 def mecab_parse(text):
+	"""MeCab ãäœ¿ã£ãŠåœ¢æ
+çŽ è§£æãïŒåºæåè©ãšäžè¬åè©ã ããæœåºãã
+	"""
 	def choice_nominal(wlist):
 		res = []
@@ -37,7 +58,8 @@
 			wtypes = wtype.split('-')
 			if 'åºæåè©' in wtypes or ('åè©' in wtypes and 'äžè¬' in wtypes):
-				res.append(word)
+				res.append(unicoding(word))
 		return res
 
+	text = ununicoding(text, 'utf-8')
 	result = []
 	tag = MeCab.Tagger('-Ochasen')
@@ -52,18 +74,28 @@
 
 class AmazonBotBase(SingleServerIRCBot):
+	"""ã¢ããŸã³ãããã®ããŒã¹ã¯ã©ã¹
+	ããã€åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ
+çŽ è§£æãšåè©æœåºãŸã§ãããããªã
+	ãµãã¯ã©ã¹ã§ process_keyword ãå®è£
+ããŠ Amazon ãžã¯ãšãªãæããã¹ã
+	"""
 	def __init__(self):
 		_server = [(config.get('irc', 'server'), config.get('irc', 'port', 'int'))]
 		_nick = config.get('bot', 'nick')
 
+		self._prev_time = time.time()
 		self._silent = False
 		SingleServerIRCBot.__init__(self, _server, _nick, _nick)
 
-	def get_version(self):
-		return 'AmazonBot by %s, based on python-irclib' % __author__
+	def start(self):
+		try:
+			SingleServerIRCBot.start(self)
+		except KeyboardInterrupt:
+			self.die(ununicoding(config.get('bot', 'bye')))
 
 	def on_welcome(self, c, e):
 		c.join(config.get('irc', 'channel'))
 		if __debug__:
-			print >> sys.stderr, '* Joined %s' % config.get('irc', 'channel')
+			print >> sys.stderr, 'DEBUG> Joined %s' % config.get('irc', 'channel')
 
 	def on_nicknameinuse(self, c, e):
@@ -74,10 +106,11 @@
 
 	def on_pubmsg(self, c, e):
-		try:
-			msg = unicode(e.arguments()[0], 'iso-2022-jp')
-		except UnicodeError:
+		if time.time() > self._prev_time + config.get('bot', 'freq', 'int'):
 			if __debug__:
-				print >> sys.stderr, 'ERR: incoming message encoding failed: %s' % e.arguments()
+				prev = time.strftime('%y/%m/%d %H:%M:%S', time.localtime(self._prev_time))
+				print >> sys.stderr, 'DEBUG> Not expired: prev time is %s' % prev
 			return False
+
+		msg = unicoding(e.arguments()[0])
 
 		self.silence(msg, c, e)
@@ -85,6 +118,8 @@
 			return False
 
-		nominals = mecab_parse(msg.encode('utf-8'))
+		nominals = mecab_parse(msg)
 		if not nominals:
+			if __debug__:
+				print >> sys.stderr, "DEBUG> Couldn't find nominal words"
 			return False
 
@@ -92,39 +127,30 @@
 		if title and url:
 			channel = e.target()
-
-			content = unicode(config.get('bot', 'content'), 'utf-8')
-			title = title
-
+			content = unicoding(config.get('bot', 'content'))
 			try:
-				message = ('%(content)s: %(title)s %(url)s' % locals()).encode('iso-2022-jp')
+				message = ununicoding(': '.join([content, title, url]))
 			except UnicodeError:
-				if __debug__:
-					print >> sys.stderr, 'ERR: my message encoding failed: '
-					print >> sys.stderr, ' * content> %s' % content
-					print >> sys.stderr, ' * title> %s' % title
-					print >> sys.stderr, ' * url> %s' % url
 				return False
+				# ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã
 
 			c.privmsg(channel, message)
-
 			return True
-
 		return False
 
-	ACTIVE_PATTERN = re.compile(config.get('bot', 'active_pattern'))
-	SILENT_PATTERN = re.compile(config.get('bot', 'silent_pattern'))
+	ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern')))
+	SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern')))
 	def silence(self, msg, c, e):
 		ch = e.target()
-		active = self.ACTIVE_PATTERN.search(msg.encode('utf-8'))
-		silent = self.SILENT_PATTERN.search(msg.encode('utf-8'))
+		active = self.ACTIVE_PATTERN.search(msg)
+		silent = self.SILENT_PATTERN.search(msg)
 		if __debug__:
-			print >> sys.stderr, 'ACTIVE_PATTERN: %s, SILENT_PATTERN: %s' % (str(active), str(silent))
+			print >> sys.stderr, 'DEBUG> ACT_PATT: %s, SIL_PATT: %s' % (str(active), str(silent))
 
 		if active:
 			self._silent = False
-			c.privmsg(ch, unicode(config.get('bot', 'thanks'), 'utf-8').encode('iso-2022-jp'))
+			c.privmsg(ch, ununicoding(config.get('bot', 'thanks')))
 		elif silent:
 			self._silent = True
-			c.privmsg(ch, unicode(config.get('bot', 'sorry'), 'utf-8').encode('iso-2022-jp'))
+			c.privmsg(ch, ununicoding(config.get('bot', 'sorry')))
 
 	def process_keyword(self, keyword):
@@ -132,13 +158,26 @@
 
 class AmazonBot(AmazonBotBase):
+	"""ã¢ããŸã³ãããã®å®è£
+ã¯ã©ã¹
+	process_keyword ã¡ãœããã§ Amazon ãžã¯ãšãªãæããŠçµæãè¿ã
+	"""
 	def __init__(self):
 		AmazonBotBase.__init__(self)
 
+	def get_version(self):
+		return 'AmazonBot by %s, based on python-irclib' % __author__
+
 	def process_keyword(self, keyword):
+		keyword = ununicoding(keyword, 'utf-8')
+		if __debug__:
+			print >> sys.stderr, 'DEBUG> KEYWORD: %s' % ununicoding(keyword, 'euc-jp')
+
 		try:
 			data = my_amazon.searchByBlended(keyword)
 			if type(data.ProductLine) is not type([]):
 				data.ProductLine = [data.ProductLine]
-		except my_amazon.AmazonError:
+		except my_amazon.AmazonError, e:
+			if __debug__:
+				print >> sys.stderr, 'DEBUG> Caught AmazonError: %s' % str(e)
 			return [None, None]
 
@@ -146,6 +185,6 @@
 		detail = random.choice(product_line.ProductInfo.Details)
 
-		url = getattr(detail, 'URL', None)
-		product_name = getattr(detail, 'ProductName', None)
+		url = unicoding(getattr(detail, 'URL', None))
+		product_name = unicoding(getattr(detail, 'ProductName', None))
 
 		return [product_name, url]
