Changeset 10
- Timestamp:
- 06/20/06 14:03:50 (18 years ago)
- Location:
- trunk/amazonbot
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/amazonbot/amazonbot.ini.sample
r9 r10 9 9 10 10 [bot] 11 freq = 120 11 12 nick = amazonbot 12 13 content = ãããªã¢ãã¿ã«ããããªã¹ã¹ã¡ 13 14 thanks = ããããšã 14 15 sorry = æ£çŽããŸããã£ã 16 bye = ããããªããŸãäŒãæ¥ãŸã§ 15 17 active_pattern = ããã|ãŽã¡ã³|ããŸã|ã¹ãã³|ãããŸãã 16 18 silent_pattern = éªé|ãããŸ|ãžã£ã|ãã(ãã|ããŒ)|ã ãŸã -
trunk/amazonbot/amazonbot.py
r9 r10 10 10 import re 11 11 import sys 12 import time 12 13 import random 13 14 import MeCab 15 import nkf 14 16 15 17 from ircbot import SingleServerIRCBot … … 29 31 30 32 def uniq(sequence): 33 """ãªã¹ãããéè€ãåãé€ã (é çªãçãã®ã§æ³šæ) 34 """ 31 35 return list(set(sequence)) 32 36 37 def unicoding(text): 38 """text ã匷å¶çã« unicode ãªããžã§ã¯ãã«å€æ 39 """ 40 if type(text) is unicode: 41 return text 42 return unicode(nkf.nkf('-w', text), 'utf-8') 43 44 def ununicoding(text, encoding='iso-2022-jp'): 45 """text ãæå®ããã encoding ã§ãšã³ã³ãŒããïŒraw str ã«åŒ·å¶å€æ 46 """ 47 if type(text) is not unicode: 48 return unicoding(text).encode(encoding) 49 return text.encode(encoding) 50 33 51 def mecab_parse(text): 52 """MeCab ã䜿ã£ãŠåœ¢æ 53 çŽ è§£æãïŒåºæåè©ãšäžè¬åè©ã ããæœåºãã 54 """ 34 55 def choice_nominal(wlist): 35 56 res = [] … … 37 58 wtypes = wtype.split('-') 38 59 if 'åºæåè©' in wtypes or ('åè©' in wtypes and 'äžè¬' in wtypes): 39 res.append( word)60 res.append(unicoding(word)) 40 61 return res 41 62 63 text = ununicoding(text, 'utf-8') 42 64 result = [] 43 65 tag = MeCab.Tagger('-Ochasen') … … 52 74 53 75 class AmazonBotBase(SingleServerIRCBot): 76 """ã¢ããŸã³ãããã®ããŒã¹ã¯ã©ã¹ 77 ããã€åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ 78 çŽ è§£æãšåè©æœåºãŸã§ãããããªã 79 ãµãã¯ã©ã¹ã§ process_keyword ãå®è£ 80 ã㊠Amazon ãžã¯ãšãªãæããã¹ã 81 """ 54 82 def __init__(self): 55 83 _server = [(config.get('irc', 'server'), config.get('irc', 'port', 'int'))] 56 84 _nick = config.get('bot', 'nick') 57 85 86 self._prev_time = time.time() 58 87 self._silent = False 59 88 SingleServerIRCBot.__init__(self, _server, _nick, _nick) 60 89 61 def get_version(self): 62 return 'AmazonBot by %s, based on python-irclib' % __author__ 90 def start(self): 91 try: 92 SingleServerIRCBot.start(self) 93 except KeyboardInterrupt: 94 self.die(ununicoding(config.get('bot', 'bye'))) 63 95 64 96 def on_welcome(self, c, e): 65 97 c.join(config.get('irc', 'channel')) 66 98 if __debug__: 67 print >> sys.stderr, ' *Joined %s' % config.get('irc', 'channel')99 print >> sys.stderr, 'DEBUG> Joined %s' % config.get('irc', 'channel') 68 100 69 101 def on_nicknameinuse(self, c, e): … … 74 106 75 107 def on_pubmsg(self, c, e): 76 try: 77 msg = unicode(e.arguments()[0], 'iso-2022-jp') 78 except UnicodeError: 108 if time.time() > self._prev_time + config.get('bot', 'freq', 'int'): 79 109 if __debug__: 80 print >> sys.stderr, 'ERR: incoming message encoding failed: %s' % e.arguments() 110 prev = time.strftime('%y/%m/%d %H:%M:%S', time.localtime(self._prev_time)) 111 print >> sys.stderr, 'DEBUG> Not expired: prev time is %s' % prev 81 112 return False 113 114 msg = unicoding(e.arguments()[0]) 82 115 83 116 self.silence(msg, c, e) … … 85 118 return False 86 119 87 nominals = mecab_parse(msg .encode('utf-8'))120 nominals = mecab_parse(msg) 88 121 if not nominals: 122 if __debug__: 123 print >> sys.stderr, "DEBUG> Couldn't find nominal words" 89 124 return False 90 125 … … 92 127 if title and url: 93 128 channel = e.target() 94 95 content = unicode(config.get('bot', 'content'), 'utf-8') 96 title = title 97 129 content = unicoding(config.get('bot', 'content')) 98 130 try: 99 message = ('%(content)s: %(title)s %(url)s' % locals()).encode('iso-2022-jp')131 message = ununicoding(': '.join([content, title, url])) 100 132 except UnicodeError: 101 if __debug__:102 print >> sys.stderr, 'ERR: my message encoding failed: '103 print >> sys.stderr, ' * content> %s' % content104 print >> sys.stderr, ' * title> %s' % title105 print >> sys.stderr, ' * url> %s' % url106 133 return False 134 # ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã 107 135 108 136 c.privmsg(channel, message) 109 110 137 return True 111 112 138 return False 113 139 114 ACTIVE_PATTERN = re.compile( config.get('bot', 'active_pattern'))115 SILENT_PATTERN = re.compile( config.get('bot', 'silent_pattern'))140 ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern'))) 141 SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern'))) 116 142 def silence(self, msg, c, e): 117 143 ch = e.target() 118 active = self.ACTIVE_PATTERN.search(msg .encode('utf-8'))119 silent = self.SILENT_PATTERN.search(msg .encode('utf-8'))144 active = self.ACTIVE_PATTERN.search(msg) 145 silent = self.SILENT_PATTERN.search(msg) 120 146 if __debug__: 121 print >> sys.stderr, ' ACTIVE_PATTERN: %s, SILENT_PATTERN: %s' % (str(active), str(silent))147 print >> sys.stderr, 'DEBUG> ACT_PATT: %s, SIL_PATT: %s' % (str(active), str(silent)) 122 148 123 149 if active: 124 150 self._silent = False 125 c.privmsg(ch, un icode(config.get('bot', 'thanks'), 'utf-8').encode('iso-2022-jp'))151 c.privmsg(ch, ununicoding(config.get('bot', 'thanks'))) 126 152 elif silent: 127 153 self._silent = True 128 c.privmsg(ch, un icode(config.get('bot', 'sorry'), 'utf-8').encode('iso-2022-jp'))154 c.privmsg(ch, ununicoding(config.get('bot', 'sorry'))) 129 155 130 156 def process_keyword(self, keyword): … … 132 158 133 159 class AmazonBot(AmazonBotBase): 160 """ã¢ããŸã³ãããã®å®è£ 161 ã¯ã©ã¹ 162 process_keyword ã¡ãœãã㧠Amazon ãžã¯ãšãªãæããŠçµæãè¿ã 163 """ 134 164 def __init__(self): 135 165 AmazonBotBase.__init__(self) 136 166 167 def get_version(self): 168 return 'AmazonBot by %s, based on python-irclib' % __author__ 169 137 170 def process_keyword(self, keyword): 171 keyword = ununicoding(keyword, 'utf-8') 172 if __debug__: 173 print >> sys.stderr, 'DEBUG> KEYWORD: %s' % ununicoding(keyword, 'euc-jp') 174 138 175 try: 139 176 data = my_amazon.searchByBlended(keyword) 140 177 if type(data.ProductLine) is not type([]): 141 178 data.ProductLine = [data.ProductLine] 142 except my_amazon.AmazonError: 179 except my_amazon.AmazonError, e: 180 if __debug__: 181 print >> sys.stderr, 'DEBUG> Caught AmazonError: %s' % str(e) 143 182 return [None, None] 144 183 … … 146 185 detail = random.choice(product_line.ProductInfo.Details) 147 186 148 url = getattr(detail, 'URL', None)149 product_name = getattr(detail, 'ProductName', None)187 url = unicoding(getattr(detail, 'URL', None)) 188 product_name = unicoding(getattr(detail, 'ProductName', None)) 150 189 151 190 return [product_name, url]
Note: See TracChangeset
for help on using the changeset viewer.