forked from rustycamper/alfred-translate-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_translate.py
124 lines (103 loc) · 3.88 KB
/
base_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import argparse
from google_translate import GoogleTranslate
from msft_translate import MicrosoftTranslate
from workflow import Workflow, ICON_WARNING, PasswordNotFound
GOOGL_API = 'GOOGL'
MSFT_API = 'MSFT'
WF_UPDATE_FREQUENCY = 3 # in days
GITHUB_SLUG = 'pbojkov/alfred-workflow-google-translate'
def main(wf):
if wf.update_available:
# Download new version and tell Alfred to install it
wf.start_update()
parser = argparse.ArgumentParser()
# Add an optional (nargs='?') --setkey argument and save its
# value to 'api_key' (dest). This will be called from a separate "Run Script"
# Alfred action with the API key.
parser.add_argument('--setkey', dest='api_key', nargs='?', default=None)
# API to be used for translations. Supported APIs: GOOGL and MSFT
parser.add_argument('--setapi', dest='api', nargs='?', default='IGNORE')
# Add a query to be translated.
parser.add_argument('query', nargs='?', default=None)
args = parser.parse_args(wf.args)
if args.api and args.api in [GOOGL_API, MSFT_API]:
wf.settings['api'] = args.api
log.debug('*** Setting API to ' + args.api)
return 0
if not args.api:
wf.add_item('Google Translate.',
'Select this to use Google Translate.',
arg=GOOGL_API,
valid=True,
icon=GoogleTranslate.get_icon())
wf.add_item('Microsoft Translate.',
'Select this to use Microsoft Translate.',
arg=MSFT_API,
valid=True,
icon=MicrosoftTranslate.get_icon())
wf.send_feedback()
return 0
if not wf.settings.get('api', None):
wf.add_item('No translation service (API) set.',
'Type tr-setapi to set one.'
+ ' Supported services are Google and Microsoft.',
valid=False,
icon=ICON_WARNING)
wf.send_feedback()
return 0
saved_api = wf.settings.get('api', None)
if saved_api == GOOGL_API:
api = GoogleTranslate(wf)
elif saved_api == MSFT_API:
api = MicrosoftTranslate(wf)
else:
raise RuntimeError("Invalid translation API: " + saved_api)
"""Save the API key, if passed as argument."""
if args.api_key:
api.api_key = args.api_key
return 0
"""Ensure we have an API key stored."""
try:
api.api_key
except PasswordNotFound:
wf.add_item('No API key set.',
'Type "tr-setkey" to set your API key.',
valid=False,
icon=ICON_WARNING)
wf.send_feedback()
return 0
"""Ensure we have a target language set."""
if not api.target_lang:
wf.add_item('No target language set.',
'Type tr-setlang to set a language to translate to.',
valid=False,
icon=ICON_WARNING)
wf.send_feedback()
return 0
# Get query from Alfred
if not args.query:
raise RuntimeError("Expecting query as argument!")
api.query = args.query
translations = api.get_translations()
for tr in translations:
wf.add_item(title=tr['title'],
subtitle=tr['subtitle'],
valid=tr['valid'],
arg=tr['arg'],
copytext=tr['copytext'],
largetext=tr['largetext'],
quicklookurl=tr['quicklookurl'],
icon=tr['icon'])
# Send output to Alfred as XML.
wf.send_feedback()
if __name__ == '__main__':
wf = Workflow(update_settings={
'github_slug': GITHUB_SLUG,
'frequency': WF_UPDATE_FREQUENCY
})
# Assign Workflow logger to a global variable for convenience
log = wf.logger
sys.exit(wf.run(main))