2
# -*- coding: UTF-8 -*-
3
# Copyright (c) 2002-2005, A4Tech Email: admin@itmages.ru
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are met:
9
# * Redistributions of source code must retain the above copyright notice,
10
# this list of conditions, and the following disclaimer.
11
# * Redistributions in binary form must reproduce the above copyright notice,
12
# this list of conditions, and the following disclaimer in the
13
# documentation and/or other materials provided with the distribution.
14
# * Neither the name of the author of this software nor the name of
15
# contributors to this software may be used to endorse or promote products
16
# derived from this software without specific prior written consent.
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
# POSSIBILITY OF SUCH DAMAGE.
34
import supybot.ircmsgs as ircmsgs
35
import supybot.ircutils as ircutils
36
import supybot.callbacks as callbacks
37
from supybot.commands import *
39
class Translate(callbacks.Privmsg):
43
class Translate(callbacks.Plugin):
44
def __init__(self, irc):
45
self.__parent = super(Translate, self)
46
self.__parent.__init__(irc)
48
def tran(self, text, langpair):
49
query = urllib.urlencode({'q' : text, 'langpair' : langpair})
50
url = u'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&%s' % (query)
51
search_results = urllib.urlopen(url)
52
json = simplejson.loads(search_results.read())
53
mess = json['responseData']['translatedText']
56
def detect(self, text):
58
if re.search("[уеаоэяиюУЕЫАОЯИЮ]", text):
62
def translate(self, irc, msg, args, text):
67
(recipients, somet) = msg.args
68
for channel in recipients.split(','):
69
if irc.isChannel(channel):
70
if self.registryValue('enable', channel):
71
#langpaird = self.registryValue('default', channel)
72
langpaird = self.detect(text)
73
translated = self.tran(text, langpaird).encode("utf-8")
74
#self._preCheck(irc, msg, channel)
75
#self.log.info('Translate %q<>%qin %s due to %s.',
76
# text, translated, channel, msg.prefix)
77
irc.reply('%s, %s' % (msg.nick, translated))
78
translate = wrap(translate, ['text'])
80
def tt(self, irc, msg, args, text):
85
(recipients, somet) = msg.args
86
for channel in recipients.split(','):
87
if irc.isChannel(channel):
88
if self.registryValue('enable', channel):
89
#langpaird = self.registryValue('default', channel)
90
langpaird = self.detect(text)
91
translated = self.tran(text, langpaird).encode("utf-8")
92
#self._preCheck(irc, msg, channel)
93
#self.log.info('Translate %q<>%qin %s due to %s.',
94
# text, translated, channel, msg.prefix)
95
irc.reply('%s, %s' % (msg.nick, translated))
96
tt = wrap(tt, ['text'])
98
#Не сложнее и API для поиска:optional('something'),
101
# query = urllib.urlencode({'q' : text.encode("utf-8")})
102
# url = u'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s'.encode("utf-8") \
104
# search_results = urllib.urlopen(url)
105
# json = simplejson.loads(search_results.read())
106
# results = json['responseData']['results']
109
#results = search("text to search")
110
#title = results[0]['title']
111
#noh_title = title.replace('', '').replace('', '')
112
#url = results[0]['url']
113
#print noh_title+": "+url # Мне повезет! :)
114
# irc.queueMsg(ircmsgs.privmsg(channel, '%s! %s' % (msg.nick, warning)))
118
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: