[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 |
---|
[8] | 13 | import random |
---|
| 14 | import MeCab |
---|
[10] | 15 | import nkf |
---|
[8] | 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): |
---|
[10] | 33 | """ãªã¹ãããéè€ãåãé€ã (é çªãçãã®ã§æ³šæ) |
---|
| 34 | """ |
---|
[8] | 35 | return list(set(sequence)) |
---|
| 36 | |
---|
[10] | 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 | |
---|
[8] | 51 | def mecab_parse(text): |
---|
[10] | 52 | """MeCab ã䜿ã£ãŠåœ¢æ
çŽ è§£æãïŒåºæåè©ãšäžè¬åè©ã ããæœåºãã |
---|
| 53 | """ |
---|
[8] | 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): |
---|
[10] | 59 | res.append(unicoding(word)) |
---|
[8] | 60 | return res |
---|
| 61 | |
---|
[10] | 62 | text = ununicoding(text, 'utf-8') |
---|
[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): |
---|
[10] | 75 | """ã¢ããŸã³ãããã®ããŒã¹ã¯ã©ã¹ |
---|
| 76 | ããã€åäœã§ã¯ïŒåãåã£ãã¡ãã»ãŒãžã®åœ¢æ
çŽ è§£æãšåè©æœåºãŸã§ãããããªã |
---|
| 77 | ãµãã¯ã©ã¹ã§ process_keyword ãå®è£
ã㊠Amazon ãžã¯ãšãªãæããã¹ã |
---|
| 78 | """ |
---|
[8] | 79 | def __init__(self): |
---|
| 80 | _server = [(config.get('irc', 'server'), config.get('irc', 'port', 'int'))] |
---|
| 81 | _nick = config.get('bot', 'nick') |
---|
| 82 | |
---|
[10] | 83 | self._prev_time = time.time() |
---|
[8] | 84 | self._silent = False |
---|
| 85 | SingleServerIRCBot.__init__(self, _server, _nick, _nick) |
---|
| 86 | |
---|
[10] | 87 | def start(self): |
---|
| 88 | try: |
---|
| 89 | SingleServerIRCBot.start(self) |
---|
| 90 | except KeyboardInterrupt: |
---|
| 91 | self.die(ununicoding(config.get('bot', 'bye'))) |
---|
[8] | 92 | |
---|
| 93 | def on_welcome(self, c, e): |
---|
| 94 | c.join(config.get('irc', 'channel')) |
---|
| 95 | if __debug__: |
---|
[10] | 96 | print >> sys.stderr, 'DEBUG> Joined %s' % config.get('irc', 'channel') |
---|
[8] | 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): |
---|
[10] | 105 | if time.time() > self._prev_time + config.get('bot', 'freq', 'int'): |
---|
[9] | 106 | if __debug__: |
---|
[10] | 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 |
---|
[9] | 109 | return False |
---|
[8] | 110 | |
---|
[10] | 111 | msg = unicoding(e.arguments()[0]) |
---|
| 112 | |
---|
[9] | 113 | self.silence(msg, c, e) |
---|
[8] | 114 | if self._silent: |
---|
| 115 | return False |
---|
| 116 | |
---|
[10] | 117 | nominals = mecab_parse(msg) |
---|
[8] | 118 | if not nominals: |
---|
[10] | 119 | if __debug__: |
---|
| 120 | print >> sys.stderr, "DEBUG> Couldn't find nominal words" |
---|
[8] | 121 | return False |
---|
| 122 | |
---|
| 123 | title, url = self.process_keyword(' '.join(nominals)) |
---|
| 124 | if title and url: |
---|
| 125 | channel = e.target() |
---|
[10] | 126 | content = unicoding(config.get('bot', 'content')) |
---|
[9] | 127 | try: |
---|
[10] | 128 | message = ununicoding(': '.join([content, title, url])) |
---|
[9] | 129 | except UnicodeError: |
---|
| 130 | return False |
---|
[10] | 131 | # ãªããããŸã« unicode ãªããžã§ã¯ãã iso-2022-jp ã§ãšã³ã³ãŒãã§ããªã |
---|
[9] | 132 | |
---|
[8] | 133 | c.privmsg(channel, message) |
---|
| 134 | return True |
---|
| 135 | return False |
---|
| 136 | |
---|
[10] | 137 | ACTIVE_PATTERN = re.compile(unicoding(config.get('bot', 'active_pattern'))) |
---|
| 138 | SILENT_PATTERN = re.compile(unicoding(config.get('bot', 'silent_pattern'))) |
---|
[9] | 139 | def silence(self, msg, c, e): |
---|
| 140 | ch = e.target() |
---|
[10] | 141 | active = self.ACTIVE_PATTERN.search(msg) |
---|
| 142 | silent = self.SILENT_PATTERN.search(msg) |
---|
[9] | 143 | if __debug__: |
---|
[10] | 144 | print >> sys.stderr, 'DEBUG> ACT_PATT: %s, SIL_PATT: %s' % (str(active), str(silent)) |
---|
[9] | 145 | |
---|
| 146 | if active: |
---|
[8] | 147 | self._silent = False |
---|
[10] | 148 | c.privmsg(ch, ununicoding(config.get('bot', 'thanks'))) |
---|
[9] | 149 | elif silent: |
---|
[8] | 150 | self._silent = True |
---|
[10] | 151 | c.privmsg(ch, ununicoding(config.get('bot', 'sorry'))) |
---|
[8] | 152 | |
---|
| 153 | def process_keyword(self, keyword): |
---|
| 154 | return [None, None] |
---|
| 155 | |
---|
| 156 | class AmazonBot(AmazonBotBase): |
---|
[10] | 157 | """ã¢ããŸã³ãããã®å®è£
ã¯ã©ã¹ |
---|
| 158 | process_keyword ã¡ãœãã㧠Amazon ãžã¯ãšãªãæããŠçµæãè¿ã |
---|
| 159 | """ |
---|
[8] | 160 | def __init__(self): |
---|
| 161 | AmazonBotBase.__init__(self) |
---|
| 162 | |
---|
[10] | 163 | def get_version(self): |
---|
| 164 | return 'AmazonBot by %s, based on python-irclib' % __author__ |
---|
| 165 | |
---|
[8] | 166 | def process_keyword(self, keyword): |
---|
[10] | 167 | keyword = ununicoding(keyword, 'utf-8') |
---|
| 168 | if __debug__: |
---|
| 169 | print >> sys.stderr, 'DEBUG> KEYWORD: %s' % ununicoding(keyword, 'euc-jp') |
---|
| 170 | |
---|
[8] | 171 | try: |
---|
| 172 | data = my_amazon.searchByBlended(keyword) |
---|
| 173 | if type(data.ProductLine) is not type([]): |
---|
| 174 | data.ProductLine = [data.ProductLine] |
---|
[10] | 175 | except my_amazon.AmazonError, e: |
---|
| 176 | if __debug__: |
---|
| 177 | print >> sys.stderr, 'DEBUG> Caught AmazonError: %s' % str(e) |
---|
[8] | 178 | return [None, None] |
---|
| 179 | |
---|
| 180 | product_line = random.choice(data.ProductLine) |
---|
| 181 | detail = random.choice(product_line.ProductInfo.Details) |
---|
| 182 | |
---|
[10] | 183 | url = unicoding(getattr(detail, 'URL', None)) |
---|
| 184 | product_name = unicoding(getattr(detail, 'ProductName', None)) |
---|
[8] | 185 | |
---|
| 186 | return [product_name, url] |
---|
| 187 | |
---|
| 188 | if __name__ == '__main__': |
---|
| 189 | bot = AmazonBot() |
---|
| 190 | bot.start() |
---|