-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
102 lines (82 loc) · 3.6 KB
/
main.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
# Utilized resources
# https://github.com/YogurtTheHorse/ulauncher-translator
# https://github.com/mouuff/mtranslate
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
import textwrap
import sys
import re
if (sys.version_info[0] < 3):
import urllib2
import urllib
import HTMLParser
else:
import html
import urllib.request
import urllib.parse
agent = {'User-Agent': "Mozilla/5.0 (Android 9; Mobile; rv:67.0.3) Gecko/67.0.3 Firefox/67.0.3"}
def unescape(text):
if (sys.version_info[0] < 3):
parser = HTMLParser.HTMLParser()
else:
parser = html
return (parser.unescape(text))
def translate(to_translate, to_language="auto", from_language="auto", wrap_len="80"):
base_link = "http://translate.google.com/m?tl=%s&sl=%s&q=%s"
if (sys.version_info[0] < 3):
to_translate = urllib.quote_plus(to_translate)
link = base_link % (to_language, from_language, to_translate)
request = urllib2.Request(link, headers=agent)
raw_data = urllib2.urlopen(request).read()
else:
to_translate = urllib.parse.quote(to_translate)
link = base_link % (to_language, from_language, to_translate)
request = urllib.request.Request(link, headers=agent)
raw_data = urllib.request.urlopen(request).read()
data = raw_data.decode("utf-8")
expr = r'class="result-container">(.*?)<'
re_result = re.findall(expr, data)
if (len(re_result) == 0):
result = ""
else:
result = unescape(re_result[0])
return ("\n".join(textwrap.wrap(result, int(wrap_len) if wrap_len.isdigit() else 80 )))
class TranslateExtension(Extension):
def __init__(self):
super(TranslateExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
query = event.get_argument() or str()
if len(query.strip()) == 0:
return RenderResultListAction([
ExtensionResultItem(icon='images/icon.png',
name='No input',
on_enter=HideWindowAction())
])
if len(query)>3 and ":" in query[0]:
from_language = "auto"
to_language = query[1:3]
query = query[3:]
elif len(query)>5 and ":" in query[2]:
from_language = query[:2]
to_language = query[3:5]
query = query[5:]
else:
from_language = extension.preferences["otherlang"]
to_language = extension.preferences["mainlang"]
ceviri = translate(query, to_language, from_language, extension.preferences["wrap"])
items = [
ExtensionResultItem(icon='images/icon.png',
name=query.replace("\n",""),
description=translate(query, to_language, from_language, extension.preferences["wrap"]),
on_enter=CopyToClipboardAction(ceviri))
]
return RenderResultListAction(items)
if __name__ == '__main__':
TranslateExtension().run()