forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_google_translate.py
110 lines (97 loc) · 4.48 KB
/
multi_google_translate.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
"""Use Google Translate to translate your sentence into multiple languages.
Visit the following link to check available languages: \
https://cloud.google.com/translate/docs/languages. To add or remove languages use modifier key \
when trigger is activated or go to: '~/.config/albert/org.albert.extension.mtr/config.json' \
Add or remove elements based on the ISO-Codes that you found on the google documentation page.
Synopsis: <trigger> [query]"""
import json
import os
import urllib.error
import urllib.parse
import urllib.request
from time import sleep
from albertv0 import (ClipAction, Item, ProcAction, UrlAction, configLocation,
iconLookup)
__iid__ = "PythonInterface/v0.2"
__prettyname__ = "MultiTranslate"
__version__ = "1.2"
__trigger__ = "mtr "
__author__ = "David Britt"
__dependencies__ = []
iconPath = iconLookup('config-language')
if not iconPath:
iconPath = ":python_module"
ua = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36"
urltmpl = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=%s&dt=t&q=%s"
urlbrowser = "https://translate.google.com/#auto/%s/%s"
configurationFileName = "language_config.json"
configuration_directory = os.path.join(configLocation(), __prettyname__)
language_configuration_file = os.path.join(configuration_directory, configurationFileName)
languages = []
def initialize():
if os.path.exists(language_configuration_file):
with open(language_configuration_file) as json_config:
languages.extend(json.load(json_config)["languages"])
else:
languages.extend(["en", "zh-CN", "hi", "es", "ru", "pt", "id", "bn", "ar", "ms", "ja", "fr", "de"])
try:
os.makedirs(configuration_directory, exist_ok=True)
try:
with open(language_configuration_file, "w") as output_file:
json.dump({"languages": languages}, output_file)
except OSError:
print("There was an error opening the file: %s" % language_configuration_file)
except OSError:
print("There was an error making the directory: %s" % configuration_directory)
def handleQuery(query):
results = []
if query.isTriggered:
# avoid rate limiting
sleep(0.2)
if not query.isValid:
return
item = Item(
id=__prettyname__,
icon=iconPath,
completion=query.rawString,
text=__prettyname__,
actions=[ProcAction("Open the language configuration file.",
commandline=["xdg-open", language_configuration_file])]
)
if len(query.string) >= 2:
for lang in languages:
try:
url = urltmpl % (lang, urllib.parse.quote_plus(query.string))
req = urllib.request.Request(url, headers={'User-Agent': ua})
with urllib.request.urlopen(req) as response:
#print(type())
#try:
data = json.loads(response.read().decode())
#except TypeError as typerr:
# print("Urgh this type.error. %s" % typerr)
translText = data[0][0][0]
sourceText = data[2]
if sourceText == lang:
continue
else:
results.append(
Item(
id=__prettyname__,
icon=iconPath,
text="%s" % (translText),
subtext="%s" % lang.upper(),
actions=[
ClipAction("Copy translation to clipboard", translText),
UrlAction("Open in your Browser", urlbrowser % (lang, query.string))
]
)
)
except urllib.error.URLError as urlerr :
print("Check your internet connection: %s" % urlerr)
item.subtext = "Check your internet connection."
return item
else:
item.subtext = "Enter a query: 'mtr <text>'. Languages {%s}" % ", ".join(languages)
return item
return results