- Timestamp:
- 06/21/06 13:01:35 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/amazonbot/amazonbot.py
r13 r14 83 83 ã㊠Amazon ãžã¯ãšãªãæããã¹ã 84 84 85 ãµãã¯ã©ã¹ã«ã¯ onmsg_HOGEHOGE(self, conn, ev, args) ã¡ãœãããäœãããšã§ã³ãã³ãè¿œå å¯èœ85 ãµãã¯ã©ã¹ã«ã¯ onmsg_HOGEHOGE(self, conn, ev, to, args) ã¡ãœãããäœãããšã§ã³ãã³ãè¿œå å¯èœ 86 86 ã³ãã³ãæžåŒã¯ !HOGEHOGE arg [, arg2, ...] ãšãªã 87 ãã«ãã¯ã¡ãœããã« docstring ãæžãã° OK 87 88 """ 88 89 def __init__(self): … … 109 110 110 111 def on_privmsg(self, c, e): 111 return self.on_pubmsg(c, e )112 113 def on_pubmsg(self, c, e ):112 return self.on_pubmsg(c, e, to=nm_to_n(e.source())) 113 114 def on_pubmsg(self, c, e, to=config.get('irc', 'channel')): 114 115 msg = unicoding(e.arguments()[0]) 116 117 if __debug__: 118 print >> sys.stderr, 'DEBUG> pubmsg incoming "%s", reply to %s' % (ununicoding(msg, 'euc-jp'), to) 119 115 120 if msg[0] == '!': 116 121 words = shlex.split(ununicoding(msg, 'utf-8')[1:]) 117 122 method = getattr(self, 'onmsg_%s' % words[0], lambda *arg: False) 118 return method(c, e, words[1:]) # words[0] == command name123 return method(c, e, to, words[1:]) # words[0] == command name 119 124 120 125 _current_time = time.time() 121 126 if _current_time < self._prev_time + config.get('bot', 'freq', 'int'): 122 127 if __debug__: 123 prev = time.strftime('%y/%m/%d %H:%M:%S', time.localtime(self._prev_time)) 124 print >> sys.stderr, 'DEBUG> Not expired: prev time is %s' % prev 128 prev = time.strftime('%H:%M:%S', time.localtime(self._prev_time)) 129 go = time.strftime('%H:%M:%S', time.localtime(self._prev_time + config.get('bot', 'freq', 'int'))) 130 print >> sys.stderr, 'DEBUG> Not expired: prev time is %s, be expired at: %s' % (prev, go) 125 131 return False 126 132 self._prev_time = _current_time 127 133 128 self.silence(msg, c, e )134 self.silence(msg, c, e, to) 129 135 if self._silent: 130 136 return False … … 138 144 title, url = self.process_keyword(' '.join(nominals)) 139 145 if title and url: 140 channel = e.target()141 146 content = unicoding(config.get('bot', 'content')) 142 147 try: … … 148 153 return False 149 154 150 c. privmsg(channel, message)155 c.notice(to, message) 151 156 return True 152 157 return False … … 154 159 ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern'))) 155 160 SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern'))) 156 def silence(self, msg, c, e): 157 ch = e.target() 161 def silence(self, msg, c, e, to): 158 162 active = self.ACTIVE_PATTERN.search(msg) 159 163 silent = self.SILENT_PATTERN.search(msg) … … 163 167 if active: 164 168 self._silent = False 165 c. privmsg(ch, ununicoding(config.get('bot', 'thanks')))169 c.notice(to, ununicoding(config.get('bot', 'thanks'))) 166 170 elif silent: 167 171 self._silent = True 168 c. privmsg(ch, ununicoding(config.get('bot', 'sorry')))172 c.notice(to, ununicoding(config.get('bot', 'sorry'))) 169 173 170 174 def process_keyword(self, keyword): … … 176 180 process_keyword ã¡ãœãã㧠Amazon ãžã¯ãšãªãæããŠçµæãè¿ã 177 181 """ 182 _AVAIL_PRODUCT_LINES = { 183 'books-jp': '(åæž, default)', 184 'books-us': '(æŽæž)', 185 'music-jp': '(ããã¥ã©ãŒé³æ¥œ)', 186 'classical-jp': '(ã¯ã©ã·ãã¯é³æ¥œ)', 187 'dvd-jp': '(DVD)', 188 'vhs-jp': '(ãããª)', 189 'electronics-jp': '(ãšã¬ã¯ãããã¯ã¹)', 190 'kitchen-jp': '(ããŒã ïŒãããã³)', 191 'software-jp': '(ãœãããŠã§ã¢)', 192 'videogames-jp': '(ã²ãŒã )', 193 'magazines-jp': '(éèª)', 194 'toys-jp': '(ããã¡ãïŒãããŒ)', 195 } 196 178 197 def __init__(self): 179 198 AmazonBotBase.__init__(self) … … 182 201 return 'AmazonBot by %s, based on python-irclib' % __author__ 183 202 184 def onmsg_isbn(self, c, e, args):203 def onmsg_isbn(self, c, e, to, args): 185 204 """Syntax: !isbn <ISBN number> 186 205 """ 187 return self.onmsg_asin(c, e, args)188 def onmsg_asin(self, c, e, args): # FIXME: arg ããªãã£ããšãã®ãšã©ãŒåŠçãè¿œå ããããš206 return self.onmsg_asin(c, e, to, args) 207 def onmsg_asin(self, c, e, to, args): 189 208 """Syntax: !asin <ASIN number> 190 209 """ … … 195 214 data = my_amazon.searchByASIN(args[0]) 196 215 except my_amazon.AmazonError, err: 197 ch = e.target() 198 c.privmsg(ch, ununicoding(config.get('bot', 'no_products'))) 216 c.notice(to, ununicoding(config.get('bot', 'no_products'))) 199 217 if __debug__: 200 218 print >> sys.stderr, 'DEBUG> Caught AmazonError in onmsg_asin: %s' % str(err) 201 219 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, ...] 220 except IndexError, err: 221 c.notice(to, 'Please specify an argument.') 222 return False 223 224 return self._process_onmsg(c, e, to, data) 225 226 def onmsg_k(self, c, e, to, args): return self.onmsg_keyword(c, e, to, args) 227 def onmsg_keyword(self, c, e, to, args): 228 """Syntax: !keyword [-h] [-t type] <keyword1> [, keyword2, ...] 207 229 """ 208 return self.onmsg_amazon(c, e, args) 209 def onmsg_amazon(self, c, e, args): # FIXME: arg ããªãã£ããšãã®ãšã©ãŒåŠçãè¿œå ããããš 210 """Syntax: !amazon [-h] [-t type] keyword1 [, keyword2, ...] 211 """ 212 if __debug__: 213 print >> sys.stderr, 'DEBUG> in amazon command: %s' % str(args) 230 if __debug__: 231 print >> sys.stderr, 'DEBUG> in keyword command: %s' % str(args) 214 232 215 233 try: … … 217 235 except getopt.GetoptError, err: 218 236 if __debug__: 219 print >> sys.stderr, 'DEBUG> Caught GetoptError in onmsg_ amazon: %s' % str(err)220 return False 221 222 keyword = ' '.join(rest) 237 print >> sys.stderr, 'DEBUG> Caught GetoptError in onmsg_keyword: %s' % str(err) 238 return False 239 240 keyword = ' '.join(rest).strip() 223 241 product_line = 'books-jp' 224 242 for opt, val in options: 225 243 if opt in ['-t', '--type']: 244 if val not in self._AVAIL_PRODUCT_LINES.keys(): 245 c.notice(to, 'Type "%s" is not available.' % val) 246 return False 247 226 248 product_line = val 227 249 break 250 228 251 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))) 252 _from = nm_to_n(e.source()) # ãã°ãæµããŠããŸãã®ã§ãã«ãã¯çŽæ¥éä¿¡å 253 ãž 254 c.notice(_from, ununicoding('Available types:')) 255 256 for key, val in self._AVAIL_PRODUCT_LINES.iteritems(): 257 time.sleep(1) # XXX: é£ç¶æçš¿ãããšåŒŸãããããšãããã®ã§æ«å®å¯ŸåŠ 258 c.notice(_from, ununicoding(' * %s: %s' % (key, val))) 259 239 260 return True 261 262 if not keyword: 263 c.notice(to, 'Please specify keywords.') 264 return False 240 265 241 266 if __debug__: … … 246 271 data = my_amazon.searchByKeyword(keyword, product_line=product_line) 247 272 except my_amazon.AmazonError, err: 248 ch = e.target() 249 c.privmsg(ch, ununicoding(config.get('bot', 'no_products'))) 273 c.notice(to, ununicoding(config.get('bot', 'no_products'))) 250 274 if __debug__: 251 275 print >> sys.stderr, 'DEBUG> Caught AmazonError in onmsg_amazon: %s' % str(err) 252 276 return False 253 277 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): 278 return self._process_onmsg(c, e, to, data) 279 280 def onmsg_h(self, c, e, to, args): return self.onmsg_help(c, e, to, args) 281 def onmsg_help(self, c, e, to, args): 282 """Syntax: !help 283 """ 284 if __debug__: 285 print >> sys.stderr, 'DEBUG> in help command: %s' % str(args) 286 287 _from = nm_to_n(e.source()) # ãã°ãæµããŠããŸãã®ã§ãã«ãã¯çŽæ¥éä¿¡å 288 ãž 289 docs = [] 290 for key in dir(self): 291 val = getattr(self, key, '') 292 if __debug__: 293 print >> sys.stderr, 'DEBUG> key=%s, val=%s' % (key, ununicoding(str(val), 'euc-jp')) 294 295 if key[:6] != 'onmsg_': 296 continue 297 298 doc = val.__doc__ 299 if doc: 300 doc = doc.strip() 301 if not doc: 302 continue 303 time.sleep(1) # XXX: é£ç¶æçš¿ãããšåŒŸãããã£ãœãã®ã§æ«å®å¯ŸåŠ 304 c.notice(_from, doc) 305 306 return True 307 308 def _process_onmsg(self, c, e, to, data): 260 309 if type(data.Details) is not list: 261 310 data.Details = [data.Details] … … 264 313 title = ununicoding(detail.ProductName) 265 314 url = ununicoding(detail.URL) 266 channel = config.get('irc', 'channel') 267 c.privmsg(channel, '%(title)s: %(url)s' % locals()) 315 c.notice(to, '%(title)s: %(url)s' % locals()) 268 316 269 317 return True … … 294 342 bot = AmazonBot() 295 343 bot.start() 344 print '> Bye ;)'
Note: See TracChangeset
for help on using the changeset viewer.