[8] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | __version__ = '$Revision$' |
---|
| 5 | __author__ = 'Atzm WATANABE <sitosito@p.chan.ne.jp>' |
---|
| 6 | __date__ = '$Date$' |
---|
[22] | 7 | __copyright__ = 'Copyright(C) 2006-2007 Atzm WATANABE, all rights reserved.' |
---|
[8] | 8 | __license__ = 'Python' |
---|
| 9 | |
---|
| 10 | import re |
---|
[9] | 11 | import sys |
---|
[10] | 12 | import time |
---|
[12] | 13 | import shlex |
---|
[8] | 14 | import random |
---|
[12] | 15 | import getopt |
---|
| 16 | |
---|
[8] | 17 | import MeCab |
---|
[10] | 18 | import nkf |
---|
[8] | 19 | |
---|
| 20 | from ircbot import SingleServerIRCBot |
---|
| 21 | from irclib import nm_to_n |
---|
| 22 | |
---|
[19] | 23 | try: |
---|
| 24 | set, frozenset |
---|
| 25 | except NameError: |
---|
| 26 | from sets import Set as set, ImmutableSet as frozenset |
---|
[8] | 27 | |
---|
[19] | 28 | |
---|
| 29 | import config; config.init() |
---|
| 30 | |
---|
[8] | 31 | import my_amazon |
---|
| 32 | my_amazon.setLocale(config.get('amazon', 'locale')) |
---|
| 33 | my_amazon.setLicense(config.get('amazon', 'access_key')) |
---|
| 34 | |
---|
| 35 | |
---|
[19] | 36 | DEBUG_MSG_TO = sys.stderr |
---|
| 37 | |
---|
| 38 | |
---|
[8] | 39 | def uniq(sequence): |
---|
[19] | 40 | """ãªã¹ãããéè€ãåãé€ã (é çªãçãã®ã§æ³šæ) |
---|
| 41 | """ |
---|
| 42 | return list(set(sequence)) |
---|
[8] | 43 | |
---|
[10] | 44 | def unicoding(text): |
---|
[19] | 45 | """text ã匷å¶çã« unicode ãªããžã§ã¯ãã«å€æ |
---|
| 46 | """ |
---|
| 47 | if type(text) is unicode: |
---|
| 48 | return text |
---|
| 49 | return unicode(nkf.nkf('-w', text), 'utf-8') |
---|
[10] | 50 | |
---|
| 51 | def ununicoding(text, encoding='iso-2022-jp'): |
---|
[19] | 52 | """text ãæå®ããã encoding ã§ãšã³ã³ãŒããïŒraw str ã«åŒ·å¶å€æ |
---|
| 53 | """ |
---|
| 54 | if type(text) is not unicode: |
---|
| 55 | return unicoding(text).encode(encoding) |
---|
| 56 | return text.encode(encoding) |
---|
[10] | 57 | |
---|
[8] | 58 | def mecab_parse(text): |
---|
[19] | 59 | """MeCab ã䜿ã£ãŠåœ¢æ
çŽ è§£æãïŒåºæåè©ãšäžè¬åè©ã ããæœåºãã |
---|
| 60 | """ |
---|
| 61 | def choice_nominal(wlist): |
---|
| 62 | res = [] |
---|
| 63 | for word, wtype in wlist: |
---|
| 64 | wtypes = wtype.split('-') |
---|
| 65 | if 'åºæåè©' in wtypes or ('åè©' in wtypes and 'äžè¬' in wtypes): |
---|
| 66 | res.append(unicoding(word)) |
---|
| 67 | return res |
---|
[8] | 68 | |
---|
[19] | 69 | text = ununicoding(text, 'utf-8') |
---|
| 70 | result = [] |
---|
| 71 | tag = MeCab.Tagger('-Ochasen') |
---|
| 72 | for line in tag.parse(text).split('\n'): |
---|
| 73 | if not line or line == 'EOS': |
---|
| 74 | break |
---|
| 75 | words = line.split() |
---|
| 76 | result.append((words[0], words[-1])) # word, word-type |
---|
[8] | 77 | |
---|
[19] | 78 | result = uniq(choice_nominal(result)) |
---|
| 79 | return result |
---|
[8] | 80 | |
---|
[19] | 81 | def _debug(fmt, *args): |
---|
| 82 | if __debug__: |
---|
| 83 | timeline = time.strftime("%b %d %T", time.localtime()) |
---|
| 84 | try: |
---|
| 85 | fmt = ununicoding(fmt, 'euc-jp') |
---|
| 86 | args = list(args) |
---|
| 87 | for i in range(len(args)): |
---|
| 88 | if isinstance(args[i], basestring): |
---|
| 89 | args[i] = ununicoding(args[i], 'euc-jp') |
---|
| 90 | |
---|
| 91 | print >> DEBUG_MSG_TO, '(%s) <DEBUG>' % timeline, |
---|
| 92 | print >> DEBUG_MSG_TO, fmt % tuple(args) |
---|
| 93 | |
---|
| 94 | except: |
---|
| 95 | print >> DEBUG_MSG_TO, '(%s) <DEBUG>' % timeline, |
---|
| 96 | print >> DEBUG_MSG_TO, '!! debug message print failed !!' |
---|
| 97 | |
---|
[8] | 98 | class AmazonBotBase(SingleServerIRCBot): |
---|
[19] | 99 | """ã¢ããŸã³ãããã®ããŒã¹ã¯ã©ã¹ |
---|
| 100 | åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ
çŽ è§£æãšåè©æœåºãŸã§ãããããªã |
---|
| 101 | ãµãã¯ã©ã¹ã§ process_keyword ãå®è£
ã㊠Amazon ãžã¯ãšãªãæããã¹ã |
---|
[12] | 102 | |
---|
[19] | 103 | ãµãã¯ã©ã¹ã«ã¯ onmsg_HOGEHOGE(self, conn, ev, to, args) ã¡ãœãããäœãããšã§ã³ãã³ãè¿œå å¯èœ |
---|
| 104 | ã³ãã³ãæžåŒã¯ !HOGEHOGE arg [, arg2, ...] ãšãªã |
---|
| 105 | ãã«ãã¯ã¡ãœããã« docstring ãæžãã° OK |
---|
| 106 | """ |
---|
| 107 | def __init__(self): |
---|
| 108 | _server = [(config.get('irc', 'server'), config.get('irc', 'port', 'int'))] |
---|
| 109 | _nick = config.get('bot', 'nick') |
---|
[8] | 110 | |
---|
[19] | 111 | self._current_lines = 0 |
---|
| 112 | self._prev_time = time.time() - config.get('freq', 'timeout', 'int') |
---|
| 113 | self._silent = False |
---|
| 114 | SingleServerIRCBot.__init__(self, _server, _nick, _nick) |
---|
[8] | 115 | |
---|
[19] | 116 | def start(self): |
---|
| 117 | try: |
---|
| 118 | SingleServerIRCBot.start(self) |
---|
| 119 | except KeyboardInterrupt: |
---|
| 120 | self.die(ununicoding(config.get('bot', 'bye'))) |
---|
[8] | 121 | |
---|
[19] | 122 | def on_welcome(self, c, e): |
---|
| 123 | c.join(config.get('irc', 'channel')) |
---|
| 124 | _debug('Joined %s', config.get('irc', 'channel')) |
---|
[8] | 125 | |
---|
[19] | 126 | def on_nicknameinuse(self, c, e): |
---|
| 127 | c.nick(c.get_nickname() + '_') |
---|
[8] | 128 | |
---|
[19] | 129 | def on_privmsg(self, c, e): |
---|
| 130 | return self.on_pubmsg(c, e, to=nm_to_n(e.source())) |
---|
[8] | 131 | |
---|
[19] | 132 | def on_pubmsg(self, c, e, to=config.get('irc', 'channel')): |
---|
| 133 | msg = unicoding(e.arguments()[0]) |
---|
| 134 | _debug('pubmsg incoming "%s", should be reply to %s', msg, to) |
---|
[14] | 135 | |
---|
[19] | 136 | if msg[0] == '!': |
---|
| 137 | try: |
---|
| 138 | words = shlex.split(ununicoding(msg, 'utf-8')[1:]) |
---|
| 139 | except: |
---|
| 140 | return False |
---|
| 141 | if not words: |
---|
| 142 | return False |
---|
| 143 | method = getattr(self, 'onmsg_%s' % words[0], lambda *arg: False) |
---|
| 144 | return method(c, e, to, words[1:]) # words[0] == command name |
---|
[14] | 145 | |
---|
[22] | 146 | self.message_action(msg, c, e, to) |
---|
| 147 | |
---|
[20] | 148 | # silence |
---|
| 149 | self.silence(msg, c, e, to) |
---|
| 150 | if self._silent: |
---|
| 151 | return False |
---|
| 152 | |
---|
[19] | 153 | # freq_lines |
---|
| 154 | self._current_lines += 1 |
---|
| 155 | _freq_lines = config.get('freq', 'lines', 'int') |
---|
| 156 | if _freq_lines: |
---|
| 157 | if config.get('freq', 'lines_random', 'boolean'): |
---|
| 158 | _freq_lines = random.randint(int(_freq_lines/2)+1, _freq_lines) |
---|
[12] | 159 | |
---|
[19] | 160 | _debug('Line count: now %d, next: %d', self._current_lines, _freq_lines) |
---|
[18] | 161 | |
---|
[19] | 162 | if self._current_lines < _freq_lines: |
---|
| 163 | return False |
---|
| 164 | self._current_lines = 0 |
---|
[18] | 165 | |
---|
[19] | 166 | # freq |
---|
| 167 | _current_time = time.time() |
---|
| 168 | if _current_time < self._prev_time + config.get('freq', 'timeout', 'int'): |
---|
| 169 | cur = time.strftime('%H:%M:%S', time.localtime(_current_time)) |
---|
| 170 | go = time.strftime('%H:%M:%S', time.localtime( |
---|
| 171 | self._prev_time + config.get('freq', 'timeout', 'int'))) |
---|
| 172 | _debug('Not expired: now %s, be expired at: %s', cur, go) |
---|
| 173 | return False |
---|
| 174 | self._prev_time = _current_time |
---|
[18] | 175 | |
---|
[19] | 176 | nominals = mecab_parse(msg) |
---|
| 177 | if not nominals: |
---|
| 178 | _debug("Couldn't find nominal words") |
---|
| 179 | return False |
---|
[8] | 180 | |
---|
[19] | 181 | title, url = self.process_keyword(' '.join(nominals)) |
---|
| 182 | if title and url: |
---|
| 183 | content = unicoding(config.get('bot', 'content')) |
---|
| 184 | try: |
---|
| 185 | message = ununicoding(': '.join([content, title, url])) |
---|
| 186 | except UnicodeError, err: |
---|
| 187 | # ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã |
---|
| 188 | _debug('%s', str(err)) |
---|
| 189 | return False |
---|
[8] | 190 | |
---|
[19] | 191 | c.notice(to, message) |
---|
| 192 | return True |
---|
| 193 | return False |
---|
[9] | 194 | |
---|
[19] | 195 | ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern'))) |
---|
| 196 | SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern'))) |
---|
| 197 | def silence(self, msg, c, e, to): |
---|
| 198 | active = self.ACTIVE_PATTERN.search(msg) |
---|
| 199 | silent = self.SILENT_PATTERN.search(msg) |
---|
| 200 | _debug('ACT_PATT: %s, SIL_PATT: %s', str(active), str(silent)) |
---|
[8] | 201 | |
---|
[19] | 202 | if active: |
---|
| 203 | self._silent = False |
---|
| 204 | c.notice(to, ununicoding(config.get('bot', 'thanks'))) |
---|
| 205 | elif silent: |
---|
| 206 | self._silent = True |
---|
| 207 | c.notice(to, ununicoding(config.get('bot', 'sorry'))) |
---|
[9] | 208 | |
---|
[19] | 209 | def process_keyword(self, keyword): |
---|
| 210 | return [None, None] |
---|
[8] | 211 | |
---|
[20] | 212 | def is_silent(self): |
---|
| 213 | return self._silent |
---|
| 214 | def get_current_lines(self): |
---|
| 215 | return self._current_lines |
---|
| 216 | def get_prev_time(self): |
---|
| 217 | return self._prev_time |
---|
| 218 | |
---|
[22] | 219 | def message_action(self, msg, c, e, to): |
---|
| 220 | for i in xrange(100): |
---|
| 221 | action = 'action%d' % i |
---|
| 222 | if not config.has_section(action): |
---|
| 223 | break |
---|
| 224 | |
---|
| 225 | c_stime = config.get(action, 'start_time') |
---|
| 226 | c_etime = config.get(action, 'end_time') |
---|
| 227 | |
---|
| 228 | try: |
---|
| 229 | if c_stime and c_etime: |
---|
| 230 | now = time.time() |
---|
| 231 | [now_y, now_m, now_d] = time.localtime(now)[:3] |
---|
| 232 | |
---|
| 233 | stime = '%04d/%02d/%02d %s' % (now_y, now_m, now_d, c_stime) |
---|
| 234 | etime = '%04d/%02d/%02d %s' % (now_y, now_m, now_d, c_etime) |
---|
| 235 | stime = time.mktime(time.strptime(stime, '%Y/%m/%d %H:%M')) |
---|
| 236 | etime = time.mktime(time.strptime(etime, '%Y/%m/%d %H:%M')) |
---|
| 237 | |
---|
| 238 | if not ((stime <= now) and (now <= etime)): |
---|
| 239 | _debug('Out of time: %s - %s' % (c_stime, c_etime)) |
---|
| 240 | continue |
---|
| 241 | except: |
---|
| 242 | _debug('Invalid time: %s - %s' % (str(c_stime), str(c_etime))) |
---|
| 243 | continue |
---|
| 244 | |
---|
| 245 | match = re.search(unicoding(config.get(action, 'input_pattern')), msg) |
---|
| 246 | if not match: |
---|
| 247 | continue |
---|
| 248 | |
---|
| 249 | act = config.get(action, 'action') |
---|
| 250 | fmt = config.get(action, 'message') |
---|
| 251 | try: |
---|
| 252 | _from = nm_to_n(e.source()) |
---|
| 253 | message = ununicoding(fmt % _from) |
---|
| 254 | |
---|
| 255 | if not message: |
---|
| 256 | _debug('No message specified') |
---|
| 257 | continue |
---|
| 258 | |
---|
| 259 | if not act: |
---|
| 260 | c.notice(to, message) |
---|
| 261 | continue |
---|
| 262 | |
---|
| 263 | method = getattr(self, 'onact_%s' % act, lambda *arg: False) |
---|
| 264 | method(message, c, e, to) |
---|
| 265 | |
---|
[23] | 266 | except: |
---|
[22] | 267 | _debug('Action failed: %s (%s)' % (str(act), str(fmt))) |
---|
| 268 | |
---|
| 269 | return True |
---|
| 270 | |
---|
[8] | 271 | class AmazonBot(AmazonBotBase): |
---|
[19] | 272 | """ã¢ããŸã³ãããã®å®è£
ã¯ã©ã¹ |
---|
| 273 | process_keyword ã¡ãœãã㧠Amazon ãžã¯ãšãªãæããŠçµæãè¿ã |
---|
| 274 | """ |
---|
| 275 | _AVAIL_PRODUCT_LINES = { |
---|
| 276 | 'books-jp': '(åæž, default)', |
---|
| 277 | 'books-us': '(æŽæž)', |
---|
| 278 | 'music-jp': '(ããã¥ã©ãŒé³æ¥œ)', |
---|
| 279 | 'classical-jp': '(ã¯ã©ã·ãã¯é³æ¥œ)', |
---|
| 280 | 'dvd-jp': '(DVD)', |
---|
| 281 | 'vhs-jp': '(ãããª)', |
---|
| 282 | 'electronics-jp': '(ãšã¬ã¯ãããã¯ã¹)', |
---|
| 283 | 'kitchen-jp': '(ããŒã ïŒãããã³)', |
---|
| 284 | 'software-jp': '(ãœãããŠã§ã¢)', |
---|
| 285 | 'videogames-jp': '(ã²ãŒã )', |
---|
| 286 | 'magazines-jp': '(éèª)', |
---|
| 287 | 'toys-jp': '(ããã¡ãïŒãããŒ)', |
---|
| 288 | } |
---|
[14] | 289 | |
---|
[19] | 290 | def __init__(self): |
---|
| 291 | AmazonBotBase.__init__(self) |
---|
[8] | 292 | |
---|
[19] | 293 | def get_version(self): |
---|
| 294 | return 'AmazonBot by %s, based on python-irclib' % __author__ |
---|
[10] | 295 | |
---|
[22] | 296 | def onmsg_lt(self, c, e, to, args): return self.onmsg_localtime(c, e, to, args) |
---|
| 297 | def onmsg_localtime(self, c, e, to, args): |
---|
| 298 | """Syntax: !localtime <unix time> |
---|
| 299 | """ |
---|
| 300 | _debug('in localtime command: %s', str(args)) |
---|
| 301 | |
---|
| 302 | _from = nm_to_n(e.source()) |
---|
| 303 | try: |
---|
| 304 | sec = float(args[0]) |
---|
| 305 | c.notice(_from, time.strftime('%b %d %T', time.localtime(sec))) |
---|
| 306 | except ValueError, e: |
---|
| 307 | c.notice(_from, 'Invalid argument: %s' % args[0]) |
---|
| 308 | return False |
---|
| 309 | |
---|
| 310 | return True |
---|
| 311 | |
---|
[20] | 312 | def onmsg_s(self, c, e, to, args): return self.onmsg_status(c, e, to, args) |
---|
| 313 | def onmsg_status(self, c, e, to, args): |
---|
| 314 | """Syntax: !status |
---|
| 315 | """ |
---|
| 316 | _debug('in status command: %s', str(args)) |
---|
| 317 | |
---|
| 318 | c.notice(to, 'silent: %s' % self.is_silent()) |
---|
| 319 | c.notice(to, 'current lines: %d' % self.get_current_lines()) |
---|
| 320 | c.notice(to, time.strftime('previous time: %b %d %T', |
---|
| 321 | time.localtime(self.get_prev_time()))) |
---|
| 322 | return True |
---|
| 323 | |
---|
[19] | 324 | def onmsg_isbn(self, c, e, to, args): |
---|
| 325 | """Syntax: !isbn <ISBN number> |
---|
| 326 | """ |
---|
| 327 | return self.onmsg_asin(c, e, to, args) |
---|
| 328 | def onmsg_asin(self, c, e, to, args): |
---|
| 329 | """Syntax: !asin <ASIN number> |
---|
| 330 | """ |
---|
| 331 | _debug('in asin command: %s', str(args)) |
---|
[12] | 332 | |
---|
[19] | 333 | try: |
---|
| 334 | data = my_amazon.searchByASIN(args[0]) |
---|
| 335 | except my_amazon.AmazonError, err: |
---|
| 336 | c.notice(to, ununicoding(config.get('bot', 'no_products'))) |
---|
| 337 | _debug('Caught AmazonError in onmsg_asin: %s', str(err)) |
---|
| 338 | return False |
---|
| 339 | except IndexError, err: |
---|
| 340 | c.notice(to, 'Please specify an argument.') |
---|
| 341 | return False |
---|
[12] | 342 | |
---|
[19] | 343 | return self._process_onmsg(c, e, to, data) |
---|
[12] | 344 | |
---|
[19] | 345 | def onmsg_k(self, c, e, to, args): return self.onmsg_keyword(c, e, to, args) |
---|
| 346 | def onmsg_keyword(self, c, e, to, args): |
---|
| 347 | """Syntax: !keyword [-h] [-t type] <keyword1> [, keyword2, ...] |
---|
| 348 | """ |
---|
| 349 | _debug('in keyword command: %s', str(args)) |
---|
[12] | 350 | |
---|
[19] | 351 | try: |
---|
| 352 | options, rest = getopt.getopt(args, 't:h', ['type=', 'help']) |
---|
| 353 | except getopt.GetoptError, err: |
---|
| 354 | _debug('Caught GetoptError in onmsg_keyword: %s', str(err)) |
---|
| 355 | return False |
---|
[12] | 356 | |
---|
[19] | 357 | keyword = ' '.join(rest).strip() |
---|
| 358 | product_line = 'books-jp' |
---|
| 359 | for opt, val in options: |
---|
| 360 | if opt in ['-t', '--type']: |
---|
| 361 | if val not in self._AVAIL_PRODUCT_LINES.keys(): |
---|
| 362 | c.notice(to, 'Type "%s" is not available.' % val) |
---|
| 363 | return False |
---|
[14] | 364 | |
---|
[19] | 365 | product_line = val |
---|
| 366 | break |
---|
[14] | 367 | |
---|
[19] | 368 | elif opt in ['-h', '--help']: |
---|
| 369 | _from = nm_to_n(e.source()) # ãã°ãæµããŠããŸãã®ã§ãã«ãã¯çŽæ¥éä¿¡å
ãž |
---|
| 370 | c.notice(_from, ununicoding('Available types:')) |
---|
[14] | 371 | |
---|
[19] | 372 | for key, val in self._AVAIL_PRODUCT_LINES.iteritems(): |
---|
| 373 | time.sleep(1) # XXX: é£ç¶æçš¿ãããšåŒŸãããããšãããã®ã§æ«å®å¯ŸåŠ |
---|
| 374 | c.notice(_from, ununicoding(' * %s: %s' % (key, val))) |
---|
[14] | 375 | |
---|
[19] | 376 | return True |
---|
[12] | 377 | |
---|
[19] | 378 | if not keyword: |
---|
| 379 | c.notice(to, 'Please specify keywords.') |
---|
| 380 | return False |
---|
[14] | 381 | |
---|
[19] | 382 | _debug('keyword="%s", product_line=%s', keyword, product_line) |
---|
[12] | 383 | |
---|
[19] | 384 | try: |
---|
| 385 | data = my_amazon.searchByKeyword(keyword, product_line=product_line) |
---|
| 386 | except my_amazon.AmazonError, err: |
---|
| 387 | c.notice(to, ununicoding(config.get('bot', 'no_products'))) |
---|
| 388 | _debug('Caught AmazonError in onmsg_amazon: %s', str(err)) |
---|
| 389 | return False |
---|
[12] | 390 | |
---|
[19] | 391 | return self._process_onmsg(c, e, to, data) |
---|
[12] | 392 | |
---|
[19] | 393 | def onmsg_h(self, c, e, to, args): return self.onmsg_help(c, e, to, args) |
---|
| 394 | def onmsg_help(self, c, e, to, args): |
---|
| 395 | """Syntax: !help |
---|
| 396 | """ |
---|
| 397 | _debug('in help command: %s', str(args)) |
---|
[12] | 398 | |
---|
[19] | 399 | _from = nm_to_n(e.source()) # ãã°ãæµããŠããŸãã®ã§ãã«ãã¯çŽæ¥éä¿¡å
ãž |
---|
[20] | 400 | c.notice(_from, self.get_version()) |
---|
| 401 | |
---|
[19] | 402 | docs = [] |
---|
| 403 | for key in dir(self): |
---|
| 404 | val = getattr(self, key, '') |
---|
| 405 | _debug('key=%s, val=%s', key, str(val)) |
---|
[14] | 406 | |
---|
[19] | 407 | if key[:6] != 'onmsg_': |
---|
| 408 | continue |
---|
[14] | 409 | |
---|
[19] | 410 | doc = val.__doc__ |
---|
| 411 | if doc: |
---|
| 412 | doc = doc.strip() |
---|
| 413 | if not doc: |
---|
| 414 | continue |
---|
| 415 | time.sleep(1) # XXX: é£ç¶æçš¿ãããšåŒŸãããã£ãœãã®ã§æ«å®å¯ŸåŠ |
---|
| 416 | c.notice(_from, doc) |
---|
[14] | 417 | |
---|
[19] | 418 | return True |
---|
[14] | 419 | |
---|
[19] | 420 | def _process_onmsg(self, c, e, to, data): |
---|
| 421 | if type(data.Details) is not list: |
---|
| 422 | data.Details = [data.Details] |
---|
[12] | 423 | |
---|
[19] | 424 | detail = random.choice(data.Details) |
---|
| 425 | title = ununicoding(detail.ProductName) |
---|
| 426 | url = ununicoding(detail.URL) |
---|
| 427 | c.notice(to, '%(title)s: %(url)s' % locals()) |
---|
[12] | 428 | |
---|
[19] | 429 | return True |
---|
[12] | 430 | |
---|
[19] | 431 | def process_keyword(self, keyword): |
---|
| 432 | keyword = ununicoding(keyword, 'utf-8') |
---|
| 433 | _debug('KEYWORD: %s', keyword) |
---|
[10] | 434 | |
---|
[19] | 435 | try: |
---|
| 436 | data = my_amazon.searchByBlended(keyword) |
---|
| 437 | if type(data.ProductLine) is not type([]): |
---|
| 438 | data.ProductLine = [data.ProductLine] |
---|
| 439 | except my_amazon.AmazonError, err: |
---|
| 440 | _debug('Caught AmazonError: %s', str(err)) |
---|
| 441 | return [None, None] |
---|
[8] | 442 | |
---|
[19] | 443 | product_line = random.choice(data.ProductLine) |
---|
| 444 | detail = random.choice(product_line.ProductInfo.Details) |
---|
[8] | 445 | |
---|
[19] | 446 | url = unicoding(getattr(detail, 'URL', None)) |
---|
| 447 | product_name = unicoding(getattr(detail, 'ProductName', None)) |
---|
[8] | 448 | |
---|
[19] | 449 | return [product_name, url] |
---|
[8] | 450 | |
---|
[22] | 451 | def onact_oper(self, msg, c, e, to): |
---|
| 452 | nick = nm_to_n(e.source()) |
---|
| 453 | _debug('in oper action: %s to %s in %s' % (msg, nick, to)) |
---|
| 454 | c.notice(to, msg) |
---|
| 455 | c.mode(to, '+o %s' % nick) |
---|
| 456 | return True |
---|
| 457 | |
---|
| 458 | def onact_nooper(self, msg, c, e, to): |
---|
| 459 | nick = nm_to_n(e.source()) |
---|
| 460 | _debug('in nooper action: %s to %s in %s' % (msg, nick, to)) |
---|
| 461 | c.notice(to, msg) |
---|
| 462 | c.mode(to, '-o %s' % nick) |
---|
| 463 | return True |
---|
| 464 | |
---|
| 465 | def onact_kick(self, msg, c, e, to): |
---|
| 466 | nick = nm_to_n(e.source()) |
---|
| 467 | _debug('in kick action: %s to %s in %s' % (msg, nick, to)) |
---|
| 468 | c.kick(to, nick, msg) |
---|
| 469 | return True |
---|
| 470 | |
---|
| 471 | def onact_kick_and_invite(self, msg, c, e, to): |
---|
| 472 | nick = nm_to_n(e.source()) |
---|
| 473 | _debug('in kick_and_invite action: %s to %s in %s' % (msg, nick, to)) |
---|
| 474 | c.kick(to, nick, msg) |
---|
| 475 | c.invite(nick, to) |
---|
| 476 | return True |
---|
| 477 | |
---|
| 478 | def onact_nick(self, msg, c, e, to): |
---|
| 479 | nick = nm_to_n(e.source()) |
---|
| 480 | _debug('in nick action: %s to %s in %s' % (msg, nick, to)) |
---|
| 481 | c.notice(to, msg) |
---|
| 482 | c.nick('%s_' % nick) |
---|
| 483 | return True |
---|
| 484 | |
---|
| 485 | def onact_topic(self, msg, c, e, to): |
---|
| 486 | nick = nm_to_n(e.source()) |
---|
| 487 | _debug('in topic action: %s to %s in %s' % (msg, nick, to)) |
---|
| 488 | c.topic(to, msg) |
---|
| 489 | return True |
---|
| 490 | |
---|
[8] | 491 | if __name__ == '__main__': |
---|
[19] | 492 | bot = AmazonBot() |
---|
| 493 | bot.start() |
---|
| 494 | print '> Bye ;)' |
---|