[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$' |
---|
| 7 | __copyright__ = 'Copyright(C) 2006 Atzm WATANABE, all rights reserved.' |
---|
| 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 | |
---|
[19] | 146 | # freq_lines |
---|
| 147 | self._current_lines += 1 |
---|
| 148 | _freq_lines = config.get('freq', 'lines', 'int') |
---|
| 149 | if _freq_lines: |
---|
| 150 | if config.get('freq', 'lines_random', 'boolean'): |
---|
| 151 | _freq_lines = random.randint(int(_freq_lines/2)+1, _freq_lines) |
---|
[12] | 152 | |
---|
[19] | 153 | _debug('Line count: now %d, next: %d', self._current_lines, _freq_lines) |
---|
[18] | 154 | |
---|
[19] | 155 | if self._current_lines < _freq_lines: |
---|
| 156 | return False |
---|
| 157 | self._current_lines = 0 |
---|
[18] | 158 | |
---|
[19] | 159 | # freq |
---|
| 160 | _current_time = time.time() |
---|
| 161 | if _current_time < self._prev_time + config.get('freq', 'timeout', 'int'): |
---|
| 162 | cur = time.strftime('%H:%M:%S', time.localtime(_current_time)) |
---|
| 163 | go = time.strftime('%H:%M:%S', time.localtime( |
---|
| 164 | self._prev_time + config.get('freq', 'timeout', 'int'))) |
---|
| 165 | _debug('Not expired: now %s, be expired at: %s', cur, go) |
---|
| 166 | return False |
---|
| 167 | self._prev_time = _current_time |
---|
[18] | 168 | |
---|
[19] | 169 | # silence |
---|
| 170 | self.silence(msg, c, e, to) |
---|
| 171 | if self._silent: |
---|
| 172 | return False |
---|
[10] | 173 | |
---|
[19] | 174 | nominals = mecab_parse(msg) |
---|
| 175 | if not nominals: |
---|
| 176 | _debug("Couldn't find nominal words") |
---|
| 177 | return False |
---|
[8] | 178 | |
---|
[19] | 179 | title, url = self.process_keyword(' '.join(nominals)) |
---|
| 180 | if title and url: |
---|
| 181 | content = unicoding(config.get('bot', 'content')) |
---|
| 182 | try: |
---|
| 183 | message = ununicoding(': '.join([content, title, url])) |
---|
| 184 | except UnicodeError, err: |
---|
| 185 | # ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã |
---|
| 186 | _debug('%s', str(err)) |
---|
| 187 | return False |
---|
[8] | 188 | |
---|
[19] | 189 | c.notice(to, message) |
---|
| 190 | return True |
---|
| 191 | return False |
---|
[9] | 192 | |
---|
[19] | 193 | ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern'))) |
---|
| 194 | SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern'))) |
---|
| 195 | def silence(self, msg, c, e, to): |
---|
| 196 | active = self.ACTIVE_PATTERN.search(msg) |
---|
| 197 | silent = self.SILENT_PATTERN.search(msg) |
---|
| 198 | _debug('ACT_PATT: %s, SIL_PATT: %s', str(active), str(silent)) |
---|
[8] | 199 | |
---|
[19] | 200 | if active: |
---|
| 201 | self._silent = False |
---|
| 202 | c.notice(to, ununicoding(config.get('bot', 'thanks'))) |
---|
| 203 | elif silent: |
---|
| 204 | self._silent = True |
---|
| 205 | c.notice(to, ununicoding(config.get('bot', 'sorry'))) |
---|
[9] | 206 | |
---|
[19] | 207 | def process_keyword(self, keyword): |
---|
| 208 | return [None, None] |
---|
[8] | 209 | |
---|
| 210 | class AmazonBot(AmazonBotBase): |
---|
[19] | 211 | """ã¢ããŸã³ãããã®å®è£
ã¯ã©ã¹ |
---|
| 212 | process_keyword ã¡ãœãã㧠Amazon ãžã¯ãšãªãæããŠçµæãè¿ã |
---|
| 213 | """ |
---|
| 214 | _AVAIL_PRODUCT_LINES = { |
---|
| 215 | 'books-jp': '(åæž, default)', |
---|
| 216 | 'books-us': '(æŽæž)', |
---|
| 217 | 'music-jp': '(ããã¥ã©ãŒé³æ¥œ)', |
---|
| 218 | 'classical-jp': '(ã¯ã©ã·ãã¯é³æ¥œ)', |
---|
| 219 | 'dvd-jp': '(DVD)', |
---|
| 220 | 'vhs-jp': '(ãããª)', |
---|
| 221 | 'electronics-jp': '(ãšã¬ã¯ãããã¯ã¹)', |
---|
| 222 | 'kitchen-jp': '(ããŒã ïŒãããã³)', |
---|
| 223 | 'software-jp': '(ãœãããŠã§ã¢)', |
---|
| 224 | 'videogames-jp': '(ã²ãŒã )', |
---|
| 225 | 'magazines-jp': '(éèª)', |
---|
| 226 | 'toys-jp': '(ããã¡ãïŒãããŒ)', |
---|
| 227 | } |
---|
[14] | 228 | |
---|
[19] | 229 | def __init__(self): |
---|
| 230 | AmazonBotBase.__init__(self) |
---|
[8] | 231 | |
---|
[19] | 232 | def get_version(self): |
---|
| 233 | return 'AmazonBot by %s, based on python-irclib' % __author__ |
---|
[10] | 234 | |
---|
[19] | 235 | def onmsg_isbn(self, c, e, to, args): |
---|
| 236 | """Syntax: !isbn <ISBN number> |
---|
| 237 | """ |
---|
| 238 | return self.onmsg_asin(c, e, to, args) |
---|
| 239 | def onmsg_asin(self, c, e, to, args): |
---|
| 240 | """Syntax: !asin <ASIN number> |
---|
| 241 | """ |
---|
| 242 | _debug('in asin command: %s', str(args)) |
---|
[12] | 243 | |
---|
[19] | 244 | try: |
---|
| 245 | data = my_amazon.searchByASIN(args[0]) |
---|
| 246 | except my_amazon.AmazonError, err: |
---|
| 247 | c.notice(to, ununicoding(config.get('bot', 'no_products'))) |
---|
| 248 | _debug('Caught AmazonError in onmsg_asin: %s', str(err)) |
---|
| 249 | return False |
---|
| 250 | except IndexError, err: |
---|
| 251 | c.notice(to, 'Please specify an argument.') |
---|
| 252 | return False |
---|
[12] | 253 | |
---|
[19] | 254 | return self._process_onmsg(c, e, to, data) |
---|
[12] | 255 | |
---|
[19] | 256 | def onmsg_k(self, c, e, to, args): return self.onmsg_keyword(c, e, to, args) |
---|
| 257 | def onmsg_keyword(self, c, e, to, args): |
---|
| 258 | """Syntax: !keyword [-h] [-t type] <keyword1> [, keyword2, ...] |
---|
| 259 | """ |
---|
| 260 | _debug('in keyword command: %s', str(args)) |
---|
[12] | 261 | |
---|
[19] | 262 | try: |
---|
| 263 | options, rest = getopt.getopt(args, 't:h', ['type=', 'help']) |
---|
| 264 | except getopt.GetoptError, err: |
---|
| 265 | _debug('Caught GetoptError in onmsg_keyword: %s', str(err)) |
---|
| 266 | return False |
---|
[12] | 267 | |
---|
[19] | 268 | keyword = ' '.join(rest).strip() |
---|
| 269 | product_line = 'books-jp' |
---|
| 270 | for opt, val in options: |
---|
| 271 | if opt in ['-t', '--type']: |
---|
| 272 | if val not in self._AVAIL_PRODUCT_LINES.keys(): |
---|
| 273 | c.notice(to, 'Type "%s" is not available.' % val) |
---|
| 274 | return False |
---|
[14] | 275 | |
---|
[19] | 276 | product_line = val |
---|
| 277 | break |
---|
[14] | 278 | |
---|
[19] | 279 | elif opt in ['-h', '--help']: |
---|
| 280 | _from = nm_to_n(e.source()) # ãã°ãæµããŠããŸãã®ã§ãã«ãã¯çŽæ¥éä¿¡å
ãž |
---|
| 281 | c.notice(_from, ununicoding('Available types:')) |
---|
[14] | 282 | |
---|
[19] | 283 | for key, val in self._AVAIL_PRODUCT_LINES.iteritems(): |
---|
| 284 | time.sleep(1) # XXX: é£ç¶æçš¿ãããšåŒŸãããããšãããã®ã§æ«å®å¯ŸåŠ |
---|
| 285 | c.notice(_from, ununicoding(' * %s: %s' % (key, val))) |
---|
[14] | 286 | |
---|
[19] | 287 | return True |
---|
[12] | 288 | |
---|
[19] | 289 | if not keyword: |
---|
| 290 | c.notice(to, 'Please specify keywords.') |
---|
| 291 | return False |
---|
[14] | 292 | |
---|
[19] | 293 | _debug('keyword="%s", product_line=%s', keyword, product_line) |
---|
[12] | 294 | |
---|
[19] | 295 | try: |
---|
| 296 | data = my_amazon.searchByKeyword(keyword, product_line=product_line) |
---|
| 297 | except my_amazon.AmazonError, err: |
---|
| 298 | c.notice(to, ununicoding(config.get('bot', 'no_products'))) |
---|
| 299 | _debug('Caught AmazonError in onmsg_amazon: %s', str(err)) |
---|
| 300 | return False |
---|
[12] | 301 | |
---|
[19] | 302 | return self._process_onmsg(c, e, to, data) |
---|
[12] | 303 | |
---|
[19] | 304 | def onmsg_h(self, c, e, to, args): return self.onmsg_help(c, e, to, args) |
---|
| 305 | def onmsg_help(self, c, e, to, args): |
---|
| 306 | """Syntax: !help |
---|
| 307 | """ |
---|
| 308 | _debug('in help command: %s', str(args)) |
---|
[12] | 309 | |
---|
[19] | 310 | _from = nm_to_n(e.source()) # ãã°ãæµããŠããŸãã®ã§ãã«ãã¯çŽæ¥éä¿¡å
ãž |
---|
| 311 | docs = [] |
---|
| 312 | for key in dir(self): |
---|
| 313 | val = getattr(self, key, '') |
---|
| 314 | _debug('key=%s, val=%s', key, str(val)) |
---|
[14] | 315 | |
---|
[19] | 316 | if key[:6] != 'onmsg_': |
---|
| 317 | continue |
---|
[14] | 318 | |
---|
[19] | 319 | doc = val.__doc__ |
---|
| 320 | if doc: |
---|
| 321 | doc = doc.strip() |
---|
| 322 | if not doc: |
---|
| 323 | continue |
---|
| 324 | time.sleep(1) # XXX: é£ç¶æçš¿ãããšåŒŸãããã£ãœãã®ã§æ«å®å¯ŸåŠ |
---|
| 325 | c.notice(_from, doc) |
---|
[14] | 326 | |
---|
[19] | 327 | return True |
---|
[14] | 328 | |
---|
[19] | 329 | def _process_onmsg(self, c, e, to, data): |
---|
| 330 | if type(data.Details) is not list: |
---|
| 331 | data.Details = [data.Details] |
---|
[12] | 332 | |
---|
[19] | 333 | detail = random.choice(data.Details) |
---|
| 334 | title = ununicoding(detail.ProductName) |
---|
| 335 | url = ununicoding(detail.URL) |
---|
| 336 | c.notice(to, '%(title)s: %(url)s' % locals()) |
---|
[12] | 337 | |
---|
[19] | 338 | return True |
---|
[12] | 339 | |
---|
[19] | 340 | def process_keyword(self, keyword): |
---|
| 341 | keyword = ununicoding(keyword, 'utf-8') |
---|
| 342 | _debug('KEYWORD: %s', keyword) |
---|
[10] | 343 | |
---|
[19] | 344 | try: |
---|
| 345 | data = my_amazon.searchByBlended(keyword) |
---|
| 346 | if type(data.ProductLine) is not type([]): |
---|
| 347 | data.ProductLine = [data.ProductLine] |
---|
| 348 | except my_amazon.AmazonError, err: |
---|
| 349 | _debug('Caught AmazonError: %s', str(err)) |
---|
| 350 | return [None, None] |
---|
[8] | 351 | |
---|
[19] | 352 | product_line = random.choice(data.ProductLine) |
---|
| 353 | detail = random.choice(product_line.ProductInfo.Details) |
---|
[8] | 354 | |
---|
[19] | 355 | url = unicoding(getattr(detail, 'URL', None)) |
---|
| 356 | product_name = unicoding(getattr(detail, 'ProductName', None)) |
---|
[8] | 357 | |
---|
[19] | 358 | return [product_name, url] |
---|
[8] | 359 | |
---|
| 360 | if __name__ == '__main__': |
---|
[19] | 361 | bot = AmazonBot() |
---|
| 362 | bot.start() |
---|
| 363 | print '> Bye ;)' |
---|