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 shlex |
---|
14 | import random |
---|
15 | import getopt |
---|
16 | |
---|
17 | import MeCab |
---|
18 | import nkf |
---|
19 | |
---|
20 | from ircbot import SingleServerIRCBot |
---|
21 | from irclib import nm_to_n |
---|
22 | |
---|
23 | try: |
---|
24 | set, frozenset |
---|
25 | except NameError: |
---|
26 | from sets import Set as set, ImmutableSet as frozenset |
---|
27 | |
---|
28 | |
---|
29 | import config; config.init() |
---|
30 | |
---|
31 | import my_amazon |
---|
32 | my_amazon.setLocale(config.get('amazon', 'locale')) |
---|
33 | my_amazon.setLicense(config.get('amazon', 'access_key')) |
---|
34 | |
---|
35 | |
---|
36 | DEBUG_MSG_TO = sys.stderr |
---|
37 | |
---|
38 | |
---|
39 | def uniq(sequence): |
---|
40 | """ãªã¹ãããéè€ãåãé€ã (é çªãçãã®ã§æ³šæ) |
---|
41 | """ |
---|
42 | return list(set(sequence)) |
---|
43 | |
---|
44 | def 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 | |
---|
51 | def 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 | |
---|
58 | def 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 | |
---|
81 | def _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 | |
---|
98 | class 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 | |
---|
210 | class 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 | |
---|
360 | if __name__ == '__main__': |
---|
361 | bot = AmazonBot() |
---|
362 | bot.start() |
---|
363 | print '> Bye ;)' |
---|