~ubuntu-ru-irc/+junk/irckit

« back to all changes in this revision

Viewing changes to ubuntuhelp/ubuntubot/plugins/Translate/plugin.py

  • Committer: rmPIC30 at gmail
  • Date: 2010-07-13 09:08:58 UTC
  • Revision ID: rmpic30@gmail.com-20100713090858-w5kkmk093hx38cen
Добавляю бота

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###
 
2
# -*- coding: UTF-8 -*-
 
3
# Copyright (c) 2002-2005, A4Tech Email: admin@itmages.ru
 
4
# All rights reserved.
 
5
#
 
6
# Redistribution and use in source and binary forms, with or without
 
7
# modification, are permitted provided that the following conditions are met:
 
8
#
 
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.
 
17
#
 
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.
 
29
###
 
30
 
 
31
import urllib
 
32
import re
 
33
import simplejson
 
34
import supybot.ircmsgs as ircmsgs
 
35
import supybot.ircutils as ircutils
 
36
import supybot.callbacks as callbacks
 
37
from supybot.commands import *
 
38
 
 
39
class Translate(callbacks.Privmsg):
 
40
    """
 
41
    """
 
42
 
 
43
class Translate(callbacks.Plugin):
 
44
    def __init__(self, irc):
 
45
        self.__parent = super(Translate, self)
 
46
        self.__parent.__init__(irc)
 
47
 
 
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']
 
54
        return mess
 
55
 
 
56
    def detect(self, text):
 
57
        langpair = 'en|ru'
 
58
        if re.search("[уеаоэяиюУЕЫАОЯИЮ]", text):
 
59
            langpair = 'ru|en'     
 
60
        return langpair
 
61
 
 
62
    def translate(self, irc, msg, args, text):
 
63
        """<text>
 
64
 
 
65
        Translate <text>.
 
66
        """
 
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'])
 
79
            
 
80
    def tt(self, irc, msg, args, text):
 
81
        """<text>
 
82
 
 
83
        Translate <text>.
 
84
        """
 
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'])
 
97
 
 
98
    #Не сложнее и API для поиска:optional('something'), 
 
99
    #def search(text):
 
100
 
 
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") \
 
103
    #      % (query)
 
104
    #    search_results = urllib.urlopen(url)
 
105
    #    json = simplejson.loads(search_results.read())
 
106
    #    results = json['responseData']['results']
 
107
    #    return results
 
108
 
 
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)))
 
115
 
 
116
Class = Translate
 
117
 
 
118
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: