aboutsummaryrefslogtreecommitdiff
path: root/modules/translate.py
blob: 36d697539952cfa854179b37e0b8168003fe19c8 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#--depends-on commands
#--require-config google-api-key

import json, re
from src import ModuleManager, utils

URL_TRANSLATE = "https://translation.googleapis.com/language/translate/v2"
URL_LANGUAGES = "https://cloud.google.com/translate/docs/languages"
REGEX_LANGUAGES = re.compile("(\w+)?:(\w+)? ")


class Module(ModuleManager.BaseModule):

    @utils.hook("received.command.tr", alias_of="translate")
    @utils.hook("received.command.translate")
    @utils.spec("?<from:to>lstring !<phrase>lstring")
    def translate(self, event):
        """
        :help: Translate the provided phrase or the last line in thie current
            channel
        :usage: [phrase]
        """
        phrase = event["spec"][0]
        source_language = "auto"
        target_language = "en"

        language_match = re.match(REGEX_LANGUAGES, phrase)
        if language_match:
            if language_match.group(1):
                source_language = language_match.group(1)
            if language_match.group(2):
                target_language = language_match.group(2)
            phrase = phrase.split(" ", 1)[1]

        page = utils.http.request(URL_TRANSLATE,
                                  method="POST",
                                  post_data={
                                      "q": phrase,
                                      "format": "text",
                                      "source": source_language,
                                      "target": target_language,
                                      "key": self.bot.config["google-api-key"]
                                  }).json()

        if "data" in page:
            translation = page["data"]["translations"][0]["translatedText"]
            event["stdout"].write("(%s -> %s) %s" % (source_language, target_language, translation))
        else:
            event["stderr"].write("Failed to translate, try checking "
                                  "source/target languages (" + URL_LANGUAGES + ")")