Changeset 12 for trunk/amazonbot
- Timestamp:
- 06/20/06 18:41:29 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/amazonbot/amazonbot.py
r11 r12 11 11 import sys 12 12 import time 13 import shlex 13 14 import random 15 import getopt 16 14 17 import MeCab 15 18 import nkf … … 75 78 class AmazonBotBase(SingleServerIRCBot): 76 79 """ã¢ããŸã³ãããã®ããŒã¹ã¯ã©ã¹ 77 ããã€åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ80 åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ 78 81 çŽ è§£æãšåè©æœåºãŸã§ãããããªã 79 82 ãµãã¯ã©ã¹ã§ process_keyword ãå®è£ 80 83 ã㊠Amazon ãžã¯ãšãªãæããã¹ã 84 85 ãµãã¯ã©ã¹ã«ã¯ onmsg_HOGEHOGE(self, conn, ev, args) ã¡ãœãããäœãããšã§ã³ãã³ãè¿œå å¯èœ 86 ã³ãã³ãæžåŒã¯ !HOGEHOGE arg [, arg2, ...] ãšãªã 81 87 """ 82 88 def __init__(self): … … 106 112 107 113 def on_pubmsg(self, c, e): 114 msg = unicoding(e.arguments()[0]) 115 if msg[0] == '!': 116 words = shlex.split(ununicoding(msg, 'utf-8')[1:]) 117 method = getattr(self, 'onmsg_%s' % words[0], lambda *arg: False) 118 return method(c, e, words[1:]) # words[0] == command name 119 108 120 _current_time = time.time() 109 121 if _current_time < self._prev_time + config.get('bot', 'freq', 'int'): … … 112 124 print >> sys.stderr, 'DEBUG> Not expired: prev time is %s' % prev 113 125 return False 114 115 126 self._prev_time = _current_time 116 msg = unicoding(e.arguments()[0])117 127 118 128 self.silence(msg, c, e) … … 132 142 try: 133 143 message = ununicoding(': '.join([content, title, url])) 134 except UnicodeError, e :144 except UnicodeError, err: 135 145 # ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã 136 146 if __debug__: 137 print >> sys.stderr, 'DEBUG> %s' % str(e )147 print >> sys.stderr, 'DEBUG> %s' % str(err) 138 148 return False 139 149 … … 172 182 return 'AmazonBot by %s, based on python-irclib' % __author__ 173 183 184 def onmsg_isbn(self, c, e, args): 185 """Syntax: !isbn <ISBN number> 186 """ 187 return self.onmsg_asin(c, e, args) 188 def onmsg_asin(self, c, e, args): 189 """Syntax: !asin <ASIN number> 190 """ 191 if __debug__: 192 print >> sys.stderr, 'DEBUG> in asin command: %s' % str(args) 193 194 try: 195 data = my_amazon.searchByASIN(args[0]) 196 except my_amazon.AmazonError, err: 197 ch = e.target() 198 c.privmsg(ch, ununicoding(config.get('bot', 'no_products'))) 199 if __debug__: 200 print >> sys.stderr, 'DEBUG> Caught AmazonError in onmsg_asin: %s' % str(err) 201 return False 202 203 return self._process_onmsg(c, e, data) 204 205 def onmsg_a(self, c, e, args): 206 """Syntax: !a [-h] [-t type] keyword1 [, keyword2, ...] 207 """ 208 return self.onmsg_amazon(c, e, args) 209 def onmsg_amazon(self, c, e, args): 210 """Syntax: !amazon [-h] [-t type] keyword1 [, keyword2, ...] 211 """ 212 if __debug__: 213 print >> sys.stderr, 'DEBUG> in amazon command: %s' % str(args) 214 215 try: 216 options, rest = getopt.getopt(args, 't:h', ['type=', 'help']) 217 except getopt.GetoptError, err: 218 if __debug__: 219 print >> sys.stderr, 'DEBUG> Caught GetoptError in onmsg_amazon: %s' % str(err) 220 return False 221 222 keyword = ' '.join(rest) 223 product_line = 'books-jp' 224 for opt, val in options: 225 if opt in ['-t', '--type']: 226 product_line = val 227 break 228 elif opt in ['-h', '--help']: 229 available = [ 230 'books-jp (åæž, default)', 'books-us (æŽæž)', 231 'music-jp (ããã¥ã©ãŒé³æ¥œ)', 'classical-jp (ã¯ã©ã·ãã¯é³æ¥œ)', 232 'dvd-jp (DVD)', 'vhs-jp (ãããª)', 233 'electronics-jp (ãšã¬ã¯ãããã¯ã¹)', 'kitchen-jp (ããŒã ïŒãããã³)', 234 'software-jp (ãœãããŠã§ã¢)', 'videogames-jp (ã²ãŒã )', 235 'magazines-jp (éèª)', 'toys-jp (ããã¡ãïŒãããŒ)', 236 ] 237 ch = e.target() 238 c.privmsg(ch, ununicoding('Available types: ' + ', '.join(available))) 239 return True 240 241 if __debug__: 242 fmt = 'DEBUG> keyword="%s", product_line=%s' 243 print >> sys.stderr, fmt % (ununicoding(keyword, 'euc-jp'), product_line) 244 245 try: 246 data = my_amazon.searchByKeyword(keyword, product_line=product_line) 247 except my_amazon.AmazonError, err: 248 ch = e.target() 249 c.privmsg(ch, ununicoding(config.get('bot', 'no_products'))) 250 if __debug__: 251 print >> sys.stderr, 'DEBUG> Caught AmazonError in onmsg_amazon: %s' % str(err) 252 return False 253 254 return self._process_onmsg(c, e, data) 255 256 def onmsg_help(self, c, e, data): 257 pass 258 259 def _process_onmsg(self, c, e, data): 260 if type(data.Details) is not list: 261 data.Details = [data.Details] 262 263 detail = random.choice(data.Details) 264 title = ununicoding(detail.ProductName) 265 url = ununicoding(detail.URL) 266 channel = config.get('irc', 'channel') 267 c.privmsg(channel, '%(title)s: %(url)s' % locals()) 268 269 return True 270 174 271 def process_keyword(self, keyword): 175 272 keyword = ununicoding(keyword, 'utf-8') … … 181 278 if type(data.ProductLine) is not type([]): 182 279 data.ProductLine = [data.ProductLine] 183 except my_amazon.AmazonError, e :184 if __debug__: 185 print >> sys.stderr, 'DEBUG> Caught AmazonError: %s' % str(e )280 except my_amazon.AmazonError, err: 281 if __debug__: 282 print >> sys.stderr, 'DEBUG> Caught AmazonError: %s' % str(err) 186 283 return [None, None] 187 284
Note: See TracChangeset
for help on using the changeset viewer.