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