| 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 |
|---|
| 11 | import sys |
|---|
| 12 | import time |
|---|
| 13 | import random |
|---|
| 14 | import MeCab |
|---|
| 15 | import nkf |
|---|
| 16 | |
|---|
| 17 | from ircbot import SingleServerIRCBot |
|---|
| 18 | from irclib import nm_to_n |
|---|
| 19 | |
|---|
| 20 | import config |
|---|
| 21 | config.init() |
|---|
| 22 | |
|---|
| 23 | import my_amazon |
|---|
| 24 | my_amazon.setLocale(config.get('amazon', 'locale')) |
|---|
| 25 | my_amazon.setLicense(config.get('amazon', 'access_key')) |
|---|
| 26 | |
|---|
| 27 | try: |
|---|
| 28 | set, frozenset |
|---|
| 29 | except NameError: |
|---|
| 30 | from sets import Set as set, ImmutableSet as frozenset |
|---|
| 31 | |
|---|
| 32 | def uniq(sequence): |
|---|
| 33 | """ãªã¹ãããéè€ãåãé€ã (é çªãçãã®ã§æ³šæ) |
|---|
| 34 | """ |
|---|
| 35 | return list(set(sequence)) |
|---|
| 36 | |
|---|
| 37 | def unicoding(text): |
|---|
| 38 | """text ã匷å¶çã« unicode ãªããžã§ã¯ãã«å€æ |
|---|
| 39 | """ |
|---|
| 40 | if type(text) is unicode: |
|---|
| 41 | return text |
|---|
| 42 | return unicode(nkf.nkf('-w', text), 'utf-8') |
|---|
| 43 | |
|---|
| 44 | def ununicoding(text, encoding='iso-2022-jp'): |
|---|
| 45 | """text ãæå®ããã encoding ã§ãšã³ã³ãŒããïŒraw str ã«åŒ·å¶å€æ |
|---|
| 46 | """ |
|---|
| 47 | if type(text) is not unicode: |
|---|
| 48 | return unicoding(text).encode(encoding) |
|---|
| 49 | return text.encode(encoding) |
|---|
| 50 | |
|---|
| 51 | def mecab_parse(text): |
|---|
| 52 | """MeCab ã䜿ã£ãŠåœ¢æ
çŽ è§£æãïŒåºæåè©ãšäžè¬åè©ã ããæœåºãã |
|---|
| 53 | """ |
|---|
| 54 | def choice_nominal(wlist): |
|---|
| 55 | res = [] |
|---|
| 56 | for word, wtype in wlist: |
|---|
| 57 | wtypes = wtype.split('-') |
|---|
| 58 | if 'åºæåè©' in wtypes or ('åè©' in wtypes and 'äžè¬' in wtypes): |
|---|
| 59 | res.append(unicoding(word)) |
|---|
| 60 | return res |
|---|
| 61 | |
|---|
| 62 | text = ununicoding(text, 'utf-8') |
|---|
| 63 | result = [] |
|---|
| 64 | tag = MeCab.Tagger('-Ochasen') |
|---|
| 65 | for line in tag.parse(text).split('\n'): |
|---|
| 66 | if not line or line == 'EOS': |
|---|
| 67 | break |
|---|
| 68 | words = line.split() |
|---|
| 69 | result.append((words[0], words[-1])) # word, word-type |
|---|
| 70 | |
|---|
| 71 | result = uniq(choice_nominal(result)) |
|---|
| 72 | return result |
|---|
| 73 | |
|---|
| 74 | class AmazonBotBase(SingleServerIRCBot): |
|---|
| 75 | """ã¢ããŸã³ãããã®ããŒã¹ã¯ã©ã¹ |
|---|
| 76 | ããã€åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ
çŽ è§£æãšåè©æœåºãŸã§ãããããªã |
|---|
| 77 | ãµãã¯ã©ã¹ã§ process_keyword ãå®è£
ã㊠Amazon ãžã¯ãšãªãæããã¹ã |
|---|
| 78 | """ |
|---|
| 79 | def __init__(self): |
|---|
| 80 | _server = [(config.get('irc', 'server'), config.get('irc', 'port', 'int'))] |
|---|
| 81 | _nick = config.get('bot', 'nick') |
|---|
| 82 | |
|---|
| 83 | self._prev_time = time.time() |
|---|
| 84 | self._silent = False |
|---|
| 85 | SingleServerIRCBot.__init__(self, _server, _nick, _nick) |
|---|
| 86 | |
|---|
| 87 | def start(self): |
|---|
| 88 | try: |
|---|
| 89 | SingleServerIRCBot.start(self) |
|---|
| 90 | except KeyboardInterrupt: |
|---|
| 91 | self.die(ununicoding(config.get('bot', 'bye'))) |
|---|
| 92 | |
|---|
| 93 | def on_welcome(self, c, e): |
|---|
| 94 | c.join(config.get('irc', 'channel')) |
|---|
| 95 | if __debug__: |
|---|
| 96 | print >> sys.stderr, 'DEBUG> Joined %s' % config.get('irc', 'channel') |
|---|
| 97 | |
|---|
| 98 | def on_nicknameinuse(self, c, e): |
|---|
| 99 | c.nick(c.get_nickname() + '_') |
|---|
| 100 | |
|---|
| 101 | def on_privmsg(self, c, e): |
|---|
| 102 | return self.on_pubmsg(c, e) |
|---|
| 103 | |
|---|
| 104 | def on_pubmsg(self, c, e): |
|---|
| 105 | if time.time() > self._prev_time + config.get('bot', 'freq', 'int'): |
|---|
| 106 | if __debug__: |
|---|
| 107 | prev = time.strftime('%y/%m/%d %H:%M:%S', time.localtime(self._prev_time)) |
|---|
| 108 | print >> sys.stderr, 'DEBUG> Not expired: prev time is %s' % prev |
|---|
| 109 | return False |
|---|
| 110 | |
|---|
| 111 | msg = unicoding(e.arguments()[0]) |
|---|
| 112 | |
|---|
| 113 | self.silence(msg, c, e) |
|---|
| 114 | if self._silent: |
|---|
| 115 | return False |
|---|
| 116 | |
|---|
| 117 | nominals = mecab_parse(msg) |
|---|
| 118 | if not nominals: |
|---|
| 119 | if __debug__: |
|---|
| 120 | print >> sys.stderr, "DEBUG> Couldn't find nominal words" |
|---|
| 121 | return False |
|---|
| 122 | |
|---|
| 123 | title, url = self.process_keyword(' '.join(nominals)) |
|---|
| 124 | if title and url: |
|---|
| 125 | channel = e.target() |
|---|
| 126 | content = unicoding(config.get('bot', 'content')) |
|---|
| 127 | try: |
|---|
| 128 | message = ununicoding(': '.join([content, title, url])) |
|---|
| 129 | except UnicodeError: |
|---|
| 130 | return False |
|---|
| 131 | # ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã |
|---|
| 132 | |
|---|
| 133 | c.privmsg(channel, message) |
|---|
| 134 | return True |
|---|
| 135 | return False |
|---|
| 136 | |
|---|
| 137 | ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern'))) |
|---|
| 138 | SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern'))) |
|---|
| 139 | def silence(self, msg, c, e): |
|---|
| 140 | ch = e.target() |
|---|
| 141 | active = self.ACTIVE_PATTERN.search(msg) |
|---|
| 142 | silent = self.SILENT_PATTERN.search(msg) |
|---|
| 143 | if __debug__: |
|---|
| 144 | print >> sys.stderr, 'DEBUG> ACT_PATT: %s, SIL_PATT: %s' % (str(active), str(silent)) |
|---|
| 145 | |
|---|
| 146 | if active: |
|---|
| 147 | self._silent = False |
|---|
| 148 | c.privmsg(ch, ununicoding(config.get('bot', 'thanks'))) |
|---|
| 149 | elif silent: |
|---|
| 150 | self._silent = True |
|---|
| 151 | c.privmsg(ch, ununicoding(config.get('bot', 'sorry'))) |
|---|
| 152 | |
|---|
| 153 | def process_keyword(self, keyword): |
|---|
| 154 | return [None, None] |
|---|
| 155 | |
|---|
| 156 | class AmazonBot(AmazonBotBase): |
|---|
| 157 | """ã¢ããŸã³ãããã®å®è£
ã¯ã©ã¹ |
|---|
| 158 | process_keyword ã¡ãœããã§ Amazon ãžã¯ãšãªãæããŠçµæãè¿ã |
|---|
| 159 | """ |
|---|
| 160 | def __init__(self): |
|---|
| 161 | AmazonBotBase.__init__(self) |
|---|
| 162 | |
|---|
| 163 | def get_version(self): |
|---|
| 164 | return 'AmazonBot by %s, based on python-irclib' % __author__ |
|---|
| 165 | |
|---|
| 166 | def process_keyword(self, keyword): |
|---|
| 167 | keyword = ununicoding(keyword, 'utf-8') |
|---|
| 168 | if __debug__: |
|---|
| 169 | print >> sys.stderr, 'DEBUG> KEYWORD: %s' % ununicoding(keyword, 'euc-jp') |
|---|
| 170 | |
|---|
| 171 | try: |
|---|
| 172 | data = my_amazon.searchByBlended(keyword) |
|---|
| 173 | if type(data.ProductLine) is not type([]): |
|---|
| 174 | data.ProductLine = [data.ProductLine] |
|---|
| 175 | except my_amazon.AmazonError, e: |
|---|
| 176 | if __debug__: |
|---|
| 177 | print >> sys.stderr, 'DEBUG> Caught AmazonError: %s' % str(e) |
|---|
| 178 | return [None, None] |
|---|
| 179 | |
|---|
| 180 | product_line = random.choice(data.ProductLine) |
|---|
| 181 | detail = random.choice(product_line.ProductInfo.Details) |
|---|
| 182 | |
|---|
| 183 | url = unicoding(getattr(detail, 'URL', None)) |
|---|
| 184 | product_name = unicoding(getattr(detail, 'ProductName', None)) |
|---|
| 185 | |
|---|
| 186 | return [product_name, url] |
|---|
| 187 | |
|---|
| 188 | if __name__ == '__main__': |
|---|
| 189 | bot = AmazonBot() |
|---|
| 190 | bot.start() |
|---|