source: trunk/amazonbot/amazonbot.py @ 19

Revision 19, 12.1 KB checked in by atzm, 17 years ago (diff)
  • untabify, and cosmeticize debug messages
Line 
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
10import re
11import sys
12import time
13import shlex
14import random
15import getopt
16
17import MeCab
18import nkf
19
20from ircbot import SingleServerIRCBot
21from irclib import nm_to_n
22
23try:
24    set, frozenset
25except NameError:
26    from sets import Set as set, ImmutableSet as frozenset
27
28
29import config; config.init()
30
31import my_amazon
32my_amazon.setLocale(config.get('amazon', 'locale'))
33my_amazon.setLicense(config.get('amazon', 'access_key'))
34
35
36DEBUG_MSG_TO = sys.stderr
37
38
39def uniq(sequence):
40    """リストから重耇を取り陀く (順番が狂うので泚意)
41    """
42    return list(set(sequence))
43
44def unicoding(text):
45    """text を匷制的に unicode オブゞェクトに倉換
46    """
47    if type(text) is unicode:
48        return text
49    return unicode(nkf.nkf('-w', text), 'utf-8')
50
51def ununicoding(text, encoding='iso-2022-jp'):
52    """text を指定された encoding で゚ンコヌドしraw str に匷制倉換
53    """
54    if type(text) is not unicode:
55        return unicoding(text).encode(encoding)
56    return text.encode(encoding)
57
58def mecab_parse(text):
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
68
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
77
78    result = uniq(choice_nominal(result))
79    return result
80
81def _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
98class AmazonBotBase(SingleServerIRCBot):
99    """アマゟンボットのベヌスクラス
100    単䜓では受け取ったメッセヌゞの圢態玠解析ず名詞抜出たでしかやらない
101    サブクラスで process_keyword を実装しお Amazon ぞク゚リを投げるべし
102
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')
110
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)
115
116    def start(self):
117        try:
118            SingleServerIRCBot.start(self)
119        except KeyboardInterrupt:
120            self.die(ununicoding(config.get('bot', 'bye')))
121
122    def on_welcome(self, c, e):
123        c.join(config.get('irc', 'channel'))
124        _debug('Joined %s', config.get('irc', 'channel'))
125
126    def on_nicknameinuse(self, c, e):
127        c.nick(c.get_nickname() + '_')
128
129    def on_privmsg(self, c, e):
130        return self.on_pubmsg(c, e, to=nm_to_n(e.source()))
131
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)
135
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
145
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)
152
153            _debug('Line count: now %d, next: %d', self._current_lines, _freq_lines)
154
155            if self._current_lines < _freq_lines:
156                return False
157        self._current_lines = 0
158
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
168
169        # silence
170        self.silence(msg, c, e, to)
171        if self._silent:
172            return False
173
174        nominals = mecab_parse(msg)
175        if not nominals:
176            _debug("Couldn't find nominal words")
177            return False
178
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
188
189            c.notice(to, message)
190            return True
191        return False
192
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))
199
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')))
206
207    def process_keyword(self, keyword):
208        return [None, None]
209
210class AmazonBot(AmazonBotBase):
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    }
228
229    def __init__(self):
230        AmazonBotBase.__init__(self)
231
232    def get_version(self):
233        return 'AmazonBot by %s, based on python-irclib' % __author__
234
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))
243
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
253
254        return self._process_onmsg(c, e, to, data)
255
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))
261
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
267
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
275
276                product_line = val
277                break
278
279            elif opt in ['-h', '--help']:
280                _from = nm_to_n(e.source()) # ログを流しおしたうのでヘルプは盎接送信元ぞ
281                c.notice(_from, ununicoding('Available types:'))
282
283                for key, val in self._AVAIL_PRODUCT_LINES.iteritems():
284                    time.sleep(1) # XXX: 連続投皿するず匟かれるこずがあるので暫定察凊
285                    c.notice(_from, ununicoding(' * %s: %s' % (key, val)))
286
287                return True
288
289        if not keyword:
290            c.notice(to, 'Please specify keywords.')
291            return False
292
293        _debug('keyword="%s", product_line=%s', keyword, product_line)
294
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
301
302        return self._process_onmsg(c, e, to, data)
303
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))
309
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))
315
316            if key[:6] != 'onmsg_':
317                continue
318
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)
326
327        return True
328
329    def _process_onmsg(self, c, e, to, data):
330        if type(data.Details) is not list:
331            data.Details = [data.Details]
332
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())
337
338        return True
339
340    def process_keyword(self, keyword):
341        keyword = ununicoding(keyword, 'utf-8')
342        _debug('KEYWORD: %s', keyword)
343
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]
351
352        product_line = random.choice(data.ProductLine)
353        detail = random.choice(product_line.ProductInfo.Details)
354
355        url = unicoding(getattr(detail, 'URL', None))
356        product_name = unicoding(getattr(detail, 'ProductName', None))
357
358        return [product_name, url]
359
360if __name__ == '__main__':
361    bot = AmazonBot()
362    bot.start()
363    print '> Bye ;)'
Note: See TracBrowser for help on using the repository browser.