[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 |
---|
[8] | 12 | import random |
---|
| 13 | import MeCab |
---|
| 14 | |
---|
| 15 | from ircbot import SingleServerIRCBot |
---|
| 16 | from irclib import nm_to_n |
---|
| 17 | |
---|
| 18 | import config |
---|
| 19 | config.init() |
---|
| 20 | |
---|
| 21 | import my_amazon |
---|
| 22 | my_amazon.setLocale(config.get('amazon', 'locale')) |
---|
| 23 | my_amazon.setLicense(config.get('amazon', 'access_key')) |
---|
| 24 | |
---|
| 25 | try: |
---|
| 26 | set, frozenset |
---|
| 27 | except NameError: |
---|
| 28 | from sets import Set as set, ImmutableSet as frozenset |
---|
| 29 | |
---|
| 30 | def uniq(sequence): |
---|
| 31 | return list(set(sequence)) |
---|
| 32 | |
---|
| 33 | def mecab_parse(text): |
---|
| 34 | def choice_nominal(wlist): |
---|
| 35 | res = [] |
---|
| 36 | for word, wtype in wlist: |
---|
| 37 | wtypes = wtype.split('-') |
---|
| 38 | if 'åºæåè©' in wtypes or ('åè©' in wtypes and 'äžè¬' in wtypes): |
---|
| 39 | res.append(word) |
---|
| 40 | return res |
---|
| 41 | |
---|
| 42 | result = [] |
---|
| 43 | tag = MeCab.Tagger('-Ochasen') |
---|
| 44 | for line in tag.parse(text).split('\n'): |
---|
| 45 | if not line or line == 'EOS': |
---|
| 46 | break |
---|
| 47 | words = line.split() |
---|
| 48 | result.append((words[0], words[-1])) # word, word-type |
---|
| 49 | |
---|
| 50 | result = uniq(choice_nominal(result)) |
---|
| 51 | return result |
---|
| 52 | |
---|
| 53 | class AmazonBotBase(SingleServerIRCBot): |
---|
| 54 | def __init__(self): |
---|
| 55 | _server = [(config.get('irc', 'server'), config.get('irc', 'port', 'int'))] |
---|
| 56 | _nick = config.get('bot', 'nick') |
---|
| 57 | |
---|
| 58 | self._silent = False |
---|
| 59 | SingleServerIRCBot.__init__(self, _server, _nick, _nick) |
---|
| 60 | |
---|
| 61 | def get_version(self): |
---|
| 62 | return 'AmazonBot by %s, based on python-irclib' % __author__ |
---|
| 63 | |
---|
| 64 | def on_welcome(self, c, e): |
---|
| 65 | c.join(config.get('irc', 'channel')) |
---|
| 66 | if __debug__: |
---|
[9] | 67 | print >> sys.stderr, '* Joined %s' % config.get('irc', 'channel') |
---|
[8] | 68 | |
---|
| 69 | def on_nicknameinuse(self, c, e): |
---|
| 70 | c.nick(c.get_nickname() + '_') |
---|
| 71 | |
---|
| 72 | def on_privmsg(self, c, e): |
---|
| 73 | return self.on_pubmsg(c, e) |
---|
| 74 | |
---|
| 75 | def on_pubmsg(self, c, e): |
---|
[9] | 76 | try: |
---|
| 77 | msg = unicode(e.arguments()[0], 'iso-2022-jp') |
---|
| 78 | except UnicodeError: |
---|
| 79 | if __debug__: |
---|
| 80 | print >> sys.stderr, 'ERR: incoming message encoding failed: %s' % e.arguments() |
---|
| 81 | return False |
---|
[8] | 82 | |
---|
[9] | 83 | self.silence(msg, c, e) |
---|
[8] | 84 | if self._silent: |
---|
| 85 | return False |
---|
| 86 | |
---|
| 87 | nominals = mecab_parse(msg.encode('utf-8')) |
---|
| 88 | if not nominals: |
---|
| 89 | return False |
---|
| 90 | |
---|
| 91 | title, url = self.process_keyword(' '.join(nominals)) |
---|
| 92 | if title and url: |
---|
| 93 | channel = e.target() |
---|
| 94 | |
---|
| 95 | content = unicode(config.get('bot', 'content'), 'utf-8') |
---|
| 96 | title = title |
---|
| 97 | |
---|
[9] | 98 | try: |
---|
| 99 | message = ('%(content)s: %(title)s %(url)s' % locals()).encode('iso-2022-jp') |
---|
| 100 | except UnicodeError: |
---|
| 101 | if __debug__: |
---|
| 102 | print >> sys.stderr, 'ERR: my message encoding failed: ' |
---|
| 103 | print >> sys.stderr, ' * content> %s' % content |
---|
| 104 | print >> sys.stderr, ' * title> %s' % title |
---|
| 105 | print >> sys.stderr, ' * url> %s' % url |
---|
| 106 | return False |
---|
| 107 | |
---|
[8] | 108 | c.privmsg(channel, message) |
---|
| 109 | |
---|
| 110 | return True |
---|
| 111 | |
---|
| 112 | return False |
---|
| 113 | |
---|
| 114 | ACTIVE_PATTERN = re.compile(config.get('bot', 'active_pattern')) |
---|
| 115 | SILENT_PATTERN = re.compile(config.get('bot', 'silent_pattern')) |
---|
[9] | 116 | def silence(self, msg, c, e): |
---|
| 117 | ch = e.target() |
---|
| 118 | active = self.ACTIVE_PATTERN.search(msg.encode('utf-8')) |
---|
| 119 | silent = self.SILENT_PATTERN.search(msg.encode('utf-8')) |
---|
| 120 | if __debug__: |
---|
| 121 | print >> sys.stderr, 'ACTIVE_PATTERN: %s, SILENT_PATTERN: %s' % (str(active), str(silent)) |
---|
| 122 | |
---|
| 123 | if active: |
---|
[8] | 124 | self._silent = False |
---|
[9] | 125 | c.privmsg(ch, unicode(config.get('bot', 'thanks'), 'utf-8').encode('iso-2022-jp')) |
---|
| 126 | elif silent: |
---|
[8] | 127 | self._silent = True |
---|
[9] | 128 | c.privmsg(ch, unicode(config.get('bot', 'sorry'), 'utf-8').encode('iso-2022-jp')) |
---|
[8] | 129 | |
---|
| 130 | def process_keyword(self, keyword): |
---|
| 131 | return [None, None] |
---|
| 132 | |
---|
| 133 | class AmazonBot(AmazonBotBase): |
---|
| 134 | def __init__(self): |
---|
| 135 | AmazonBotBase.__init__(self) |
---|
| 136 | |
---|
| 137 | def process_keyword(self, keyword): |
---|
| 138 | try: |
---|
| 139 | data = my_amazon.searchByBlended(keyword) |
---|
| 140 | if type(data.ProductLine) is not type([]): |
---|
| 141 | data.ProductLine = [data.ProductLine] |
---|
| 142 | except my_amazon.AmazonError: |
---|
| 143 | return [None, None] |
---|
| 144 | |
---|
| 145 | product_line = random.choice(data.ProductLine) |
---|
| 146 | detail = random.choice(product_line.ProductInfo.Details) |
---|
| 147 | |
---|
| 148 | url = getattr(detail, 'URL', None) |
---|
| 149 | product_name = getattr(detail, 'ProductName', None) |
---|
| 150 | |
---|
| 151 | return [product_name, url] |
---|
| 152 | |
---|
| 153 | if __name__ == '__main__': |
---|
| 154 | bot = AmazonBot() |
---|
| 155 | bot.start() |
---|